• 大小: 785KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-15
  • 语言: 其他
  • 标签: 机器学习  

资源简介

机器学习算法,包含随机森林,决策树,SVM,CNN等十几种算法的程序包

资源截图

代码片段和文件信息

# coding:UTF-8
‘‘‘
Date:20160923
@author: zhaozhiyong
‘‘‘

import numpy as np
import math

MinPts = 5  # 定义半径内的最少的数据点的个数

def load_data(file_path):
    ‘‘‘导入数据
    input:  file_path(string):文件名
    output: data(mat):数据
    ‘‘‘
    f = open(file_path)
    data = []
    for line in f.readlines():
        data_tmp = []
        lines = line.strip().split(“\t“)
        for x in lines:
            data_tmp.append(float(x.strip()))
        data.append(data_tmp)
    f.close()
    return np.mat(data)

def epsilon(data MinPts):
    ‘‘‘计算半径
    input:  data(mat):训练数据
            MinPts(int):半径内的数据点的个数
    output: eps(float):半径
    ‘‘‘
    m n = np.shape(data)
    xMax = np.max(data 0)
    xMin = np.min(data 0)
    eps = ((np.prod(xMax - xMin) * MinPts * math.gamma(0.5 * n + 1)) / (m * math.sqrt(math.pi ** n))) ** (1.0 / n)
    return eps
    
def distance(data):
    m n = np.shape(data)
    dis = np.mat(np.zeros((m m)))
    for i in xrange(m):
        for j in xrange(i m):
            # 计算i和j之间的欧式距离
            tmp = 0
            for k in xrange(n):
                tmp += (data[i k] - data[j k]) * (data[i k] - data[j k])
            dis[i j] = np.sqrt(tmp)
            dis[j i] = dis[i j]
    return dis

def find_eps(distance_D eps):
    ind = []
    n = np.shape(distance_D)[1]
    for j in xrange(n):
        if distance_D[0 j] <= eps:
            ind.append(j)
    return ind

def dbscan(data eps MinPts):
    m = np.shape(data)[0]
    # 区分核心点1,边界点0和噪音点-1
    types = np.mat(np.zeros((1 m)))
    sub_class = np.mat(np.zeros((1 m)))
    # 用于判断该点是否处理过,0表示未处理过
    dealed = np.mat(np.zeros((m 1)))
    # 计算每个数据点之间的距离
    dis = distance(data)
    # 用于标记类别
    number = 1
    
    # 对每一个点进行处理
    for i in xrange(m):
        # 找到未处理的点
        if dealed[i 0] == 0:
            # 找到第i个点到其他所有点的距离
            D = dis[i ]
            # 找到半径eps内的所有点
            ind = find_eps(D eps)
            # 区分点的类型
            # 边界点
            if len(ind) > 1 and len(ind) < MinPts + 1:
                types[0 i] = 0
                sub_class[0 i] = 0
            # 噪音点
            if len(ind) == 1:
                types[0 i] = -1
                sub_class[0 i] = -1
                dealed[i 0] = 1
            # 核心点
            if len(ind) >= MinPts + 1:
                types[0 i] = 1
                for x in ind:
                    sub_class[0 x] = number
                # 判断核心点是否密度可达
                while len(ind) > 0:
                    dealed[ind[0] 0] = 1
                    D = dis[ind[0] ]
                    tmp = ind[0]
                    del ind[0]
                    ind_1 = find_eps(D eps)
                    
                    if len(ind_1) > 1:  # 处理非噪音点
                        for x1 in ind_1:
                            sub_class[0 x1] = number
                        if len(ind_1) >= MinPts + 1:
                            types[0 tmp] = 1
                        else:
                            types[0 tmp] = 0
                  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter12_DBSCAN\
     文件        1520  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter12_DBSCAN\data.txt
     文件        4631  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter12_DBSCAN\dbscan.py
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_1 Logistic Regression\
     文件          32  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_1 Logistic Regression\README.md
     文件        7251  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_1 Logistic Regression\data.txt
     文件        2420  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_1 Logistic Regression\lr_test.py
     文件        2907  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_1 Logistic Regression\lr_train.py
     文件        6851  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_1 Logistic Regression\test_data
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_10 KMeans\
     文件        4278  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_10 KMeans\KMeans.py
     文件        2746  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_10 KMeans\KMeanspp.py
     文件        2800  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_10 KMeans\data.txt
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_11 MeanShift\
     文件        3001  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_11 MeanShift\data
     文件        5620  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_11 MeanShift\mean_shift.py
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_13 LabelPropagation\
     文件         131  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_13 LabelPropagation\cd_data.txt
     文件        4356  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_13 LabelPropagation\lb.py
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_14 CollaborativeFiltering\
     文件          50  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_14 CollaborativeFiltering\data.txt
     文件        2386  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_14 CollaborativeFiltering\item_based_recommend.py
     文件        3737  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_14 CollaborativeFiltering\user_based_recommend.py
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_15 MatrixFactorization\
     文件          50  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_15 MatrixFactorization\data.txt
     文件        4325  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_15 MatrixFactorization\mf.py
     文件        1720  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_15 MatrixFactorization\nmf.py
     目录           0  2018-04-16 23:33  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_16 PersonalRank\
     文件          50  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_16 PersonalRank\data.txt
     文件        3262  2017-12-01 23:19  机器学习算法Python-Machine-Learning-Algorithm-master\Chapter_16 PersonalRank\personal_rank.py
............此处省略51个文件信息

评论

共有 条评论