一天一個演算法·筆記
阿新 • • 發佈:2020-07-13
kNN__---只為自己記錄
# !/usr/bin/python # -*- coding: UTF-8 -*- #建立資料集 a = \ [ { "電影名稱": "man", "打鬥鏡頭": 3, "接吻鏡頭": 104, "電影型別": "愛情片" }, { "電影名稱": "He's Not Really into Dudes", "打鬥鏡頭": 2, "接吻鏡頭": 100, "電影型別": "愛情片" }, { "電影名稱": "Amped II", "打鬥鏡頭": 98, "接吻鏡頭": 2, "電影型別": "動作片" }, { "電影名稱": "Robo Slayer 3000", "打鬥鏡頭": 99, "接吻鏡頭": 5, "電影型別": "愛情片" }, ] b = [1, 5, 8, 10, 66, 9] def quick_sort(quick_list): if len(quick_list) < 2: return quick_list mid = quick_list[len(quick_list)//2] left_list = [] right_list = [] quick_list.remove(mid) for i in quick_list: if mid >= i: left_list.append(i) else: right_list.append(i) return quick_sort(left_list) + [mid] + quick_sort(right_list) print(quick_sort(b)) from numpy import * import operator def creatDataSet(): group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) labels = ["A","A","B","B"] return group,labels group,labels = creatDataSet() def classify0(inX, dataset, labels, k): datasetSize = dataset.shape[0] diffMat = tile(inX, (datasetSize, 1)) - dataset sqDiffMat = diffMat ** 2 print(sqDiffMat) sqDistances = sqDiffMat.sum(axis=1) print(sqDistances) distances = sqDistances ** 0.5 print(distances) sortedDistIndicies = distances.argsort() classCount = {} for i in range(k): voteIlabel = labels[sortedDistIndicies[i]] classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1),reverse=True) print(sortedClassCount) return sortedClassCount[0][0] print(classify0([0,0],group,labels,3))
kNN演算法基礎使用