K近邻改进约会网站(五):使用算法进行预测

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'>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值