这个单子其实就是一个朴素贝叶斯实现多分类以及大批量数据用这个算法会导致内存崩,为了这个单子我还上网租服务器呢
import pandas as pd
import numpy as np
# 载入语料
raw = pd.read_csv("data/data.txt",
names = ['txt'], sep ='aaa', encoding ="utf-8" ,engine='python')
# 章节判断用变量预处理
def m_head(tmpstr):
return tmpstr[:1]
def m_mid(tmpstr):
return tmpstr.find("种类")
raw['head'] = raw.txt.apply(m_head)
raw['mid'] = raw.txt.apply(m_mid)
raw['len'] = raw.txt.apply(len)
# 章节判断
chapnum = 0
for i in range(len(raw)):
if raw['head'][i] == "第" and raw['mid'][i] > 0 and raw['len'][i] < 30 :
chapnum += 1
raw.loc[i, 'chap'] = chapnum
# 删除临时变量
del raw['head']
del raw['mid']
del raw['len']
raw
txt | chap | |
---|---|---|
0 | 第一种类 | 1.0 |
1 | 证券通:百联股份未来5年有能力保持高速增长 | 1.0 |
2 | 深度报告 权威内参 来自“证券通”www.KL178.com | 1.0 |
3 | 鉴于公司管理层更迭后的新气象,再融资开闸使公司重新拥有了跳跃式发展的可能,预计G百联(行情,... | 1.0 |
4 | 更多详情免费咨询021*64690729或登录WWW.KL178.COM(证券通),资深行业... | 1.0 |
5 | 本文版权为“证券通”独家拥有,任何单位和个人不得复制、转发以及用于业务经营,违者将追究其法律... | 1.0 |
6 | 深度报告 权威内参 来自“证券通”www.KL178.com | 1.0 |
7 | 今日热点:五一长假过后,人民币再度逼近8元关口,中间价达8.0090。 | 1.0 |
8 | 证券通认为,由于市场预期美联储将在今年6月暂停升息,导致了国际市场上美元走势疲软,从而推高了... | 1.0 |
9 | 更多详情免费咨询021*64690729或登录WWW.KL178.COM(证券通),资深行业... | 1.0 |
10 | 作者声明:在本机构、本人所知情的范围内,本机构、本人以及财产上的利害关系人与所述文章内容没有... | 1.0 |
11 | 深度报告 权威内参 来自“证券通”www.KL178.com | 1.0 |
12 | 今日热点:五一长假过后,人民币再度逼近8元关口,中间价达8.0090。 | 1.0 |
13 | 证券通认为,由于市场预期美联储将在今年6月暂停升息,导致了国际市场上美元走势疲软,从而推高了... | 1.0 |
14 | 更多详情免费咨询021*64690729或登录WWW.KL178.COM(证券通),资深行业... | 1.0 |
15 | 作者声明:在本机构、本人所知情的范围内,本机构、本人以及财产上的利害关系人与所述文章内容没有... | 1.0 |
16 | 深度报告 权威内参 来自“证券通”www.KL178.com | 1.0 |
17 | 今日热点:再融资第一单 G长电(行情,论坛)G申能(行情,论坛)有望拔头筹 | 1.0 |
18 | 昨日《上市公司证券发行管理办法》正式发布,市场对此做出明显的正面反映,再融资开闸后,谁将成为... | 1.0 |
19 | 更多详情免费咨询021*64690729或登录WWW.KL178.COM(证券通),资深行业... | 1.0 |
20 | 作者声明:在本机构、本人所知情的范围内,本机构、本人以及财产上的利害关系人与所述文章内容没有... | 1.0 |
21 | 深度报告 权威内参 来自“证券通”www.KL178.com | 1.0 |
22 | 今日热点:五一长假过后,人民币再度逼近8元关口,中间价达8.0090。 | 1.0 |
23 | 证券通认为,由于市场预期美联储将在今年6月暂停升息,导致了国际市场上美元走势疲软,从而推高了... | 1.0 |
24 | 更多详情免费咨询021*64690729或登录WWW.KL178.COM(证券通),资深行业... | 1.0 |
25 | 作者声明:在本机构、本人所知情的范围内,本机构、本人以及财产上的利害关系人与所述文章内容没有... | 1.0 |
26 | G上港(600018):机构争持的防御蓝筹品种 | 1.0 |
27 | 股指向上突破1500点之后,存在着一定的震荡反复现象,连续三日累计最大涨幅达到130点,使得... | 1.0 |
28 | 跻身于世界一流的集装箱码头 | 1.0 |
29 | 公司是国家512家重点扶持的特大型国有企业之一,与世界上46个国家和地区、145个港口建立了... | 1.0 |
... | ... | ... |
325672 | 战事的发展和国民党军的节节败退使得不少水兵从思想上开始动摇,军方的严厉压制和国内的民不聊生更... | 9.0 |
325673 | 为了掌握舰上的全部轻武器,以便控制住舰上的主要部位,制服可能遇到的反抗,毕重远找到了存放全部... | 9.0 |
325674 | 1949年2月初,传来军舰将前往青岛执行任务的风声,毕重远非常高兴,因为倘若在航行中起义,一... | 9.0 |
325675 | 谁料天不遂人愿,军舰刚出黄浦江口,便抛锚待命。不久即有消息称“重庆”号即将前往江阴,阻击共军... | 9.0 |
325676 | 形势刻不容缓,他们作出了按计划起义的抉择 | 9.0 |
325677 | 这次事件使毕重远感到越是临近起义越要谨慎从事。他一面向不可靠人员散布起义策划终止的消息,一面... | 9.0 |
325678 | 2月23日,副舰长突然离舰,前往南京海军部。舰上议论纷纷,说是舰方已闻起义风声。毕重远感到情... | 9.0 |
325679 | 就在他们忙碌之际,突然出现了一系列有利条件:舰方决定2月25日下午2点半移动锚位,而移动锚位... | 9.0 |
325680 | 2月24日晚,“解委会”在281室召开会议。与会人员只许进,不许出。起义者们依旧分歧严重。毕... | 9.0 |
325681 | 当时针指到1时15分之际,毕重远发出开始起义行动的信号。大家各司其职,快速行动起来。一个多小... | 9.0 |
325682 | 2月25日下午,突然发现一艘美国军舰,全舰进入一级战备 | 9.0 |
325683 | 军舰出吴淞口,起义者们都松了一口气。到了2月25日下午,哨兵报告发现一艘美国军舰。气氛顿时紧... | 9.0 |
325684 | 2月26日黎明是令人陶醉的黎明。烟台解放军炮兵发现,国民党军舰有人在挥动手绢。烟台警备区作训... | 9.0 |
325685 | 时任东北军区参谋长的伍修权将军在30多年后回忆往事道:“毕重远同志是我党在‘重庆’舰上的一粒... | 9.0 |
325686 | 孙京波 陈玮 | 9.0 |
325687 | “红蓝”对抗演练是部队训练的一种高级形式,是和平时期训练走向实战的有效手段。上个世纪八十年代... | 9.0 |
325688 | 实战化是“蓝军”建设之本 | 9.0 |
325689 | 目前各国军队“蓝军”建设坚持的一条最根本的原则,就是试图从那些近似实战的环境中培养一个逼真的... | 9.0 |
325690 | 现在有些对抗性演习,在“战之必胜”的思想指导下,有时存在将“蓝军”片面低能化的情况,而真实中... | 9.0 |
325691 | 专业化是“蓝军”生存之魂 | 9.0 |
325692 | 一支合格的“蓝军”,首先应该是一支专业化的“蓝军”。美军第117装甲旅就曾经是针对苏联而设计... | 9.0 |
325693 | 现在有些单位虽然也建立了“蓝军”,但仅仅把“蓝军”建设当成临时性任务,甚至有的“蓝军”以己度... | 9.0 |
325694 | 针对性是“蓝军”发展之基 | 9.0 |
325695 | 一支“蓝军”的建立总是要瞄准某一潜在对手,对手发生变化,“蓝军”扮演的角色也要随之改变。19... | 9.0 |
325696 | 我军的“蓝军”出现在训练场之时,正值冷战期间,“蓝军”模拟的对象有一定的历史局限性。随着新军... | 9.0 |
325697 | 新华网莫斯科3月15日电 (记者张金海) | 9.0 |
325698 | 俄罗斯空军新闻局局长德罗贝舍夫斯基15日说,俄空军一架苏-24M前线轰炸机当天在飞行训练时失... | 9.0 |
325699 | 德罗贝舍夫斯基对媒体说,这架轰炸机是在俄西南部的沃罗涅日市东南56公里处坠毁的。飞机失事没有... | 9.0 |
325700 | 德罗贝舍夫斯基说,米哈伊洛夫已下令,在轰炸机失事原因查清之前,同一型号的飞机暂停起飞。据初步... | 9.0 |
325701 | 苏-24M前线轰炸机1985年装备部队。该型号飞机最大起飞重量约为40吨,最大飞行时速达23... | 9.0 |
325702 rows × 2 columns
#从原始语料df中提取出所需的前两章段落
raw12 = raw[raw.chap.isin([1,2,3,4,5,6,7,8,9])]
# def function(x):
# import re
# #停用词表、词典路径
# stop_words_list_path = "stop_words_list.txt"
# words_list_path = "words_list.txt"
# def load_data(stop_words_list_path, words_list_path):
# '''
# 加载数据文件
# '''
# stop_words_list = [] # 停用词表
# words_list = [] # 词典
# with open(stop_words_list_path, 'r', encoding='gb18030') as f_s:
# lines = f_s.readlines()
# for line in lines:
# word = line.strip().split()
# stop_words_list.extend(word)
# with open(words_list_path, 'r', encoding='gb18030') as f_w:
# lines = f_w.readlines()
# for line in lines:
# word = line.strip().split()
# words_list.extend(word)
# return stop_words_list, words_list
# class SegmentSentence(object):
# def __init__(self, stop_words_list, words_list, sentence):
# '''
# stoplist 停用词表
# words_list 词典
# maxLen 最大步长
# '''
# self.stop_words_list = stop_words_list
# self.words_list = words_list
# self.sentence = sentence
# self.maxLen = self.get_maxLen()
# def get_maxLen(self):
# '''
# 遍历词典,找出单个词语的最大长度maxLen
# '''
# count = []
# for word in self.words_list:
# count.append(len(word))
# return max(count)
# def get_num_singlewords(self, words):
# '''
# 遍历分词结果,得到单字的数量
# '''
# num = 0
# for word in words:
# if len(word)==1:
# num += 1
# return num
# def output_file(self, result, filename):
# '''
# 将结果写入文件,词语之间用“/”分隔
# '''
# _filename = str('./' + filename)
# with open(_filename, 'w') as fw:
# for words in result:
# for word in words:
# fw.write(word)
# fw.write('/')
# return
# def forward_max_matching(self):
# '''
# 正向最大匹配法
# '''
# words = []
# start, end= 0, 0 #指针位于左边第一个字符
# length_sentence = len(self.sentence)
# while start < length_sentence: # 第二层循环,每一次循环都是一个end-start长度的窗口
# if end+self.maxLen <= length_sentence:
# end = start + self.maxLen # 若语料长度比词典中的单个词语长度长,则窗口大小为maxLen长度
# else:
# end = length_sentence # 若语料长度比词典中的单个词语长度短,则窗口大小为语料长度
# i = end
# while i > start: # 第一层循环,用一个标记i,对窗口进行从后往前的遍历
# word = self.sentence[start:i] # 每一次循环都将start-i长度的短句与词典匹配,看是否为词语
# if word in self.words_list:
# if word not in self.stop_words_list:
# words.append(word)
# start = i
# break
# if word in self.stop_words_list:
# start = i
# break
# if i==start+1: # 当短句长度为1(即单字)时,判断是否在词典内,若在,加入分词结果;若不在,直接丢掉。
# if word in self.words_list:
# words.append(word)
# start = i
# break
# else:
# start += 1
# break
# i -= 1
# end = end + self.maxLen
# self.output_file(words, "result_forward_max_matching.txt") # 将结果写入result_forward_max_matching.txt
# return words
# def backward_max_matching(self):
# '''
# 逆向最大匹配法
# '''
# words = []
# start, end= len(self.sentence), len(self.sentence)#指针位于最后一个字符,从整个实验文本的最后一个字符开始
# while start > 0:
# if end - self.maxLen >= 0: # 若语料长度比maxLen长度长,则窗口大小为maxLen长度
# end = start - self.maxLen # 若语料长度比maxLen长度短,则窗口大小为语料长度
# else:
# end = 0
# i = end
# while i < start: # 第一层循环,用一个标记i,对窗口进行从前往后的遍历
# word = self.sentence[i:start] # 每一次循环都将i-start长度的短句与词典匹配,看是否为词语
# if word in self.words_list:
# if word not in self.stop_words_list:
# words.append(word)
# start = i
# break
# if word in self.stop_words_list:
# start -= 1
# break
# if i==start-1: # 当短句长度为1(即单字)时,判断是否在词典内,若在,加入分词结果;若不在,直接丢掉。
# if word in self.words_list:
# words.append(word)
# start = i
# break
# else:
# start -= 1
# break
# i += 1
# end = end - self.maxLen
# self.output_file(words, "result_backward_max_matching.txt") # 将分词结果写入result_backward_max_matching.txt
# return words
# def bi_max_matching(self):
# '''
# 双向最大匹配法
# '''
# result, re_forward, re_backward= [], [], []
# re_forward = self.forward_max_matching()
# re_backward = self.backward_max_matching()
# if len(re_forward) < len(re_backward): # 比较正向、逆向最大匹配法结果的词数,选择分词数量少的那个
# result = re_forward
# elif len(re_backward) < len(re_forward):
# result = re_backward
# else:
# if re_forward == re_backward: # 当分词数量相等时,a.若分词结果相同,则返回任意一个(这里返回的是正向结果)
# result = re_forward
# else: # 当分词数量相等时,b.若分词结果不相同,则返回单字数量较少的那一个
# num_forward = self.get_num_singlewords(re_forward) # 统计正向算法分词结果中的单字数量
# num_backward = self.get_num_singlewords(re_backward) # 统计逆向算法分词结果中的单字数量
# if num_forward < num_backward:
# result = re_forward
# else:
# result = re_backward
# self.output_file(result, "result_bi_max_matching.txt") # 将分词结果写入result_bi_max_matching.txt
# return result, re_forward, re_backward
# if __name__ == "__main__":
# stop_words_list, words_list = load_data(stop_words_list_path, words_list_path)
# sentence = x
# ss = SegmentSentence(stop_words_list, words_list, sentence)
# result_bi, result_forward, result_backward = ss.bi_max_matching()
# print(result_bi)
# return result_bi
import jieba
dict = 'SegDict.TXT'
jieba.load_userdict(dict) # dict为自定义词典的路径
tmpdf = pd.read_csv('停用词.txt',
names = ['w'], sep = 'aaa', encoding = 'utf-8',engine='python')
def function(a):
word_list=[w for w in jieba.cut(a) if w not in list(tmpdf.w)]
return word_list
raw12['cleantxt'] = raw12.apply(lambda x: function(x['txt']), axis = 1)
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\ADMINI~1\AppData\Local\Temp\jieba.cache
Loading model cost 1.182 seconds.
Prefix dict has been built succesfully.
raw12ana = raw12.iloc[list(raw12.txt.apply(len) > 10), :] # 只使用超过10字的段落
raw12ana.reset_index(drop = True, inplace = True)
print(len(raw12ana))
raw12ana.head()
263025
txt | chap | cleantxt | |
---|---|---|---|
0 | 证券通:百联股份未来5年有能力保持高速增长 | 1.0 | [证券, 通, 百联, 股份, 未来, 5, 年有, 能力, 保持高速, 增长] |
1 | 深度报告 权威内参 来自“证券通”www.KL178.com | 1.0 | [深度, 报告, , 权威, 内参, , 证券, 通, www, KL178] |
2 | 鉴于公司管理层更迭后的新气象,再融资开闸使公司重新拥有了跳跃式发展的可能,预计G百联(行情,... | 1.0 | [公司, 管理层, 更迭, 新气象, 再融资, 开闸, 公司, 拥有, 跳跃式, 发展, 预... |
3 | 更多详情免费咨询021*64690729或登录WWW.KL178.COM(证券通),资深行业... | 1.0 | [更多, 详情, 免费, 咨询, 021, *, 64690729, 登录, WWW, KL... |
4 | 本文版权为“证券通”独家拥有,任何单位和个人不得复制、转发以及用于业务经营,违者将追究其法律... | 1.0 | [本文, 版权, 证券, 通, 独家, 拥有, 单位, 复制, 转发, 用于, 业务, 经营... |
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(raw12ana.cleantxt,raw12ana.chap)
from numpy import *
# '''
# 贝叶斯公式 p(ci|w) = p(w|ci)*p(ci) / p(w)
# 即比较两类别分子大小,把结果归为分子大的一类
# p(w|ci)条件概率,即在类别1或0下,w(词频)出现的概率(词频/此类别总词数即n/N)
# '''
# 取得DataSet中不重复的word
def createVocabList(dataSet):
vocabSet = set([])#使用set创建不重复词表库
for document in dataSet:
vocabSet = vocabSet | set(document) #创建两个集合的并集
return list(vocabSet)
# '''
# 我们将每个词的出现与否作为一个特征,这可以被描述为词集模型(set-of-words model)。
# 在词集中,每个词只能出现一次。
# '''
def setOfWords2Vec(vocabList, inputSet):
returnVec = [0]*len(vocabList)#创建一个所包含元素都为0的向量
#遍历文档中的所有单词,如果出现了词汇表中的单词,则将输出的文档向量中的对应值设为1
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] = 1
else: print("the word: %s is not in my Vocabulary!" % word)
return returnVec
# '''
# 如果一个词在文档中出现不止一次,这可能意味着包含该词是否出现在文档中所不能表达的某种信息,
# 这种方法被称为词袋模型(bag-of-words model)。
# 在词袋中,每个单词可以出现多次。
# 为适应词袋模型,需要对函数setOfWords2Vec稍加修改,修改后的函数称为bagOfWords2VecMN
# '''
def bagOfWords2Vec(vocabList, inputSet):
returnVec = [0]*len(vocabList)
for word in inputSet:
if word in vocabList:
returnVec[vocabList.index(word)] += 1
return returnVec
def countX(aList,el):
count = 0
for item in aList:
if item == el:
count += 1
return count
def trainNB0(trainMatrix,trainCategory):
# trainMatrix:文档矩阵
# trainCategory:每篇文档类别标签
# '''rix,trainCategory):
#
numTrainDocs = len(trainMatrix)
numWords = len(trainMatrix[0])
pAbusive0 = countX(trainCategory,0) / float(numTrainDocs)
pAbusive1 = countX(trainCategory,1) / float(numTrainDocs)
pAbusive2 = countX(trainCategory,2) / float(numTrainDocs)
pAbusive3 = countX(trainCategory,3) / float(numTrainDocs)
pAbusive4 = countX(trainCategory,4) / float(numTrainDocs)
pAbusive5 = countX(trainCategory,5) / float(numTrainDocs)
pAbusive6 = countX(trainCategory,6) / float(numTrainDocs)
pAbusive7 = countX(trainCategory,7) / float(numTrainDocs)
pAbusive8 = countX(trainCategory,8) / float(numTrainDocs)
#初始化所有词出现数为1,并将分母初始化为2,避免某一个概率值为0
p0Num = ones(numWords)
p1Num = ones(numWords)
p2Num = ones(numWords)
p3Num = ones(numWords)
p4Num = ones(numWords)
p5Num = ones(numWords)
p6Num = ones(numWords)
p7Num = ones(numWords)
p8Num = ones(numWords)
p0Denom = 2.0; p1Denom = 2.0 ;p2Denom = 2.0
p3Denom = 2.0; p4Denom = 2.0;p5Denom = 2.0;p6Denom = 2.0;p7Denom = 2.0;p8Denom = 2.0
for i in range(numTrainDocs):
if trainCategory[i] == 1:
p1Num += trainMatrix[i]
p1Denom += sum(trainMatrix[i])
if trainCategory[i] == 2:
p2Num += trainMatrix[i]
p2Denom += sum(trainMatrix[i])
if trainCategory[i] == 3:
p3Num += trainMatrix[i]
p3Denom += sum(trainMatrix[i])
if trainCategory[i] == 4:
p4Num += trainMatrix[i]
p4Denom += sum(trainMatrix[i])
if trainCategory[i] == 0:
p0Num += trainMatrix[i]
p0Denom += sum(trainMatrix[i])
if trainCategory[i] == 5:
p5Num += trainMatrix[i]
p5Denom += sum(trainMatrix[i])
if trainCategory[i] == 6:
p6Num += trainMatrix[i]
p6Denom += sum(trainMatrix[i])
if trainCategory[i] == 7:
p7Num += trainMatrix[i]
p7Denom += sum(trainMatrix[i])
if trainCategory[i] ==8:
p8Num += trainMatrix[i]
p8Denom += sum(trainMatrix[i])
p4Vect = log(p4Num/p4Denom)#将结果取自然对数,避免下溢出,即太多很小的数相乘造成的影响
p3Vect = log(p3Num/p3Denom)
p2Vect = log(p2Num/p2Denom)
p1Vect = log(p1Num/p1Denom)#change to log()
p0Vect = log(p0Num/p0Denom)#change to log()
p5Vect = log(p1Num/p5Denom)#change to log()
p6Vect = log(p0Num/p6Denom)#change to log()
p7Vect = log(p1Num/p7Denom)#change to log()
p8Vect = log(p0Num/p8Denom)#change to log()
return p0Vect,p1Vect,p2Vect,p3Vect,p4Vect,p5Vect,p6Vect,p7Vect,p8Vect,pAbusive0,pAbusive1,pAbusive2,pAbusive3,pAbusive4,pAbusive5,pAbusive6,pAbusive7,pAbusive8
def classifyNB(vec2Classify,p0Vec,p1Vec,p2Vec,p3Vec,p4Vec,p5Vec,p6Vec,p7Vec,p8Vec,pClass0,pClass1,pClass2,pClass3,pClass4,pClass5,pClass6,pClass7,pClass8):
p1 = sum(vec2Classify * p1Vec) + log(pClass1)
p2 = sum(vec2Classify * p2Vec) + log(pClass2)
p3 = sum(vec2Classify * p3Vec) + log(pClass3)
p4 = sum(vec2Classify * p4Vec) + log(pClass4)
p0 = sum(vec2Classify * p0Vec) + log(pClass0)## print(p0,p1,p2,p3,p4)
p5 = sum(vec2Classify * p5Vec) + log(pClass5)
p6 = sum(vec2Classify * p6Vec) + log(pClass6)
p7 = sum(vec2Classify * p7Vec) + log(pClass7)
p8 = sum(vec2Classify * p8Vec) + log(pClass8)
return [p0,p1,p2,p3,p4,p5,p6,p7,p8].index(max([p0,p1,p2,p3,p4,p5,p6,p7,p8]))
result=[]
if __name__ == "__main__":
dataset=x_train[0:]
classVec=y_train[0:]
# 0,1,2,3,4分别表示不同类别
# classVec = [0,1,0,1,0,1,2,2,4,2,4,3,3,3]
print("正在创建词频列表")
myVocabList = createVocabList(dataset)
print("正在建词向量")
trainMat = []
for postinDoc in dataset:
trainMat.append(setOfWords2Vec(myVocabList,postinDoc))
print("开始训练")
p0V,p1V,p2V,p3V,p4V,p5V,p6V,p7V,p8V,pAb0,pAb1,pAb2,pAb3,pAb4,pAb5,pAb6,pAb7,pAb8 = trainNB0(array(trainMat),array(classVec))
# 输入的测试案例
for i in x_test:
tmp = i
thisDoc = array(setOfWords2Vec(myVocabList,tmp))
flag = classifyNB(thisDoc,p0V,p1V,p2V,p3V,p4V,p5V,p6V,p7V,p8V,pAb0,pAb1,pAb2,pAb3,pAb4,pAb5,pAb6,pAb7,pAb8)
print('flag is',flag)
result.append(flag)
正在创建词频列表
正在建词向量
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-9-9ebb53927434> in <module>()
129 trainMat = []
130 for postinDoc in dataset:
--> 131 trainMat.append(setOfWords2Vec(myVocabList,postinDoc))
132 print("开始训练")
133 p0V,p1V,p2V,p3V,p4V,p5V,p6V,p7V,p8V,pAb0,pAb1,pAb2,pAb3,pAb4,pAb5,pAb6,pAb7,pAb8 = trainNB0(array(trainMat),array(classVec))
<ipython-input-9-9ebb53927434> in setOfWords2Vec(vocabList, inputSet)
16 # '''
17 def setOfWords2Vec(vocabList, inputSet):
---> 18 returnVec = [0]*len(vocabList)#创建一个所包含元素都为0的向量
19 #遍历文档中的所有单词,如果出现了词汇表中的单词,则将输出的文档向量中的对应值设为1
20 for word in inputSet:
MemoryError:
final=[]
for i in y_test:
final.append(i)
# 分类报告:precision/recall/fi-score/均值/分类个数
from sklearn.metrics import classification_report
class_true = final #正确的分类结果
class_pred = result #实际的分类结果'
print('-------------训练集测试结果-------------------')
target_names = ['class 1', 'class 2','class3','class4','class5','class6','class7','class8','class9']
print(classification_report(class_true, class_pred, target_names=target_names))