机器学习-分类2

今天,在分类模型上又添加了如下代码。

#单个预测,对输出的类别进行排序
Class = list(pipe.classes_)
print(Class)
X=['感冒 咳嗽 恶心 头晕 头痛']
Pipe = pipe.predict(X)
print(Pipe)
prob = pipe.predict_proba(X)
Prob = sum(prob.tolist(),[])
dict1=dict(zip(Class,Prob))
SORT=sorted(dict1.items(), key=lambda e:e[1], reverse=True)
test1={}
test1[Pipe[0]]=SORT
print(test1)

输出结果如下

['免疫系统', '内分泌系统', '呼吸系统', '泌尿系统', '消化系统', '淋巴系统', '生殖系统', '皮肤系统', '神经系统', '肌肉系统', '血液血管', '骨骼系统']
['呼吸系统']
{'呼吸系统': [('呼吸系统', 0.4076064809555076), ('消化系统', 0.12141905116600904), ('骨骼系统', 0.059634970594289), ('神经系统', 0.05574679784390724), ('血液血管', 0.05548952693913911), ('皮肤系统', 0.05461155325220834), ('免疫系统', 0.05206000062171274), ('肌肉系统', 0.049025147795265955), ('生殖系统', 0.04393882033284857), ('泌尿系统', 0.03716749480813982), ('淋巴系统', 0.034065064514598876), ('内分泌系统', 0.029235091176373674)]}
#对没有label的文件进行预测,对预测结果进行排序后输出json格式文件
import json
f=open('test_to_predict.txt','r',encoding='utf-8')
test ={}
for i in f:
    i=i.strip('\n')
    Pipe = pipe.predict([i])
#     print(Pipe)
    prob = pipe.predict_proba([i])
    Prob = sum(prob.tolist(),[])
    dict2=dict(zip(Class,Prob))#将2个list整合成dict,也可以整合成tuple
    Sort = sorted(dict2.items(), key=lambda e:e[1], reverse=True)
#     print(Sort)
    test[Pipe[0]]=Sort
print(test)
# print(json.dumps(test,ensure_ascii=False))
with open('predict_result.json','w',encoding='utf-8') as f:
    f.write(json.dumps(test,ensure_ascii=False))
#计算预测的数据集的准确率。predict_text是已经标好了label的数据,用Tab键隔开
f=open('predict_text.txt','r',encoding='utf-8')
total_num=0
corr_num=0
for i in f:   
    i = i.encode('utf-8').decode('utf-8-sig').strip('\n').split('\t')#防止读入txt出现\ufeff的问题
    Pipe = pipe.predict([i[1]])
#     print(Pipe)
#     print([i[0]])
    if [i[0]]==Pipe:
        corr_num += 1
    total_num +=1
# print(corr_num)
print(corr_num/total_num)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值