1. 程式人生 > >機器學習入門--kNN演算法

機器學習入門--kNN演算法

新的學習征程

之前的blog主要是關於Android和Nodejs的一些開發學習筆記,在經歷了一段時間的學習和實習之後,也拿到了幾個大廠offer,痛定思痛下決心在大四參加考研,現在考研成功,也要繼續開始新的學習了,接下來的學習將主要以Machine Learning為主,由於很多主流的書籍論文都是英文,希望也能鍛鍊著以後用英文來寫一些學習記錄blog。我個人之前是沒有過任何機器學習相關的學習經驗的,現在也是from 0 to n.

專案原始碼

這一個系列打算都放在這個project裡面,語言是Python,即時更新,希望能點個star

https://github.com/CallMeSp/MachineLearning

正文

機器學習開發大體遵循一定的步驟

  • 收集資料
  • 準備輸入資料
  • 分析輸入資料
  • 訓練演算法(無監督學習則不需要)
  • 測試演算法
  • 使用演算法

機器學習界有個所謂的“十大演算法”或者“二十大演算法”
今天帶來的就是最簡單的一個:k-近鄰演算法

工作原理很簡單:存在一個樣本資料集合,也稱作訓練樣本集,並且樣本集中每個資料都存在標籤,即我們知道樣本集中每一個數據與所屬分類的對應關係。輸入沒有標籤的新資料後,將新資料的每個特徵與樣本集中資料對應的特徵進行比較,然後演算法提取樣本集中特徵最相似的分類標籤。一般來說只是選擇樣本資料集中前k個最相似的資料,這就是k-近鄰演算法中k的出處,簡稱kNN

kNN優劣:

優勢 劣勢
精度高、對異常值不敏感 計算複雜度高
無資料輸入假定 空間複雜度高

演算法:

def classify0(inX,dataSet,labels,k):
    dataSetSize=dataSet.shape[0]
    #[0]和[1]分別是行數和列數
    diffMat = tile(inX,(dataSetSize,1))-dataSet
    sqDiffMat=diffMat**2
    sqDistances=sqDiffMat.sum(axis=1)
    #axis=0指同一行相加
distances=sqDistances**0.5 sortedDistIndicies=distances.argsort() #遞增排列的原陣列下標組成的新陣列 classcount={} for i in range(k): #選擇距離最小的k個點 vl=labels[sortedDistIndicies[i]] classcount[vl]=classcount.get(vl,0)+1 sortedclasscount = sorted(classcount.iteritems(),key=operator.itemgetter(1),reverse=True) #距離最小的k個點中哪個類別佔得比重多 return sortedclasscount[0][0]

歸一化特徵值

這種處理是防止同等權重的特徵因為範圍差距大,而導致某個特徵嚴重的影響到計算結果,所以進行一下處理,將所有的特徵值全都轉化到0~ 1之間,從而保證權重相同,下面看一下程式碼吧。

def autoNorm(dataSet):
    minvals=dataSet.min(0)
    maxvals=dataSet.max(0)
    ranges=maxvals-minvals
    normDataSet=zeros(shape(dataSet))
    m=dataSet.shape[0]
    normDataSet=dataSet-tile(minvals,(m,1))
    normDataSet=normDataSet/tile(ranges,(m,1))
    return normDataSet,ranges,minvals

示例demo

環境:linux+Python2.7
測試用例:在gihub連結中有
本demo參考《Machine Learning in Action》

from numpy import *
import operator
import matplotlib
import matplotlib.pyplot as plt
from os import listdir

#分類,inX是待分類向量,dataSet是已有表現的一組資料,labels就是該標籤元組,k是kNN中對應的k,
def classify0(inX,dataSet,labels,k):
    dataSetSize=dataSet.shape[0]
    diffMat = tile(inX,(dataSetSize,1))-dataSet
    sqDiffMat=diffMat**2
    sqDistances=sqDiffMat.sum(axis=1)
    distances=sqDistances**0.5
    sortedDistIndicies=distances.argsort()
    classcount={}
    for i in range(k):
        vl=labels[sortedDistIndicies[i]]
        classcount[vl]=classcount.get(vl,0)+1
    sortedclasscount = sorted(classcount.iteritems(),key=operator.itemgetter(1),reverse=True)
    return sortedclasscount[0][0]

#將文字檔案轉化為元組,這裡為了更加直觀,測試用例都通過txt形式來展現。
def file2Matrix(filename):
    fr=open(filename)
    arraylines=fr.readlines()
    numberOfLines=len(arraylines)
    returnMat=zeros((numberOfLines,3))
    classLabelVector=[]
    index=0
    for line in arraylines:
        line=line.strip()
        listFromLine=line.split('\t')
        returnMat[index,:]=listFromLine[0:3]
        classLabelVector.append(listFromLine[-1])
        index+=1
    return returnMat,classLabelVector

#通過matplotlib繪圖
def draw():
    datas, labels = file2Matrix('datingTestSet2.txt')
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(datas[:, 1], datas[:, 2], 15.0 * array(map(int,labels)), 15.0 * array(map(int,labels)))
    plt.show()

#歸一化
def autoNorm(dataSet):
    minvals=dataSet.min(0)
    maxvals=dataSet.max(0)
    ranges=maxvals-minvals
    normDataSet=zeros(shape(dataSet))
    m=dataSet.shape[0]
    normDataSet=dataSet-tile(minvals,(m,1))
    normDataSet=normDataSet/tile(ranges,(m,1))
    return normDataSet,ranges,minvals

#將demo中的示例歸一化操作
def normalization():
    dataSet,labels=file2Matrix('datingTestSet2.txt')
    normMat,ranges,minvals=autoNorm(dataSet)
    print normMat
    print ranges
    print minvals

#自動分類測試,列印錯誤率
def datingClassTest():
    #前10%是用於測試正確率,後90%用於訓練分類器
    hoRatio=0.10
    dataSet,labels=file2Matrix('datingTestSet.txt')
    normMat,ranges,minvals=autoNorm(dataSet)
    m=normMat.shape[0]
    numTestVecs=int(m*hoRatio)
    errorCount=0.0
    for i in range(numTestVecs):
        classifierResult = classify0(normMat[i,:],normMat[numTestVecs:m,:],labels[numTestVecs:m],3)
        print "the classifier came back with  : %s,the real answer is : %s"%(classifierResult,labels[i])
        if(classifierResult != labels[i]):
                errorCount+=1
    print "errorCount = %d"%(errorCount)
    print "the total error rate is %f"%(errorCount/float(numTestVecs))

#將32*32文字資訊轉化為1*1024
def img2Vector(filename):
    returnVect=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 returnVect

#手寫識別分類器
def handwritingClassTest():
    hwLabels=[]
    trainingFileList=listdir('trainingDigits')
    m=len(trainingFileList)
    trainingMat=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('trainingDigits/%s'%fileNameStr)

    testFileList=listdir('testDigits')
    errorCount=0.0
    mTest=len(testFileList)
    for i in range(mTest):
        fileNameStr=testFileList[i]
        fileStr=fileNameStr.split('.')[0]
        classNumStr=int (fileStr.split('_')[0])
        vectorUnderTest = img2Vector('testDigits/%s'%fileNameStr)
        classifierResult=classify0(vectorUnderTest,trainingMat,hwLabels,3)
        print "the classifier came 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 "\nthe total error rate is : %f "%(errorCount/float(mTest))


實戰

接下來打算寫個python爬蟲,獲取到測試用例,然後通過kNN演算法來進行一些測試。

to be continued