国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實(shí)現(xiàn)的knn算法示例

2020-02-15 21:51:43
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)的knn算法。分享給大家供大家參考,具體如下:

代碼參考機(jī)器學(xué)習(xí)實(shí)戰(zhàn)那本書:

機(jī)器學(xué)習(xí)實(shí)戰(zhàn) (Peter Harrington著) 中文版

機(jī)器學(xué)習(xí)實(shí)戰(zhàn) (Peter Harrington著) 英文原版[附源代碼]

有興趣你們可以去了解下

具體代碼:

# -*- coding:utf-8 -*-#! python2'''''@author:zhoumeixucreatedate:2015年8月27日'''#np.zeros((4,2))#np.zeros(8).reshape(4,2)#x=np.array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) np.zeros_like(x)# 最值和排序:最值有np.max(),np.min() 他們都有axis和out(輸出)參數(shù),# 而通過np.argmax(), np.argmin()可以得到取得最大或最小值時(shí)的 下標(biāo)。# 排序通過np.sort(), 而np.argsort()得到的是排序后的數(shù)據(jù)原來位置的下標(biāo)# 簡單實(shí)現(xiàn)knn算法的基本思路import numpy as npimport operator #運(yùn)算符操作包from _ctypes import Arrayfrom statsmodels.sandbox.regression.kernridgeregress_class import plt_closealldef createDataSet(): group=np.array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) labels=['A','A','B','B'] return group ,labelsgroup,labels=createDataSet()def classify0(inx,dataSet,labels,k): dataSetSize=dataSet.shape[0] diffMat=np.tile(inx,(dataSetSize,1))-dataSet sqDiffMat=diffMat**2 sqDistances=sqDiffMat.sum(axis=1) distances=sqDistances**0.5   #計(jì)算距離 python中會(huì)自動(dòng)廣播的形式 sortedDistIndicies=distances.argsort() #排序,得到原來數(shù)據(jù)的在原來所在的下標(biāo) classCount={} for i in range(k):  voteIlabel=labels[sortedDistIndicies[i]] # 計(jì)算距離最近的值所在label標(biāo)簽  classCount[voteIlabel]=classCount.get(voteIlabel,0)+1 # 計(jì)算距離最近的值所在label標(biāo)簽,對(duì)前k哥最近數(shù)據(jù)進(jìn)行累加 sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True) #排序得到距離k個(gè)最近的數(shù)所在的標(biāo)簽 return sortedClassCount[0][0]if __name__=='__main__': print(classify0([0,0],group,labels,4))# 利用knn算法改進(jìn)約會(huì)網(wǎng)站的配對(duì)效果def file2matrix(filename): fr=open(filename) arrayOLines=fr.readlines() numberOfLines=len(arrayOLines) returnMat=np.zeros((numberOfLines,3)) classLabelVector=[] index=0 for line in arrayOLines:  line=line.strip()  listFromLine=line.split('/t')  returnMat[index,:]=listFromLine[0:3]  classLabelVector.append(int(listFromLine[-1]))  index+=1 return returnMat ,classLabelVector #生成訓(xùn)練數(shù)據(jù)的array和目標(biāo)arraypath=u'D://Users//zhoumeixu204//Desktop//python語言機(jī)器學(xué)習(xí)//機(jī)器學(xué)習(xí)實(shí)戰(zhàn)代碼 python//機(jī)器學(xué)習(xí)實(shí)戰(zhàn)代碼//machinelearninginaction//Ch02//'datingDataMat,datingLabels=file2matrix(path+'datingTestSet2.txt')import matplotlibimport matplotlib.pyplot as pltfig=plt.figure()ax=fig.add_subplot(111)ax.scatter(datingDataMat[:,1],datingDataMat[:,2])plt.show()ax.scatter(datingDataMat[:,1],datingDataMat[:,2],15.0*np.array(datingLabels),15*np.array(datingDataMat[:,2]))plt.show()  #生成訓(xùn)練數(shù)據(jù)的array和目標(biāo)arraydef autoNorm(dataset): minVals=dataset.min(0) maxVals=dataset.max(0) ranges=maxVals-minVals normeDataSet=np.zeros(np.shape(dataset)) m=dataset.shape[0] normDataSet=dataset-np.tile(minVals,(m,1)) normDataSet=normDataSet/np.tile(ranges,(m,1)) return normDataSet ,ranges,minValsnormMat,ranges,minVals=autoNorm(datingDataMat)def datingClassTest(): hoRatio=0.1 datingDataMat,datingLabels=file2matrix(path+'datingTestSet2.txt') normMat,ranges,minVals=autoNorm(datingDataMat) m=normMat.shape[0] numTestVecs=int(m*hoRatio) errorCount=0.0 for i in range(numTestVecs):  classifierResult=classify0(normMat[i,:], normMat[numTestVecs:m,:], datingLabels[numTestVecs:m],3)  print "the classifier came back with :%d,the real answer is :%d"/     %(classifierResult,datingLabels[i])  if classifierResult!=datingLabels[i]:   errorCount+=1.0 print "the total error rare is :%f"%(errorCount/float(numTestVecs)) #利用knn算法測試錯(cuò)誤率if __name__=='__main__': datingClassTest()#利用構(gòu)建好的模型進(jìn)行預(yù)測def classifyPerson(): resultList=['not at all','in same doses','in large d oses'] percentTats=float(raw_input("percentage if time spent playin cideo games:")) ffMiles=float(raw_input("frequnet fliter miles earned per year:")) iceCream=float(raw_input("liters of ice cream consumed per year:")) datingDataMat,datingLabels=file2matrix(path+'datingTestSet2.txt') normMat,ranges,minVals=autoNorm(datingDataMat) inArr=np.array([ffMiles,percentTats,iceCream]) classifierResult=classify0((inArr-minVals)/ranges,normMat,datingLabels,3) print("you will probably like the person:",resultList[classifierResult-1])if __name__!='__main__': classifyPerson()#利用knn算法進(jìn)行手寫識(shí)別系統(tǒng)驗(yàn)證path=u'D://Users//zhoumeixu204//Desktop//python語言機(jī)器學(xué)習(xí)//機(jī)器學(xué)習(xí)實(shí)戰(zhàn)代碼 python//機(jī)器學(xué)習(xí)實(shí)戰(zhàn)代碼//machinelearninginaction//Ch02//'def img2vector(filename): returnVect=np.zeros((1,1024)) fr=open(filename) for i in range(32):  lineStr=fr.readline()  for j in range(32):   returnVect[0,32*i+j]=int(lineStr[j]) return returnVecttestVector=img2vector(path+'testDigits//0_13.txt')print(testVector[0,0:31])import osdef handwritingClassTest(): hwLabels=[] trainingFileList=os.listdir(path+'trainingDigits') m=len(trainingFileList) trainingMat=np.zeros((m,1024)) for i in range(m):  fileNameStr=trainingFileList[i]  fileStr=fileNameStr.split('.')[0]  classNumStr=int(fileStr.split('_')[0])  hwLabels.append(classNumStr)  trainingMat[i,:]=img2vector(path+'trainingDigits//'+fileNameStr) testFileList=os.listdir(path+'testDigits') errorCount=0.0 mTest=len(testFileList) for j in range(mTest):  fileNameStr=testFileList[j]  fileStr=fileNameStr.split('.')[0]  classNumStr=int(fileNameStr.split('_')[0])  classNumStr=int(fileStr.split('_')[0])  vectorUnderTest=img2vector(path+'testDigits//'+fileNameStr)  classifierResult=classify0(vectorUnderTest,trainingMat,hwLabels,3)  print("the classifier canme back with:%d,the real answer is :%d"%(classifierResult,classNumStr))  if classifierResult!=classNumStr:   errorCount+=1.0 print("/nthe total number of errors is :%d"%errorCount) print("/n the total error rate is :%f"%(errorCount/float(mTest)))if __name__=='__main__': handwritingClassTest()            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲麻莱县| 兴化市| 安国市| 金塔县| 丹巴县| 嫩江县| 顺昌县| 射洪县| 陇南市| 大埔区| 墨竹工卡县| 武隆县| 抚州市| 靖远县| 荔波县| 鹿泉市| 泗水县| 山丹县| 饶平县| 江门市| 罗田县| 牙克石市| 吴川市| 平山县| 巩留县| 泰宁县| 崇左市| 新源县| 昂仁县| 民权县| 介休市| 新沂市| 织金县| 竹山县| 临泽县| 沙雅县| 望奎县| 昆山市| 铜陵市| 稷山县| 延吉市|