函数定义中:

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版本所支持的文本格式问题的更多相关文章

  1. 机器学习实战__安装python环境

    环境:win7 64位系统 第一步:安装python 1.下载python2.7.3 64位 msi 版本(这里选择了很多2.7的其他更高版本导致安装setuptools失败,也不知道是什么原因,暂时 ...

  2. 机器学习实战 logistic回归 python代码

    # -*- coding: utf-8 -*- """ Created on Sun Aug 06 15:57:18 2017 @author: mdz "&q ...

  3. 阿里云 rds python sdk不支持python3处理

    阿里云文档中心的python版本aliyun-python-sdk-rds不支持python3处理 问题:默认情况下文档中心的python版本只支持python2,不兼容python3版本 需要稍微修 ...

  4. 机器学习实战笔记(Python实现)-05-支持向量机(SVM)

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  5. 《机器学习实战》——k-近邻算法Python实现问题记录(转载)

    py2.7 : <机器学习实战> k-近邻算法 11.19 更新完毕 原文链接 <机器学习实战>第二章k-近邻算法,自己实现时遇到的问题,以及解决方法.做个记录. 1.写一个k ...

  6. 机器学习实战笔记(Python实现)-01-K近邻算法(KNN)

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  7. 机器学习实战笔记(Python实现)-02-决策树

    --------------------------------------------------------------------------------------- 本系列文章为<机器 ...

  8. python机器学习实战(三)

    python机器学习实战(三) 版权声明:本文为博主原创文章,转载请指明转载地址 www.cnblogs.com/fydeblog/p/7277205.html  前言 这篇notebook是关于机器 ...

  9. 吴裕雄--天生自然python机器学习实战:K-NN算法约会网站好友喜好预测以及手写数字预测分类实验

    实验设备与软件环境 硬件环境:内存ddr3 4G及以上的x86架构主机一部 系统环境:windows 软件环境:Anaconda2(64位),python3.5,jupyter 内核版本:window ...

随机推荐

  1. 在eclipse上集成安装阿里巴巴代码规约P3C插件

    在eclipse上集成安装阿里巴巴代码规约P3C插件 参照网址: https://jingyan.baidu.com/article/2d5afd6923e78b85a3e28e5e.html 首先进 ...

  2. vue-router 学习

    Vue.js的一大特色就是构建单页面应用十分方便,既然要方便构建单页面应用那么自然少不了路由,vue-router就是vue官方提供的一个路由框架.总体来说,vue-router设计得简单好用,下面就 ...

  3. Openflow协议详解

    http://www.h3c.com/cn/d_201811/1131080_30005_0.htm# 1 OpenFlow背景 转发和控制分离是SDN网络的本质特点之一 .在SDN网络架构中,控制平 ...

  4. 微信小程序 - app.json配置解析

    { "pages": [ "pages/index/index", "pages/logs/logs", "pages/login ...

  5. 小程序视频播放组件video

    最近在做一个视频播放的功能,要求如下: 1.实现视频的全屏播放: 2.实现视频相关信息的展示: 3.实现视频滑动上下切换效果: 肯定选择用原生组件video了,真是不用不知道,一用都是坑: 首先,组件 ...

  6. 分享一个好用的tmux配置文件

    tmux众所周知,不过多介绍,友好的tmux配置,让人用起来很舒服,分享一个tmux配置文件 # ------ general ------------------------------------ ...

  7. 利用 SPICE 分析理解心电图前端中的右腿驱动

      [导读] 心电图(ECG)学是一门将心脏离子去极(ionic depolarization) 后转换为分析用可测量电信号的科学.模拟电子接口到电极/患者设计中最为常见的难题之一便是优化右腿驱动 ( ...

  8. scrapy学习笔记之hello world

    1. 创建项目文档 在目标路径下,打开命令行,使用如下命令创建项目,例如项目名称为 "tutorial": scrapy startproject tutorial - 创建项目时 ...

  9. height属性

    高度属性: height:长度值|百分比|auto 最大高度:max-height 最小高度:min-height 说明:设置块级元素和替换元素的内容高度.

  10. 什么是C/S模式与B/S模式,两者区别与优缺点

    转自https://wenwen.sogou.com/z/q1709598292.htm C/S (Client/Server,客户机/服务器)模式又称C/S结构,是软件系统体系结构的一种.C/S模式 ...