利用sklearn包中的k-近鄰演算法進行分類
阿新 • • 發佈:2019-02-04
剛開始學習機器學習不久,這也是我第一次寫部落格。就先寫個簡單的,利用sklearn包中的kNN分類器進行分類,分類的資料集為皮馬印第安人糖尿病資料集(pima-indians-diabetes.data.csv)。廢話不多說,直接上程式碼,寫的不完善的地方,望大家指正。
#coding:utf-8 ''''' Create by Ma Chao August 4th kNN ''''' from sklearn.neighbors import NearestNeighbors, KNeighborsClassifier from sklearn import datasets import numpy as np
程式碼中所用資料集可在此處下載:http://download.csdn.net/detail/liexian2816/9922400#載入資料集 def loadDataSet(fileName): input = [] output = [] with open(fileName) as fr: for line in fr.readlines(): lineArr = line.strip().split(',') input.append([float(lineArr[0]), float(lineArr[1]), float(lineArr[2]), float(lineArr[3]), float(lineArr[4]), float(lineArr[5]), float(lineArr[6]), float(lineArr[7])]) output.append(int(lineArr[8])) return input, output if __name__ == "__main__": input, output = loadDataSet('pima-indians-diabetes.data.csv') np.random.seed(0) indices = np.random.permutation(len(input)) input_train = input[:-10] output_train = output[:-10] input_test = input[-10:] output_test = output[-10:] kNN = KNeighborsClassifier() kNN.fit(input_train, output_train) output_predict = kNN.predict(input_test) prob = kNN.predict_proba(input_test) score = kNN.score(input_test, output_test, sample_weight=None) kNeighbor = kNN.kneighbors(input_test[:-1], 4)#找出與測試集中最後一個元素距離最近的4個元素 print 'output_test:', output_test print 'output_predic:', output_predict print '****************' print 'Prob:', prob print '****************' print 'Score:', score