資源簡(jiǎn)介
機(jī)器學(xué)習(xí)實(shí)戰(zhàn)(源碼和數(shù)據(jù)樣本)
代碼片段和文件信息
‘‘‘
Created?on?Sep?16?2010
kNN:?k?Nearest?Neighbors
Input:??????inX:?vector?to?compare?to?existing?dataset?(1xN)
????????????dataSet:?size?m?data?set?of?known?vectors?(NxM)
????????????labels:?data?set?labels?(1xM?vector)
????????????k:?number?of?neighbors?to?use?for?comparison?(should?be?an?odd?number)
????????????
Output:?????the?most?popular?class?label
@author:?pbharrin
‘‘‘
from?numpy?import?*
import?operator
from?os?import?listdir
def?classify0(inX?dataSet?labels?k):
????dataSetSize?=?dataSet.shape[0]
????diffMat?=?tile(inX?(dataSetSize1))?-?dataSet
????sqDiffMat?=?diffMat**2
????sqDistances?=?sqDiffMat.sum(axis=1)
????distances?=?sqDistances**0.5
????sortedDistIndicies?=?distances.argsort()?????
????classCount={}??????????
????for?i?in?range(k):
????????voteIlabel?=?labels[sortedDistIndicies[i]]
????????classCount[voteIlabel]?=?classCount.get(voteIlabel0)?+?1
????sortedClassCount?=?sorted(classCount.iteritems()?key=operator.itemgetter(1)?reverse=True)
????return?sortedClassCount[0][0]
def?createDataSet():
????group?=?array([[1.01.1][1.01.0][00][00.1]])
????labels?=?[‘A‘‘A‘‘B‘‘B‘]
????return?group?labels
def?file2matrix(filename):
????love_dictionary={‘largeDoses‘:3?‘smallDoses‘:2?‘didntLike‘:1}
????fr?=?open(filename)
????arrayOLines?=?fr.readlines()
????numberOfLines?=?len(arrayOLines)????????????#get?the?number?of?lines?in?the?file
????returnMat?=?zeros((numberOfLines3))????????#prepare?matrix?to?return
????classLabelVector?=?[]???????????????????????#prepare?labels?return???
????index?=?0
????for?line?in?arrayOLines:
????????line?=?line.strip()
????????listFromLine?=?line.split(‘\t‘)
????????returnMat[index:]?=?listFromLine[0:3]
????????if(listFromLine[-1].isdigit()):
????????????classLabelVector.append(int(listFromLine[-1]))
????????else:
????????????classLabelVector.append(love_dictionary.get(listFromLine[-1]))
????????index?+=?1
????return?returnMatclassLabelVector
????
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?(m1))
????normDataSet?=?normDataSet/tile(ranges?(m1))???#element?wise?divide
????return?normDataSet?ranges?minVals
???
def?datingClassTest():
????hoRatio?=?0.50??????#hold?out?10%
????datingDataMatdatingLabels?=?file2matrix(‘datingTestSet2.txt‘)???????#load?data?setfrom?file
????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?rate?is:?%f“?%?(errorCount/float(numTestVecs))
????print?errorCount
????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch02\
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch02\EXTRAS\
?????文件?????????514??2016-02-18?14:50??machinelearninginaction-master\Ch02\EXTRAS\README.txt
?????文件????????1988??2016-02-18?14:50??machinelearninginaction-master\Ch02\EXTRAS\createDist.py
?????文件????????2094??2016-02-18?14:50??machinelearninginaction-master\Ch02\EXTRAS\createDist2.py
?????文件?????????543??2016-02-18?14:50??machinelearninginaction-master\Ch02\EXTRAS\createFirstPlot.py
?????文件???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch02\EXTRAS\testSet.txt
?????文件?????????239??2016-02-18?14:50??machinelearninginaction-master\Ch02\README.txt
?????文件???????34725??2016-02-18?14:50??machinelearninginaction-master\Ch02\datingTestSet.txt
?????文件???????26067??2016-02-18?14:50??machinelearninginaction-master\Ch02\datingTestSet2.txt
?????文件??????739988??2016-02-18?14:50??machinelearninginaction-master\Ch02\digits.zip
?????文件????????5222??2016-02-18?14:50??machinelearninginaction-master\Ch02\kNN.py
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch03\
?????文件??????????84??2016-02-18?14:50??machinelearninginaction-master\Ch03\classifierStorage.txt
?????文件?????????771??2016-02-18?14:50??machinelearninginaction-master\Ch03\lenses.txt
?????文件????????3824??2016-02-18?14:50??machinelearninginaction-master\Ch03\treePlotter.py
?????文件????????4065??2016-02-18?14:50??machinelearninginaction-master\Ch03\trees.py
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch04\
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch04\EXTRAS\
?????文件?????????514??2016-02-18?14:50??machinelearninginaction-master\Ch04\EXTRAS\README.txt
?????文件?????????922??2016-02-18?14:50??machinelearninginaction-master\Ch04\EXTRAS\create2Normal.py
?????文件?????????433??2016-02-18?14:50??machinelearninginaction-master\Ch04\EXTRAS\monoDemo.py
?????文件????????7076??2016-02-18?14:50??machinelearninginaction-master\Ch04\bayes.py
?????文件???????15141??2016-02-18?14:50??machinelearninginaction-master\Ch04\email.zip
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch05\
?????目錄???????????0??2016-02-18?14:50??machinelearninginaction-master\Ch05\EXTRAS\
?????文件?????????514??2016-02-18?14:50??machinelearninginaction-master\Ch05\EXTRAS\README.txt
?????文件????????1233??2016-02-18?14:50??machinelearninginaction-master\Ch05\EXTRAS\plot2D.py
?????文件????????1710??2016-02-18?14:50??machinelearninginaction-master\Ch05\EXTRAS\plotGD.py
?????文件????????1846??2016-02-18?14:50??machinelearninginaction-master\Ch05\EXTRAS\plotSDerror.py
............此處省略99個(gè)文件信息
評(píng)論
共有 條評(píng)論