91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

《機器學習實戰》源代碼Python3

資源截圖

代碼片段和文件信息

‘‘‘
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
‘‘‘
import?numpy?as?np
import?operator
from?os?import?listdir

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
????sortedDistIndicies?=?distances.argsort()
????classCount?=?{}
????for?i?in?range(k):
????????voteIlabel?=?labels[sortedDistIndicies[i]]
????????classCount[voteIlabel]?=?classCount.get(voteIlabel?0)?+?1
????sortedClassCount?=?sorted(classCount.items()?key=operator.itemgetter(1)?reverse=True)
????return?sortedClassCount[0][0]

def?createDataSet():
????group?=?np.array([[1.0?1.1]?[1.0?1.0]?[0?0]?[0?0.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?=?np.zeros((numberOfLines?3))????????#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?returnMat?classLabelVector


def?autoNorm(dataSet):
????minVals?=?dataSet.min(0)
????maxVals?=?dataSet.max(0)
????ranges?=?maxVals?-?minVals
????normDataSet?=?np.zeros(np.shape(dataSet))
????m?=?dataSet.shape[0]
????normDataSet?=?dataSet?-?np.tile(minVals?(m?1))
????normDataSet?=?normDataSet/np.tile(ranges?(m?1))???#element?wise?divide
????return?normDataSet?ranges?minVals

def?datingClassTest():
????hoRatio?=?0.50??????#hold?out?10%
????datingDataMat?datingLabels?=?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(errorC

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\
?????文件????????1045??2017-12-25?12:43??machinelearninginaction3x-master\.gitignore
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\EXTRAS\
?????文件?????????514??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\EXTRAS\README.txt
?????文件????????1988??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\EXTRAS\createDist.py
?????文件????????2094??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\EXTRAS\createDist2.py
?????文件?????????557??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\EXTRAS\createFirstPlot.py
?????文件???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\EXTRAS\testSet.txt
?????文件?????????239??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\README.txt
?????文件???????34725??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\datingTestSet.txt
?????文件???????26067??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\datingTestSet2.txt
?????文件??????739988??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\digits.zip
?????文件????????5230??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\kNN.py
?????文件?????????600??2017-12-25?12:43??machinelearninginaction3x-master\Ch02\kNNTest.py
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch03\
?????文件??????????84??2017-12-25?12:43??machinelearninginaction3x-master\Ch03\classifierStorage.txt
?????文件?????????771??2017-12-25?12:43??machinelearninginaction3x-master\Ch03\lenses.txt
?????文件????????3848??2017-12-25?12:43??machinelearninginaction3x-master\Ch03\treePlotter.py
?????文件????????4021??2017-12-25?12:43??machinelearninginaction3x-master\Ch03\trees.py
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\EXTRAS\
?????文件?????????514??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\EXTRAS\README.txt
?????文件?????????928??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\EXTRAS\create2Normal.py
?????文件?????????445??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\EXTRAS\monoDemo.py
?????文件????????7250??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\bayes.py
?????文件???????15141??2017-12-25?12:43??machinelearninginaction3x-master\Ch04\email.zip
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch05\
?????目錄???????????0??2017-12-25?12:43??machinelearninginaction3x-master\Ch05\EXTRAS\
?????文件?????????514??2017-12-25?12:43??machinelearninginaction3x-master\Ch05\EXTRAS\README.txt
?????文件????????1233??2017-12-25?12:43??machinelearninginaction3x-master\Ch05\EXTRAS\plot2D.py
............此處省略111個文件信息

評論

共有 條評論