函数定义中:

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. nginx 学习(一)

    今天不会nginx被怼了一顿.我必然不能忍,所以就赶忙来补充一下nginx知识!! nginx简介 nginx是一款高性能的http服务器,目前国内包括BAT在内的众多互联网企业均采用其作为反向代理服 ...

  2. c#线程池ThreadPool实例详解

    1. 如何查看线程池的最大线程数和最小线程数 static void Main(string[] args) { Console.WriteLine("----------线程池开始,线程I ...

  3. 通过SSH去连接 github 和bitbucket

    github 和 bitbucket 都是项目托管服务器, 1 创建SSH private key and public key 首先需要安装git命令, 并且请检查是否有ssh 命令. 打开 Git ...

  4. Strut2第一章

    一.Struts2的执行流程: 用户提交一个请求,服务器接收,交给Struts2的核心过滤器进行处理,Struts2的过滤器调用Struts2的一系列处理器来处理(如:解析struts.xml配置文件 ...

  5. bootstrap-edittable 使用笔记之 (select, data,text, number)

    可编辑列表的数据格式可以指定,常用的有select, data, text, number.代码如下. 前端代码: <table id="tb_product" class= ...

  6. PostgreSQL注入基础

    一般注入多用于在mssql和mysql两类数据库中,如mssql+asp.mysql+php则是最为常见的搭配环境.不同的网站应用的数据库也大不一样,根据数据库的处理能力.负载等多重因素决定.本文主要 ...

  7. 大数据 - hadoop三台linux虚拟服务器 - 初始化部署

    搭建hadoop环境 1.解压Hadoop的安装包,解压到modules文件夹中.(安装包下载地址:http://archive.apache.org/dist/hadoop/core/hadoop- ...

  8. AVL Tree Deletion

    Overview 知识点: 1. delete函数的signature public AVLTreeNode Delete(AVLTreeNode node, int key) 2. 算法,如何删除节 ...

  9. Visual C++ 6.0中if语句的常见问题

    # include <stdio.h> int main (void) { > )//如果在第四行加分号的话,编译的时候就会在第六行出错 printf("你好\n" ...

  10. [Java Web学习]JSP中uri="http://java.sun.com/jsp/jstl/core"报红错误

    在官网下载jstl.jar和standard.jar,问题解决.