机器学习实战之朴素贝叶斯

问题1
来源:使用朴素贝叶斯过滤垃圾邮件
描述:spamTest()和textParse()读文件时编译通不过
报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
      TypeError: cannot use a string pattern on a bytes-like object
解决办法:1、将textParse()函数中listOfTokens = re.split(r'\w*', bigString)改成
      regEx = re.compile('\\w*')
    bigString = bigString.decode('GBK', 'ignore')
    listOfTokens = regEx.split(bigString)
      2、将spamTest()函数中wordList = textParse(open('spam/%d.txt' % i).read())和wordList = textParse(open('ham/%d.txt' % i).read())改成
    wordList = textParse(open('spam/%d.txt' % i, 'rb').read())
    wordList = textParse(open('ham/%d.txt' % i, 'rb').read())
原因:①python2转python3 ②读的文件中出现了无法编码的字符,需使用ignore属性进行忽略

问题2
来源:使用朴素贝叶

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值