案例-k-近邻-改进约会网站的配对效果
思路
- 收集数据:提供文本数据
- 准备数据:解析文本数据
- 分析数据:使用matplotlib 二维扩散图
- 训练算法:不适用于k-近邻
- 测试算法:切分测试样本; 测试样本是已经完成分类的数据,如果预测分类与实际分类不同,则标记为错误
- 使用算法:程序执行,输入一些特征数据来判断是否为喜欢的类型
- 三种类型:不喜欢的、一般的、喜欢的
准备数据
import numpy as np
import matplotlib.pyplot as plt
import operator
1000,3)).shape np.zeros((
(1000, 3)
def file2matrix(filename):
with open (filename,'r') as f:
= f.readlines()
arrayOLines # print(arrayLines)
= len(arrayOLines)
numberOflines# print(numberOflines)
= np.zeros((numberOflines,3)) # 创建numpy矩阵
returnMat # print(returnMat)
= []
classLabelVector = 0
indexfor line in arrayOLines:
= line.strip()
line # print(line)
= line.split('\t')
listFromLine # print(listFromLine)
# x[:,n]表示在全部数组(维)中取第n个数据,直观来说,x[:,n]就是取所有集合的第n个数据,
# x[n,:]表示在n个数组(维)中取全部数据,直观来说,x[n,:]就是取第n集合的所有数据,
= listFromLine[0:3]
returnMat[index,:] # print(returnMat)
int(listFromLine[-1]))
classLabelVector.append(# print(classLabelVector)
+=1
index return returnMat,classLabelVector
# 每年获得的飞行常客里程数、玩视频游戏所耗时间的占比、每周消费的冰淇淋公升数
=file2matrix("./dataset/datingTestSet2.txt") datingDataMat,datingLabels
datingDataMat
array([[4.0920000e+04, 8.3269760e+00, 9.5395200e-01],
[1.4488000e+04, 7.1534690e+00, 1.6739040e+00],
[2.6052000e+04, 1.4418710e+00, 8.0512400e-01],
...,
[2.6575000e+04, 1.0650102e+01, 8.6662700e-01],
[4.8111000e+04, 9.1345280e+00, 7.2804500e-01],
[4.3757000e+04, 7.8826010e+00, 1.3324460e+00]])
0:20] datingLabels[
[3, 2, 1, 1, 1, 1, 3, 3, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2, 3]
分析数据
= plt.figure()
fig= fig.add_subplot(111)
ax # 第二列和第三列数据
1], datingDataMat[:,2])
ax.scatter(datingDataMat[:, plt.show()
= plt.figure()
fig= fig.add_subplot(111)
ax # 第二列和第三列数据
1], datingDataMat[:,2], 15.0*np.array(datingLabels) , 15.0*np.array(datingLabels))
ax.scatter(datingDataMat[:, plt.show()
= plt.figure()
fig= fig.add_subplot(111)
ax # 第二列和第三列数据
0], datingDataMat[:,1], 15.0*np.array(datingLabels) , 15.0*np.array(datingLabels))
ax.scatter(datingDataMat[:, plt.show()
= plt.figure()
fig= fig.add_subplot(111)
ax # 第二列和第三列数据
0], datingDataMat[:,2], 15.0*np.array(datingLabels) , 15.0*np.array(datingLabels))
ax.scatter(datingDataMat[:, plt.show()
准备数据:归一化数据
- 将数值归一化,如将取值范围 8 处理为0到1或者-1到1之间 newValue = (oldValue -min) / (max-min)
def autoNorm(dataSet):
= dataSet.min(0)
minVals = dataSet.max(0)
maxVals = maxVals- minVals
ranges = np.zeros(np.shape(dataSet))
normDataSet = dataSet.shape[0] # 行数
m # print(m)
= dataSet - np.tile(minVals , (m,1))
normDataSet = normDataSet / np.tile(ranges,(m,1)) # 特征值相除
normDataSet return normDataSet,ranges,minVals
datingDataMat.shape
(1000, 3)
= autoNorm(datingDataMat) normMat , ranges, minVals
1000
normMat
array([[0.44832535, 0.39805139, 0.56233353],
[0.15873259, 0.34195467, 0.98724416],
[0.28542943, 0.06892523, 0.47449629],
...,
[0.29115949, 0.50910294, 0.51079493],
[0.52711097, 0.43665451, 0.4290048 ],
[0.47940793, 0.3768091 , 0.78571804]])
10:20,:] normMat[
array([[0.55045851, 0.17799301, 0.49030933],
[0.69324992, 0.40086711, 0.9846361 ],
[0.06101476, 0.23305864, 0.42936659],
[0.55933299, 0.22372102, 0.36832056],
[0.84769866, 0.73135976, 0.19487878],
[0.47848761, 0.09032121, 0.11221162],
[0.67231273, 0.35932065, 0.74836944],
[0.76334732, 0.68067104, 0.15355464],
[0.1716718 , 0. , 0.73716817],
[0.31211859, 0.50329267, 0.76942753]])
min(0).shape datingDataMat.
array([9.1273000e+04, 2.0919349e+01, 1.6943610e+00])
minVals
array([0. , 0. , 0.001156])
def classify0(inX, dataSet, labels, k):
# shape[0]:表示矩阵的行数
# shape[1]:表示矩阵的列数
= dataSet.shape[0]
dataSetSize # print(dataSetSize)
# 距离计算
# np.tile(a,(2,1))第一个参数为Y轴扩大倍数,第二个为X轴行扩大倍数。
# np.tile(a,(2))函数的作用就是将函数将函数沿着X轴/行扩大两倍。如果扩大倍数只有一个,默认为X轴
= np.tile(inX, (dataSetSize,1))-dataSet
diffMat = diffMat ** 2
sqDiffMat = sqDiffMat.sum(axis=1)
sqDistances = sqDistances ** 0.5
distances = distances.argsort()
sortedDistIndicies # print(sortedDistIndicies)
# 选择距离最小的k个点
={}
classCountfor i in range(k):
= labels[sortedDistIndicies[i]]
voteIlabel print("voteIlabel:",voteIlabel)
= classCount.get(voteIlabel,0) +1
classCount[voteIlabel]print("classCount[voteIlabel]:",classCount[voteIlabel])
print("classCount:",classCount)
# 排序
= sorted(classCount.items(),key=operator.itemgetter(1), reverse=True)
sortedClassCount return sortedClassCount[0][0]
测试算法:作为完整程序验证分类器
- 通常使用90%作为训练样本来训练分类器,使用其余的10%数据去测试分类器
def datingClassTest():
= 0.10
hoRatio = file2matrix("./dataset/datingTestSet2.txt")
datingDataMat ,datingLabels = autoNorm(datingDataMat)
normMat, ranges, minVals = normMat.shape[0]
m = int(m * hoRatio)
numTestVecs print(numTestVecs)
= 0.0
errorCount for i in range(numTestVecs):
= classify0(normMat[i,:], normMat[numTestVecs:m,:], datingLabels[numTestVecs:m],3)
classifierResult # print("最近邻的结果",classifierResult,"realanswer", datingLabels[i])
if (classifierResult != datingLabels[i]):
+=1.0
errorCount print("正确率",1.0-errorCount/float(numTestVecs))
datingClassTest()
1000
100
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {2: 1, 3: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2, 3: 1}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {3: 2, 1: 1}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {3: 1, 2: 1}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {3: 1, 2: 1, 1: 1}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {3: 1, 1: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2, 1: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {3: 1, 2: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2, 2: 1}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {3: 1, 2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {3: 1, 2: 2}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {1: 2, 3: 1}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {3: 1, 1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {3: 1, 1: 2}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {3: 1, 2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {3: 1, 2: 2}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {3: 2, 2: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {3: 2, 1: 1}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 2
classCount[voteIlabel]: 3
classCount: {2: 3}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {1: 2}
voteIlabel: 1
classCount[voteIlabel]: 3
classCount: {1: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {3: 2}
voteIlabel: 3
classCount[voteIlabel]: 3
classCount: {3: 3}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {2: 2, 3: 1}
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {2: 1, 1: 1}
voteIlabel: 1
classCount[voteIlabel]: 2
classCount: {2: 1, 1: 2}
voteIlabel: 1
classCount[voteIlabel]: 1
classCount: {1: 1}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {1: 1, 3: 1}
voteIlabel: 3
classCount[voteIlabel]: 2
classCount: {1: 1, 3: 2}
正确率 0.95
def classifyPerson():
=['not at all','in small doses','in large doses']
resultList= float(input("percentage of time spent playing video games?"))
percentTats = float(input("frequent flier miles per year?"))
ffMiles = float(input("liters of ice cream per week?"))
iceCream = file2matrix("./dataset/datingTestSet2.txt")
datingDataMat, datingLabels = autoNorm(datingDataMat)
normMat ,ranges, minVals
= np.array([ffMiles,percentTats,iceCream])
inArr= classify0((inArr-minVals)/ranges , normMat, datingLabels ,3)
classifierResult print("probably result:",resultList[classifierResult-1])
classifyPerson()
percentage of time spent playing video games? 12
frequent flier miles per year? 999
liters of ice cream per week? 2
1000
1000
voteIlabel 2
voteIlabel 3
voteIlabel 3
probably result: in large doses
classifyPerson()
percentage of time spent playing video games? 10
frequent flier miles per year? 10000
liters of ice cream per week? 0.5
1000
voteIlabel: 2
classCount[voteIlabel]: 1
classCount: {2: 1}
voteIlabel: 2
classCount[voteIlabel]: 2
classCount: {2: 2}
voteIlabel: 3
classCount[voteIlabel]: 1
classCount: {2: 2, 3: 1}
probably result: in small doses
About ME
👋 读书城南,🤔 在未来面前,我们都是孩子~
- 📙 一个热衷于探索学习新方向、新事物的智能产品经理,闲暇时间喜欢coding💻、画图🎨、音乐🎵、学习ing~
👋 Social Media
🛠️ Blog: http://oceaneyes.top
⚡ PM导航: https://pmhub.oceangzy.top
☘️ CNBLOG: https://www.cnblogs.com/oceaneyes-gzy/
🌱 AI PRJ自己部署的一些算法demo: http://ai.oceangzy.top/
📫 Email: 1450136519@qq.com
💬 WeChat: OCEANGZY
💬 公众号: UncleJoker-GZY
👋 加入小组~
👋 感谢打赏~
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 OCAEN.GZY读书城南!