时间:2021-05-23
获取数据来源
电影名称 打斗次数 接吻次数 电影类型 California Man 3 104 Romance He's Not Really into Dudes 8 95 Romance Beautiful Woman 1 81 Romance Kevin Longblade 111 15 Action Roob Slayer 3000 99 2 Action Amped II 88 10 Action Unknown 18 90 unknown
核心思想:
未标记样本的类别由距离其最近的K个邻居的类别决定
距离度量:
一般距离计算使用欧式距离(用勾股定理计算距离),也可以采用曼哈顿距离(水平上和垂直上的距离之和)、余弦值和相似度(这是距离的另一种表达方式)。相比于上述距离,马氏距离更为精确,因为它能考虑很多因素,比如单位,由于在求协方差矩阵逆矩阵的过程中,可能不存在,而且若碰见3维及3维以上,求解过程中极其复杂,故可不使用马氏距离
知识扩展
cov(x, y) = E(xy) - E(x)*E(y)
cov(x, x) = D(x)
cov(x1+x2, y) = cov(x1, y) + cov(x2, y)
cov(ax, by) = abcov(x, y)
∑ij = [cov(a, a) cov(a, b) cov(a, c) cov(b, a) cov(b,b) cov(b, c) cov(c, a) cov(c, b) cov(c, c)]
算法实现:欧氏距离
编码实现
# 自定义实现 mytest1.pyimport numpy as np# 创建数据集def createDataSet(): features = np.array([[3, 104], [8, 95], [1, 81], [111, 15], [99, 2], [88, 10]]) labels = ["Romance", "Romance", "Romance", "Action", "Action", "Action"] return features, labelsdef knnClassify(testFeature, trainingSet, labels, k): """ KNN算法实现,采用欧式距离 :param testFeature: 测试数据集,ndarray类型,一维数组 :param trainingSet: 训练数据集,ndarray类型,二维数组 :param labels: 训练集对应标签,ndarray类型,一维数组 :param k: k值,int类型 :return: 预测结果,类型与标签中元素一致 """ dataSetsize = trainingSet.shape[0] """ 构建一个由dataSet[i] - testFeature的新的数据集diffMat diffMat中的每个元素都是dataSet中每个特征与testFeature的差值(欧式距离中差) """ testFeatureArray = np.tile(testFeature, (dataSetsize, 1)) diffMat = testFeatureArray - trainingSet # 对每个差值求平方 sqDiffMat = diffMat ** 2 # 计算dataSet中每个属性与testFeature的差的平方的和 sqDistances = sqDiffMat.sum(axis=1) # 计算每个feature与testFeature之间的欧式距离 distances = sqDistances ** 0.5 """ 排序,按照从小到大的顺序记录distances中各个数据的位置 如distance = [5, 9, 0, 2] 则sortedStance = [2, 3, 0, 1] """ sortedDistances = distances.argsort() # 选择距离最小的k个点 classCount = {} for i in range(k): voteiLabel = labels[list(sortedDistances).index(i)] classCount[voteiLabel] = classCount.get(voteiLabel, 0) + 1 # 对k个结果进行统计、排序,选取最终结果,将字典按照value值从大到小排序 sortedclassCount = sorted(classCount.items(), key=lambda x: x[1], reverse=True) return sortedclassCount[0][0]testFeature = np.array([100, 200])features, labels = createDataSet()res = knnClassify(testFeature, features, labels, 3)print(res)# 使用python包实现 mytest2.pyfrom sklearn.neighbors import KNeighborsClassifierfrom .mytest1 import createDataSetfeatures, labels = createDataSet()k = 5clf = KNeighborsClassifier(k_neighbors=k)clf.fit(features, labels)# 样本值my_sample = [[18, 90]]res = clf.predict(my_sample)print(res)数据来源:略
数据显示
import pandas as pdimport numpy as npfrom matplotlib import pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D# 数据加载def loadDatingData(file): datingData = pd.read_table(file, header=None) datingData.columns = ["FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek", "label"] datingTrainData = np.array(datingData[["FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek"]]) datingTrainLabel = np.array(datingData["label"]) return datingData, datingTrainData, datingTrainLabel# 3D图显示数据def dataView3D(datingTrainData, datingTrainLabel): plt.figure(1, figsize=(8, 3)) plt.subplot(111, projection="3d") plt.scatter(np.array([datingTrainData[x][0] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "smallDoses"]), np.array([datingTrainData[x][1] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "smallDoses"]), np.array([datingTrainData[x][2] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "smallDoses"]), c="red") plt.scatter(np.array([datingTrainData[x][0] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "didntLike"]), np.array([datingTrainData[x][1] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "didntLike"]), np.array([datingTrainData[x][2] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "didntLike"]), c="green") plt.scatter(np.array([datingTrainData[x][0] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "largeDoses"]), np.array([datingTrainData[x][1] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "largeDoses"]), np.array([datingTrainData[x][2] for x in range(len(datingTrainLabel)) if datingTrainLabel[x] == "largeDoses"]), c="blue") plt.xlabel("飞行里程数", fontsize=16) plt.ylabel("视频游戏耗时百分比", fontsize=16) plt.clabel("冰淇凌消耗", fontsize=16) plt.show() datingData, datingTrainData, datingTrainLabel = loadDatingData(FILEPATH1)datingView3D(datingTrainData, datingTrainLabel)编码实现
# 自定义方法实现import pandas as pdimport numpy as np# 数据加载def loadDatingData(file): datingData = pd.read_table(file, header=None) datingData.columns = ["FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek", "label"] datingTrainData = np.array(datingData[["FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek"]]) datingTrainLabel = np.array(datingData["label"]) return datingData, datingTrainData, datingTrainLabel# 数据归一化def autoNorm(datingTrainData): # 获取数据集每一列的最值 minValues, maxValues = datingTrainData.min(0), datingTrainData.max(0) diffValues = maxValues - minValues # 定义形状和datingTrainData相似的最小值矩阵和差值矩阵 m = datingTrainData.shape(0) minValuesData = np.tile(minValues, (m, 1)) diffValuesData = np.tile(diffValues, (m, 1)) normValuesData = (datingTrainData-minValuesData)/diffValuesData return normValuesData# 核心算法实现def KNNClassifier(testData, trainData, trainLabel, k): m = trainData.shape(0) testDataArray = np.tile(testData, (m, 1)) diffDataArray = (testDataArray - trainData) ** 2 sumDataArray = diffDataArray.sum(axis=1) ** 0.5 # 对结果进行排序 sumDataSortedArray = sumDataArray.argsort() classCount = {} for i in range(k): labelName = trainLabel[list(sumDataSortedArray).index(i)] classCount[labelName] = classCount.get(labelName, 0)+1 classCount = sorted(classCount.items(), key=lambda x: x[1], reversed=True) return classCount[0][0] # 数据测试def datingTest(file): datingData, datingTrainData, datingTrainLabel = loadDatingData(file) normValuesData = autoNorm(datingTrainData) errorCount = 0 ratio = 0.10 total = datingTrainData.shape(0) numberTest = int(total * ratio) for i in range(numberTest): res = KNNClassifier(normValuesData[i], normValuesData[numberTest:m], datingTrainLabel, 5) if res != datingTrainLabel[i]: errorCount += 1 print("The total error rate is : {}\n".format(error/float(numberTest)))if __name__ == "__main__": FILEPATH = "./datingTestSet1.txt" datingTest(FILEPATH)# python 第三方包实现import pandas as pdimport numpy as npfrom sklearn.neighbors import KNeighborsClassifierif __name__ == "__main__": FILEPATH = "./datingTestSet1.txt" datingData, datingTrainData, datingTrainLabel = loadDatingData(FILEPATH) normValuesData = autoNorm(datingTrainData) errorCount = 0 ratio = 0.10 total = normValuesData.shape[0] numberTest = int(total * ratio) k = 5 clf = KNeighborsClassifier(n_neighbors=k) clf.fit(normValuesData[numberTest:total], datingTrainLabel[numberTest:total]) for i in range(numberTest): res = clf.predict(normValuesData[i].reshape(1, -1)) if res != datingTrainLabel[i]: errorCount += 1 print("The total error rate is : {}\n".format(errorCount/float(numberTest)))以上就是python实现KNN近邻算法的详细内容,更多关于python实现KNN近邻算法的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前面文章分别简单介绍了线性回归,逻辑回归,贝叶斯分类,并且用python简单实现。这篇文章介绍更简单的knn,k-近邻算法(kNN,k-NearestNeigh
一、knn算法描述1.基本概述knn算法,又叫k-近邻算法。属于一个分类算法,主要思想如下:一个样本在特征空间中的k个最近邻的样本中的大多数都属于某一个类别,则
简介邻近算法,或者说K最近邻(kNN,k-NearestNeighbor)分类算法是数据挖掘分类技术中最简单的方法之一。所谓K最近邻,就是k个最近的邻居的意思,
K最近邻(k-NearestNeighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。该方法的思路是:如果一个样本在特征空间
本文实例为大家分享了python实现knn算法的具体代码,供大家参考,具体内容如下knn算法描述对需要分类的点依次执行以下操作:1.计算已知类别数据集中每个点与