机器学习——KNN与降维具体算法实现
Posted Arthur的仓库
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器学习——KNN与降维具体算法实现相关的知识,希望对你有一定的参考价值。
KNN:
import numpy as np
def creatDataSet():
dataSet = [
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
[ ],
]
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:
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 np
import 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与降维具体算法实现的主要内容,如果未能解决你的问题,请参考以下文章