示例:《电影类型分类》
获取数据来源
电影名称
打斗次数
接吻次数
电影类型
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
数据显示:肉眼判断电影类型unknown是什么
from matplotlib import pyplot as plt "font.sans-serif"] = ["SimHei"] # 电影名称 names = ["California Man", "He's Not Really into Dudes", "Beautiful Woman", "Kevin Longblade", "Robo Slayer 3000", "Amped II", "Unknown"] # 类型标签 labels = ["Romance", "Romance", "Romance", "Action", "Action", "Action", "Unknown"] colors = ["darkblue", "red", "green"] colorDict = {label: color for (label, color) in zip(set(labels), colors)} print(colorDict) # 打斗次数,接吻次数 X = [3, 8, 1, 111, 99, 88, 18] Y = [104, 95, 81, 15, 2, 10, 88] "通过打斗次数和接吻次数判断电影类型", fontsize=18) plt.xlabel("电影中打斗镜头出现的次数", fontsize=16) plt.ylabel("电影中接吻镜头出现的次数", fontsize=16) "htmlcode"># 自定义实现 mytest1.py import numpy as np "Romance", "Romance", "Romance", "Action", "Action", "Action"] return features, labels """ 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() "htmlcode">import pandas as pd import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D "FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek", "label"] datingTrainData = np.array(datingData[["FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek"]]) datingTrainLabel = np.array(datingData["label"]) return datingData, datingTrainData, datingTrainLabel "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)问题分析:抽取数据集的前10%在数据集的后90%进行测试
编码实现
# 自定义方法实现 import pandas as pd import numpy as np "FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek", "label"] datingTrainData = np.array(datingData[["FlightDistance", "PlaytimePreweek", "IcecreamCostPreweek"]]) datingTrainLabel = np.array(datingData["label"]) return datingData, datingTrainData, datingTrainLabel "The total error rate is : {}\n".format(error/float(numberTest))) "__main__": FILEPATH = "./datingTestSet1.txt" datingTest(FILEPATH) # python 第三方包实现 import pandas as pd import numpy as np from sklearn.neighbors import KNeighborsClassifier "__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近邻算法的资料请关注其它相关文章!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- [发烧人声]群星《邂逅》DTS-WAV
- 艻打绿《夏/狂热(苏打绿版)》[320K/MP3][106.42MB]
- 艻打绿《夏/狂热(苏打绿版)》[FLAC分轨][574.2MB]
- 黄雨勳《魔法列车首部曲》[320K/MP3][33.1MB]
- 李蕙敏.2014-记得·销魂新歌+精丫乐意唱片】【WAV+CUE】
- 谢金燕.1995-含泪跳恰蔷冠登】【WAV+CUE】
- 于文文.2024-天蝎座【华纳】【FLAC分轨】
- 黄雨勳《魔法列车首部曲》[FLAC/分轨][173.61MB]
- 群星《歌手2024 第13期》[320K/MP3][50.09MB]
- 群星《歌手2024 第13期》[FLAC/分轨][325.93MB]
- 阿木乃《爱情买卖》DTS-ES【NRG镜像】
- 江蕾《爱是这样甜》DTS-WAV
- VA-Hair(OriginalBroadwayCastRecording)(1968)(PBTHAL24-96FLAC)
- 博主分享《美末2RE》PS5 Pro运行画面 玩家仍不买账
- 《双城之战2》超多新歌MV发布:林肯公园再次献声