机器学习——KNN与降维具体算法实现

Posted Arthur的仓库

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器学习——KNN与降维具体算法实现相关的知识,希望对你有一定的参考价值。

KNN:

import numpy as np
def creatDataSet(): dataSet = [ [0.697,0.46,1], [0.774,0.376,1], [0.634,0.264,1], [0.608,0.318,1], [0.556,0.215,1], [0.403,0.237,1], [0.481,0.149,1], [0.437,0.211,1], [0.666,0.091,0], [0.243,0.267,0], [0.245,0.057,0], [0.343,0.099,0], [0.639,0.161,0], [0.657,0.198,0], [0.36,0.37,0], [0.593,0.042,0], [0.719,0.103,0], ] labels = ['密度','含糖率','好瓜'] return dataSet, labels
def calckNeighbourhood(sample, k, dataSet): D = [] N = [] for data in dataSet: #计算出样本点和数据集所有样本的距离 d = pow(sum(pow((np.array(sample) - np.array(data[:-1])), 2)), 1/2) D.append(d) dataSetCopy = list.copy(dataSet) for i in range(0, k): #找出距离最近的k个样本 min_index = np.argmin(D) N.append(dataSetCopy[min_index]) D.pop(min_index) dataSetCopy.pop(min_index) return N
def kNN(sample, k, dataSet, mode): #k为k邻近,sample为样本,mode为分类(0)或者预测(1) N = calckNeighbourhood(sample, k, dataSet) if mode == 0: Class = {} for data in N: #计算k邻近的类别个数 if data[-1] not in Class: Class[data[-1]] = 0 Class[data[-1]] += 1 maxValue = max(Class.values()) for key,value in Class.items(): if value == maxValue: return key elif mode == 1: avg = 0 for data in N: avg += data[-1] avg = avg / k return avg

PCA:

import numpy as npimport scipy.linalg as lina
def calcCovarianceMatrix(X, Y = None): #计算协方差矩阵 m = X.shape[0] X = X - np.mean(X, axis = 0) if Y == None: Y = X else: Y = np.mean(Y, axis = 0) return 1 / m * np.matmul(X.T, Y)
def PCA(X, q): #将n维数据降为q维 covariance_matrix = calcCovarianceMatrix(X) eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix) #获取特征值,特征向量 idx = np.argsort(eigenvalues[::-1]) #返回特征值从大到小的下标 eigenvectors = eigenvectors[:, idx] #特征向量按照从大到小排序 eigenvectors = eigenvectors[:, :q] return np.matmul(X, eigenvectors)


以上是关于机器学习——KNN与降维具体算法实现的主要内容,如果未能解决你的问题,请参考以下文章

机器学习40讲学习笔记15 从回归到分类:联系函数与降维

机器学习经典算法具体解释及Python实现--K近邻(KNN)算法

PHP实现图片的汉明码提取与降维

携英第7期|聚类算法与降维操作

从零开始的AI·吃透kNN算法,学完我悟了(附实例代码)

KNN算法的实现