機器學習實踐-k近鄰演算法-約會網站配對原始碼
阿新 • • 發佈:2019-01-01
原始碼如下:
#-*- coding: utf-8 -*- from numpy import * import operator import pdb import matplotlib import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font = FontProperties(fname='/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf', size=15) import sys reload(sys) sys.setdefaultencoding('utf8') def createDataSet(): group = array([[1.0, 1.1], [1.0,1.0], [0,0], [0, 0.1]]) labels = ['A', 'A', 'B', 'B'] return group, labels def classify0(inX, dataSet, labels, k): #pdb.set_trace() 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): voteIlabel = labels[sortedDistIndicies[i]] classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True) return sortedClassCount[0][0] def file2matrix(filename): # pdb.set_trace() fr = open(filename) arrayOfLines = fr.readlines() numberOfLines = len(arrayOfLines) returnMat = zeros((numberOfLines, 3)) classLabelVector = [] index = 0 for line in arrayOfLines: line = line.strip() listFromLine = line.split('\t') returnMat[index,:] = listFromLine[0:3] classLabelVector.append(int(listFromLine[-1])) index += 1 return returnMat, classLabelVector def conf_zh(font_name): from pylab import mpl mpl.rcParams['font.sans-serif'] = [font_name] mpl.rcParams['axes.unicode_minus'] = False def drawPlot(): datingDataMat, datingLabels = file2matrix('datingTestSet2.txt') fig = plt.figure() ax = fig.add_subplot(111) ax.set_title('你好',fontproperties=font) plt.xlabel(u"每年獲取的飛行常客里程數",fontproperties=font) plt.ylabel(u"玩視訊遊戲所耗時間百分比", fontproperties=font) ax.scatter(datingDataMat[:, 0], datingDataMat[:, 1], 15.0 * array(datingLabels), 1.0 * array(datingLabels)) plt.legend(u"不喜歡", prop=font) plt.show() def autoNorm(dataSet): #pdb.set_trace() 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 def datingClassTest(): hoRatio = 0.10 datingDataMat, datingLabels = file2matrix('datingTestSet2.txt') norMat, ranges, minVals = autoNorm(datingDataMat) m = norMat.shape[0] numTestVecs = int(m * hoRatio) errorCount = 0.0 for i in range(numTestVecs): classifierResult = classify0(norMat[i, :], norMat[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)) def classifyPerson(): resultList = ['not at all', 'in small doses', 'in large doses'] percentTats = float(raw_input("percentage of time spent playing video games?")) ffMiles = float(raw_input("frequent flier miles earned per year?")) iceCream = float(raw_input("liters of ice cream consumed per year?")) datingDataMat, datingLabels = file2matrix('datingTestSet2.txt') norMat, ranges, minVals = autoNorm(datingDataMat) inArr = array([ffMiles, percentTats, iceCream]) classifierResult = classify0((inArr - minVals)/ ranges, norMat, datingLabels, 3) print "You will probably like this person: ", resultList[classifierResult\ -1] if __name__ == '__main__': #group, labels = createDataSet() #print classify0([0,0], group, labels, 3) #print group #print labels #drawPlot() #datingClassTest() classifyPerson()