机器学习实战ch04 关于python版本所支持的文本格式问题
函数定义中:
def spamTest():
docList=[]; classList = []; fullText =[]
for i in range(1,26):
# print('cycle counts is % i'%i)
wordList = textParse(open('email/spam/%d.txt' % i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(1)
wordList = textParse(open('email/ham/%d.txt' % i).read())
docList.append(wordList)
fullText.extend(wordList)
classList.append(0)
vocabList = createVocabList(docList)#create vocabulary
trainingSet = range(50); testSet=[] #create test set
for i in range(10):
randIndex = int(random.uniform(0,len(trainingSet)))
testSet.append(trainingSet[randIndex])
del(trainingSet[randIndex])
trainMat=[]; trainClasses = []
for docIndex in trainingSet:#train the classifier (get probs) trainNB0
trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex]))
trainClasses.append(classList[docIndex])
p0V,p1V,pSpam = trainNB0(array(trainMat),array(trainClasses))
errorCount = 0
for docIndex in testSet: #classify the remaining items
wordVector = bagOfWords2VecMN(vocabList, docList[docIndex])
if classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]:
errorCount += 1
print ("classification error",docList[docIndex])
print ('the error rate is: ',float(errorCount)/len(testSet))
#return vocabList,fullText
程序调试时出现两个错误:
(1)UnicodeDecodeError: 'utf8' codec can't decode ...........
解决办法:将spam和ham文件夹.txt文件用Sublime Text打开,Save with Encoding UTF-8
(2) 'range' object doesn't support item deletion
解决办法:将trainingSet = range(50)改为 trainingSet = list(range(50)). #python3.x range返回的是range对象,不返回数组对象解决方法
以上两个错误均由python版本差异引起
机器学习实战ch04 关于python版本所支持的文本格式问题的更多相关文章
- 机器学习实战__安装python环境
环境:win7 64位系统 第一步:安装python 1.下载python2.7.3 64位 msi 版本(这里选择了很多2.7的其他更高版本导致安装setuptools失败,也不知道是什么原因,暂时 ...
- 机器学习实战 logistic回归 python代码
# -*- coding: utf-8 -*- """ Created on Sun Aug 06 15:57:18 2017 @author: mdz "&q ...
- 阿里云 rds python sdk不支持python3处理
阿里云文档中心的python版本aliyun-python-sdk-rds不支持python3处理 问题:默认情况下文档中心的python版本只支持python2,不兼容python3版本 需要稍微修 ...
- 机器学习实战笔记(Python实现)-05-支持向量机(SVM)
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- 《机器学习实战》——k-近邻算法Python实现问题记录(转载)
py2.7 : <机器学习实战> k-近邻算法 11.19 更新完毕 原文链接 <机器学习实战>第二章k-近邻算法,自己实现时遇到的问题,以及解决方法.做个记录. 1.写一个k ...
- 机器学习实战笔记(Python实现)-01-K近邻算法(KNN)
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- 机器学习实战笔记(Python实现)-02-决策树
--------------------------------------------------------------------------------------- 本系列文章为<机器 ...
- python机器学习实战(三)
python机器学习实战(三) 版权声明:本文为博主原创文章,转载请指明转载地址 www.cnblogs.com/fydeblog/p/7277205.html 前言 这篇notebook是关于机器 ...
- 吴裕雄--天生自然python机器学习实战:K-NN算法约会网站好友喜好预测以及手写数字预测分类实验
实验设备与软件环境 硬件环境:内存ddr3 4G及以上的x86架构主机一部 系统环境:windows 软件环境:Anaconda2(64位),python3.5,jupyter 内核版本:window ...
随机推荐
- 在eclipse上集成安装阿里巴巴代码规约P3C插件
在eclipse上集成安装阿里巴巴代码规约P3C插件 参照网址: https://jingyan.baidu.com/article/2d5afd6923e78b85a3e28e5e.html 首先进 ...
- C16记技术服务支持
1.首页笔记:可以通过点击加号添加笔记的分类,还可以查看最近的10条笔记 2.图形:可以通过查看笔记的比例来分析自己最近的状况 3.快速添加笔记:点击添加笔记能够直接进行笔记 4.时间轴:能够通过文字 ...
- Some notes in Stanford CS106A(4)
1.x++ is a method , the return value is x. (post increment) ++x is also a method , the return value ...
- FlappyBirdWeb素材资源
https://pan.baidu.com/s/1UeNmhnmK4RInmfiEduPrAQ 先上网盘地址 https://github.com/JIANGYUJING1995/FlappyBird ...
- Visual C++ 6.0中if..else..的简单用法和基本格式
# include <stdio.h> int main (void) { float score; printf("请输入您的考试成绩:"); scanf(" ...
- tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案 ZT
错误代码如下: 严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException ...
- 田螺便利店—opencv3.4.5与vs2015配置并测试
参考博客:https://blog.csdn.net/sinat_39061823/article/details/80723096 遇到的问题:由于找不到 opencv_world345d.dll, ...
- Egret_简单的封装方法
////////////随机颜色***////////////////////////// private getRdmClr(): number { return (Math.floor(Math. ...
- SSH与MVC
MVC是一个框架模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.最典型的MVC就是JSP + servlet + j ...
- js: 文件(excel)下载处理
以前很少接触文件下载的功能,昨天和后台开发人员联调下载功能出现了问题,一开始我请求接口,返回二进制文件流乱码,在网上找了方法,可以解决.后面后台开发人员改了一下,返回文件地址,然后就解决了.所以我了解 ...