from pandas import read_csv
import numpy as np
from sklearn.datasets.base import Bunch
import pickle #导入cPickle包并且取一个别名pickle #持久化类
from sklearn.feature_extraction.text import TfidfVectorizer
import jieba
import xlwt
import operator#排序用
from sklearn import metrics Straindata=[]
Strainlabel=[]
Sart_train=[] Stestdata=[]
Stestlabel=[]
Sart_test=[] Slast=[]
Snew=[] class obj:
def __init__(self):
self.key=0
self.weight=0.0 def importSmallContentdata(file,data,art,label,f):
dataset=read_csv(file)
Sdata = dataset.values[:,:]
print(type(Sdata)) if f==1:
for line in Sdata:
ls=[]
ls.append(line[14])
ls.append(line[15])
ls.append(line[16])
ls.append(line[17])
Slast.append(ls)
#print(len(Slast))
#print("需要对照的小类数据准备完毕") '''找到smalli不为0的装入Straindata,把数据分开'''
for smalli in range(14,18):
#print(smalli)
count=0
for line in Sdata:
count=count+1
if line[smalli]!='' and line[smalli]!=0 :
k=1
ls=[]
for i in line:
if k==1:
art.append(i)
k=k+1
continue
if k==11:#k为14并不代表是line[14],因为line是从0开始
break
ls.append(float(i))
k=k+1
data.append(ls)
label.append(line[smalli])
if f==1:
Snew.append(count) #print("为什么都超限",len(Snew)) def getKvector(train_set,vec,n):
nonzero=train_set.tdm.nonzero()
k=0
lis=[]
gather=[]
p=-1
for i in nonzero[0]:
p=p+1
if k==i:
a=obj()
a.key=nonzero[1][p]
a.weight=train_set.tdm[i,nonzero[1][p]]
lis.append(a)
else:
lis.sort(key=lambda obj: obj.weight, reverse=True)#对链表内为类对象的排序
gather.append(lis)
while k < i:
k=k+1
lis=[]
a=obj()
a.key=nonzero[1][p]
a.weight=train_set.tdm[i,nonzero[1][p]]
lis.append(a)
gather.append(lis)#gather存储的是每条数据的事实描述的特征向量,已经从小到大排好了,只不过每个存既有key又有weight #我们只要key,不再需要weight sj=1
for i in gather:
ls=[]
for j in i:
sj=sj+1
ls.append(float(j.key))
while sj<=n:
sj=sj+1
ls.append(-1)
sj=1
vec.append(ls) '''读取停用词'''
def _readfile(path):
with open(path, "rb") as fp:
content = fp.read()
return content ''' 读取bunch对象'''
def _readbunchobj(path):
with open(path, "rb") as file_obj:
bunch = pickle.load(file_obj)
return bunch '''写入bunch对象'''
def _writebunchobj(path, bunchobj):
with open(path, "wb") as file_obj:
pickle.dump(bunchobj, file_obj) def buildtrainbunch(bunch_path,art_train,trainlabel):
bunch = Bunch(label=[],contents=[])
for item1 in trainlabel:
bunch.label.append(item1) #trainContentdatasave=[] #存储所有训练和测试数据的分词
for item2 in art_train:
item2=str(item2)
item2 = item2.replace("\r\n", "")
item2 = item2.replace(" ", "")
content_seg=jieba.cut(item2)
save2=''
for item3 in content_seg:
if len(item3) > 1 and item3!='\r\n':
#trainContentdatasave.append(item3)
save2=save2+","+item3
bunch.contents.append(save2)
with open(bunch_path, "wb") as file_obj:
pickle.dump(bunch, file_obj)
print("构建训练数据文本对象结束!!!") def buildtestbunch(bunch_path,art_test,testlabel):
bunch = Bunch(label=[],contents=[])
for item1 in testlabel:
bunch.label.append(item1) #testContentdatasave=[] #存储所有训练和测试数据的分词
for item2 in art_test:
item2=str(item2)
item2 = item2.replace("\r\n", "")
item2 = item2.replace(" ", "")
content_seg=jieba.cut(item2)
save2=''
for item3 in content_seg:
if len(item3) > 1 and item3!='\r\n':
#testContentdatasave.append(item3)
save2=save2+","+item3
bunch.contents.append(save2)
with open(bunch_path, "wb") as file_obj:
pickle.dump(bunch, file_obj)
print("构建测试数据文本对象结束!!!")
def vector_space(stopword_path,bunch_path,space_path): stpwrdlst = _readfile(stopword_path).splitlines()#读取停用词
bunch = _readbunchobj(bunch_path)#导入分词后的词向量bunch对象
#构建tf-idf词向量空间对象
tfidfspace = Bunch(label=bunch.label,tdm=[], vocabulary={}) #权重矩阵tdm,其中,权重矩阵是一个二维矩阵,tdm[i][j]表示,第j个词(即词典中的序号)在第i个类别中的IF-IDF值 #使用TfidVectorizer初始化向量空间模型
vectorizer = TfidfVectorizer(stop_words=stpwrdlst, sublinear_tf=True, max_df=0.5, min_df=0.0001,use_idf=True,max_features=15000)
#print(vectorizer)
#文本转为词频矩阵,单独保存字典文件
tfidfspace.tdm = vectorizer.fit_transform(bunch.contents)
tfidfspace.vocabulary = vectorizer.vocabulary_
#创建词袋的持久化
_writebunchobj(space_path, tfidfspace)
print("if-idf词向量空间实例创建成功!!!") def testvector_space(stopword_path,bunch_path,space_path,train_tfidf_path): stpwrdlst = _readfile(stopword_path).splitlines()#把停用词变成列表
bunch = _readbunchobj(bunch_path)
tfidfspace = Bunch(label=bunch.label,tdm=[], vocabulary={})
#导入训练集的TF-IDF词向量空间 ★★
trainbunch = _readbunchobj(train_tfidf_path)
tfidfspace.vocabulary = trainbunch.vocabulary vectorizer = TfidfVectorizer(stop_words=stpwrdlst, sublinear_tf=True, max_df=0.7, vocabulary=trainbunch.vocabulary, min_df=0.001) tfidfspace.tdm = vectorizer.fit_transform(bunch.contents)
_writebunchobj(space_path, tfidfspace)
print("if-idf词向量空间实例创建成功!!!") if __name__=="__main__": '''============================先导入数据=================================='''
file_train = 'F:/goverment/exceloperating/all_tocai_train.csv'
file_test = 'F:/goverment/exceloperating/all_tocai_test.csv' importSmallContentdata(file_train,Straindata,Sart_train,Strainlabel,0)
importSmallContentdata(file_test,Stestdata,Sart_test,Stestlabel,1) #print("Stestlabel" ,len(Stestlabel)) #print("小类导入数据完毕") #print("大类标签导入完毕")#共1329*4 '''==========================================================tf-idf对Bar进行文本特征提取============================================================================'''
#导入分词后的词向量bunch对象
train_bunch_path ="F:/goverment/exceloperating/trainbunch.bat"#Bunch保存路径
train_space_path = "F:/goverment/exceloperating/traintfdifspace.dat"
test_bunch_path ="F:/goverment/exceloperating/testbunch.bat"
test_space_path = "F:/goverment/exceloperating/testtfdifspace.dat"
stopword_path ="F:/goverment/exceloperating/hlt_stop_words.txt" '''============================================================tf-idf对Sart进行文本特征提取=============================================================================='''
buildtrainbunch(train_bunch_path,Sart_train,Strainlabel)
buildtestbunch(test_bunch_path,Sart_test,Stestlabel) vector_space(stopword_path,train_bunch_path,train_space_path)
testvector_space(stopword_path,test_bunch_path,test_space_path,train_space_path) train_set=_readbunchobj(train_space_path)
test_set=_readbunchobj(test_space_path) '''训练数据''' S_vec_train=[]
getKvector(train_set,S_vec_train,76) '''测试数据''' S_vec_test=[]
getKvector(test_set,S_vec_test,76) '''=================将得到的61个特征和之前的其它特征合并Btraindata==================''' '''小类训练数据'''
S_vec_train=np.array(S_vec_train)
#print(type(S_vec_train))
#print(S_vec_train.shape)
Straindata=np.array(Straindata)
#print(type(Straindata))
#print(Straindata.shape)
Straindata=np.hstack((S_vec_train,Straindata))
#print(Straindata) '''小类测试数据'''
S_vec_test=np.array(S_vec_test)
Stestdata=np.array(Stestdata)
Stestdata=np.hstack((S_vec_test,Stestdata)) print("分类算小类精度")
Strainlabel=np.array(Strainlabel)
Strainlabel=np.array(Strainlabel) from xgboost import XGBClassifier
clf= XGBClassifier(learning_rate =0.1,
n_estimators=1150,
max_depth=2,
min_child_weight=1,
gamma=0,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread=4,#没用
scale_pos_weight=1,#没用
seed=27)
clf.fit(Straindata, Strainlabel)
predict=clf.predict(Stestdata)
aa=metrics.accuracy_score(Stestlabel, predict)
print(aa)#40.09 ''''============================输出技术问题及其可能性================'''
class attri:
def __init__(self):
self.key=0
self.weight=0.0 '''====================小类======================='''
attribute_proba=clf.predict_proba(Stestdata) label=[]
for i in attribute_proba:
lis=[]
k=0
while k<4:
k=k+1
p=1
mm=0
sj=-1
for j in i:
sj=sj+1
if j>mm:
mm=j
p=sj
i[p]=0#难道是从1开始?
a=attri()
a.key=p
a.weight=mm
lis.append(a)
#lis.append(p)
label.append(lis)
#接下来将label和snew结合,再排序去重就可以和slast比较了
#print("为什么都超限",len(Snew))
print("label",len(label))
count=0
for lis in label:
lis.append(Snew[count])
count=count+1
print("结合完成,准备去重!")#此时label和Snew的长度都为1439 bol=np.zeros(len(label)+1)
Snew=[]
for lis in label:
if bol[lis[4]]==0:
Snew.append(lis)
bol[lis[4]]=1 #print(len(Snew))#去重后为1162 for i in range(len(Slast)+1):
if i==0:
continue
if bol[i]==0:
ls=[]
a=attri()
a.weight=1
a.key=0
ls.append(a)
ls.append(a)
ls.append(a)
ls.append(a)
ls.append(i)
Snew.append(ls)
#print("Snew",len(Snew)) #为1329 print("去重完毕,准备排序!") Snew.sort(key=operator.itemgetter(4))
print("排序完毕,准备比较!") myexcel = xlwt.Workbook()
sheet = myexcel.add_sheet('sheet')
si=-2
sj=-1
#cys=1
#print(Snew)
for i in Snew:
si=si+2
#print(si)
#print("对于记录 %d:" % cys)
#cys=cys+1
for j in range(len(i)):
if(j==len(i)-1):
continue
sj=sj+1
#sheet.write(si,sj,str(j))
sheet.write(si,sj,str(i[j].key))
sheet.write(si+1,sj,str(i[j].weight*100))
#print ("发生技术问题 %d 的可能性是:%.2f %%" % (j.key,j.weight*100))
sj=-1
myexcel.save("Snew.xls")

Python项目输出小类概率,机器学习的更多相关文章

  1. python项目实战-小游戏1

    项目规则: 1.玩家和敌人分别从现有的角色中选择3个角色 2.随机生成目前的血量,和攻击量 3.游戏规则:当玩家向敌人发起攻击,敌人当前的血量=之前的血量-玩家的血量,同理 4.3局两胜 5.自定义玩 ...

  2. Python mysql 操作小类,供大家用用

    import binascii import os import linecache import time #add pyDes path #sys.path.append("/data1 ...

  3. 以正确的方式开源 Python 项目

    以正确的方式开源 Python 项目 大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清 晰和无痛.我不是 ...

  4. 以正确的方式开源 Python 项目(转)

    大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清晰和无痛.我不是简单的指——“创建一个GitHub库,提 ...

  5. Python 爬取的类封装【将来可能会改造,持续更新...】(2020年寒假小目标09)

    日期:2020.02.09 博客期:148 星期日 按照要求,我来制作 Python 对外爬取类的固定部分的封装,以后在用 Python 做爬取的时候,可以直接使用此类并定义一个新函数来处理CSS选择 ...

  6. 2013流行Python项目汇总

    2013流行Python项目汇总 转自:http://www.kankanews.com/ICkengine/archives/102963.shtml Python作为程序员的宠儿,越来越得到人们的 ...

  7. 流行的Python项目汇总

    年有哪些流行的Python项目呢?下面,我们一起来看下. 一.测试和调试 python_koans :Python Koans 算 “Ruby Koans” 的一部分,作为交互式教程,可以学习 TDD ...

  8. Docker如何部署Python项目

    Docker 部署Python项目 作者:白宁超 2019年5月24日09:09:00 导读: 软件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正确,软件才能运行.如 ...

  9. 笔记14:Docker 部署Python项目

    Docker 部署Python项目 导读: 软件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正确,软件才能运行.如果从一种操作系统里面运行另一种操作系统,通常我们采取 ...

随机推荐

  1. [PyTorch]论文pytorch复现中遇到的BUG

    目录 1. zip argument #1 must support iteration 2. torch.nn.DataParallel 3. model.state_dict() 1. zip a ...

  2. iframe与父页面传值

    最近做的项目中用到了不少iframe,而且需要实现: 从父页面获取iframe中某个元素的值或则从iframe页面获取其父页面上某个元素的值. 在网上查询了相关东西,后总结如下: (1)父页面获取if ...

  3. Android静态变量的生命周期

    Android是用Java开发,其静态变量的生命周期遵守Java的设计.我们知道静态变量是在类被load的时候分配内存的,并且存在于方法区.当类 被卸载的时候,静态变量被销毁.在PC机的客户端程序中, ...

  4. flutter 安装详细教程

    Flutter 是 Google 用以帮助开发者在 iOS 和 Android 两个平台开发高质量原生 UI 的移动 SDK.Flutter 兼容现有的代码,免费且开源,在全球开发者中广泛被使用. 安 ...

  5. Appium 自动化测试(6) -- 使用Appium操作YY语音例子

    #!/usr/bin/env python # -*- coding: utf-8 -*- import os import unittest from appium import webdriver ...

  6. C primer plus 5 读书笔记2

    1..字符串的输入:scanf()在读入时,当遇到空白字符空格blank.制表符tab.换行符newline时停止读取.一般使用gets(),来输入字符串. 2.strlen(),一字符为单位输出输出 ...

  7. CF 272E Dima and Horses 染色,dfs 难度:2

    http://codeforces.com/problemset/problem/272/E 把仇恨关系想象为边, 因为度只能为0,1,2,3,所以有以下几种 0,1 直接放即可 2: 有(1,1), ...

  8. java-IO-基本输出输入流

    / 标准输入输出流: 直接类名调用 一经创建无法改变 public static final PrintStream err “标准”错误输出流. public static final InputS ...

  9. L200

    Last week, I read a story about a 34-year-old British woman who is extremely afraid of metal forks.S ...

  10. eclipse加入c标签

    在MyEclipse中使用jstl标签只需导读jstl.jar就能使用,但是在Eclipse中还需要一点小套路 步骤: 一.导入jstl.jar 二.导入导入standard.jar 三.在WEB-I ...