斯坦福大学机器学习作业题Problem Set #2 Spam classi cation下篇

               

最终得出最代表垃圾邮件的五个词为gun,moral,israel,jew,faith

将上一篇的main函数替换为这个

def main():
    trainMatrix, tokenlist, trainCategory = readMatrix('MATRIX.TRAIN')
    testMatrix, tokenlist, testCategory = readMatrix('MATRIX.TEST')
    state0, state1, proportion_state0, proportion_state1 = nb_train(trainMatrix,tokenlist,trainCategory)
    proportion_p1_p0=[]
    for i in range(len(state0)):
        proportion_p1_p0.append((state0[i]/state1[i]))
    largest_five=heapq.nlargest(5, proportion_p1_p0)
    location=[]
    for i in range(len(largest_five)):
        j=proportion_p1_p0.index(largest_five[i])
        location.append(j)
        print tokenlist[j]
    return



随着数据量的增大,误差不断减小

将上一篇的main函数替换为这个

def main():
    trainfile=['MATRIX.TRAIN.50','MATRIX.TRAIN.100','MATRIX.TRAIN.200','MATRIX.TRAIN.400','MATRIX.TRAIN.800','MATRIX.TRAIN.1400']
    error= np.zeros(len(trainfile))
    x=[50,100,200,400,800,1400]
    for i in range(len(trainfile)):
        trainMatrix, tokenlist, trainCategory = readMatrix(trainfile[i])
        testMatrix, tokenlist, testCategory = readMatrix('MATRIX.TEST')
        state0, state1, proportion_state0, proportion_state1 = nb_train(trainMatrix,tokenlist,trainCategory)
        output = nb_test(testMatrix,state0, state1, proportion_state0, proportion_state1)
        error[i]=evaluate(output, testCategory)
    plt.xlabel('Data quantity')
    plt.ylabel('Error')
    plt.plot(x,error)
    plt.show()
    return


svm的误差更小一些




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值