文章来源:http://pocore.com/blog/article_514.html
errorSum:0.0 总错误率
numTests:6 循环次数循环numTests次
k=0
读取训练数据
currLine: ['2.000000', '1.000000', '38.500000', '66.000000', '28.000000', '3.000000', '3.000000', '0.000000', '2.000000', '5.000000', '4.000000', '4.000000', '0.000000', '0.000000', '0.000000', '3.000000', '5.000000', '45.000000', '8.400000', '0.000000', '0.000000', '0.000000']
转成数据
lineArr: [2.0, 1.0, 38.5, 66.0, 28.0, 3.0, 3.0, 0.0, 2.0, 5.0, 4.0, 4.0, 0.0, 0.0, 0.0, 3.0, 5.0, 45.0, 8.4, 0.0, 0.0]
添加一行训练数据
trainingSet: [[2.0, 1.0, 38.5, 66.0, 28.0, 3.0, 3.0, 0.0, 2.0, 5.0, 4.0, 4.0, 0.0, 0.0, 0.0, 3.0, 5.0, 45.0, 8.4, 0.0, 0.0]]
添加一行训练标签
trainingLabels: [[0.0]]
最终得到
trainingSet: [[2.0, 1.0, 38.5, 66.0, 28.0, 3.0, 3.0, 0.0, 2.0, 5.0, 4.0, 4.0, 0.0, 0.0, 0.0, 3.0, 5.0, 45.0, 8.4, 0.0, 0.0], .....]
trainingLabels:
: : [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, ......]
循环500次
得到trainWeights
![Cinque Terre](https://i-blog.csdnimg.cn/blog_migrate/36cf4f1a6f1e9d63043ef63f9c931acb.png)
读取测试数据:
numTestVec:0.0 记录全部测试的记录数
lineArr: [2.0, 1.0, 38.5, 54.0, 20.0, 0.0, 1.0, 2.0, 2.0, 3.0, 4.0, 1.0, 2.0, 2.0, 5.9, 0.0, 2.0, 42.0, 6.3, 0.0, 0.0]
利用lineArr和上一步的trainWeights计算得到
prob=1.0
计算得到分类
if prob>0.5: return 1.0
else: return 0.0
判断分类是否正确
如果分类正确
errorCount=errorCount+1
利用errorCount=(errorCount)/numTestVec得到
errorRate=0.3582089552238806
计算全部循环的平均错误率
errorSum/float(numTests)
![Cinque Terre](https://i-blog.csdnimg.cn/blog_migrate/6251813816dcc9c8aeb7ff162943c135.png)