資源簡介
機器學(xué)習(xí)實戰(zhàn)——KNN算法代碼+數(shù)據(jù)
代碼片段和文件信息
#!/usr/bin/env?python3
#?-*-?coding:?utf-8?-*-
__author__=‘Kevin?Liao‘
#監(jiān)督學(xué)習(xí)算法————k-臨近算法的特點
#優(yōu)點:精度高、對異常值不敏感、無數(shù)據(jù)輸入假定
#缺點:計算復(fù)雜度高、空間復(fù)雜度高,耗時、有時所需樣本量大,無法給出任何數(shù)據(jù)的基礎(chǔ)結(jié)構(gòu)信息
#適用數(shù)據(jù)范圍:數(shù)值型和標(biāo)稱型
from?numpy?import?*
import?operator
def?createDataSet():
????group?=?array([[1.01.1][1.01.0][00][00.1]])
????labels=[‘A‘‘A‘‘B‘‘B‘]
????return?grouplabels
#————————約會網(wǎng)址預(yù)測
def?classify0(inXdataSetlabelsk):
????dataSetSize=dataSet.shape[0]
????#以下三行計算距離
????diffMat=tile(inX?(dataSetSize1))-dataSet
????sqDiffMat=diffMat**2
????sqDistances=sqDiffMat.sum(axis=1)
????distances=sqDistances**0.5
????sortedDistIndicies=distances.argsort()
????#選擇距離最小的k個點?
????classCount={}
????for?i?in?range(k):
????????votellabel=labels[sortedDistIndicies[i]]
????????classCount[votellabe
評論
共有 條評論