def classifyPerson():
resultList = ['not at all', 'in small doses', 'in large doses'] #定义结果列表表示3种结果
percentTats = float( input('percentage of time spent playing video games?') ) #输入特征1
ffMiles = float( input('frequent flier miles earned per year?') ) #输入特征2
iceCream = float( input('liters of ice cream consumed per year?') ) #输入特征3
datingDataMat, datingLabels = file2matrix('datingTestSet2.txt') #处理文件得到特征值矩阵和标签矩阵
normMat, ranges, minVals = autoNorm(datingDataMat) #归一化处理特征值矩阵
inArr = array( [ffMiles, percentTats, iceCream] ) #inArr为1*3的特征值矩阵
classifierResult = classify0( (inArr - minVals) / ranges, normMat, datingLabels, 3 ) #取距离最近的3个点按标签出现次数排序
print('You will probably like this person:', resultList[int(classifierResult) - 1]) #之前的文本数据标签值被虽然是数,但被处理后变成了str,文本标签值为1,2,3,对应resultList列表中的位置是0,1,2
classifyPerson()
学习要点:
python3中取消了raw_input,实际上Python3中的input等价于python2中的raw_input,而python2中的input功能在3中被取消了
3中的input性质:
输入的不管是数字还是字符,返回都是字符‘str’
>>> a = input('请输入')
请输入22
>>> type(a)
<class 'str'>