做内贸的电子商务网站典型有,联通公司做网站吗,湘潭网站,wordpress 评论顺序NeuralNLP-NeuralClassifier的使用记录#xff0c;训练预测自己的【中文文本多分类】
数据准备#xff1a;
与英文的训练预测一致#xff0c;都使用相同的数据格式#xff0c;将数据通过代码处理为JSON格式#xff0c;以下是我使用的一种#xff0c;不同的原数据情况…NeuralNLP-NeuralClassifier的使用记录训练预测自己的【中文文本多分类】
数据准备
与英文的训练预测一致都使用相同的数据格式将数据通过代码处理为JSON格式以下是我使用的一种不同的原数据情况会有所改动
import jieba.analyse as ana
import re
import jiebadef make_data_json(df,outpath):def stop_words(path):txt open(outpath,r,encodingutf-8) lines txt.readlines()txt.close()stop_txt []for line in lines:stop_txt.append(line.strip(\n))return stop_txtwith open(outpath, w, encodingutf-8) as f:# with open(output_path, w) as fw:for indexs in df.index:dict1 {}dict1[doc_label] [str(df.loc[indexs].values[0])]doc_token df.loc[indexs].values[1]# 只保留中文、大小写字母和阿拉伯数字reg [^0-9A-Za-z\u4e00-\u9fa5]doc_token re.sub(reg, , doc_token)print(doc_token)# 中文分词seg_list jieba.cut(doc_token, cut_allFalse)#$提取关键词20个ana.set_stop_words(./人工智能挑战赛-文本分类/停用词列表.txt)keyword ana.extract_tags(doc_token, topK20,withWeightFalse,) #True表示显示权重# 去除停用词content [x for x in seg_list if x not in stop_words(../data/stop_words.txt)]dict1[doc_token] contentdict1[doc_keyword] keyworddict1[doc_topic] []# 组合成字典print(dict1)# 将字典转化成字符串json_str json.dumps(dict1, ensure_asciiFalse)f.write(%s\n % json_str)
使用构造JSON数据方法 训练前期准备
1、创建中文数据文件夹Chinese_datas
2、创建该数据的文本数据对应的标签集Chinese_label.taxonomy
3、创建该数据的训练配置文件Chinese_train_conf.json
继续目录如下 配置文件的注意点 其中需要额外修改的地方
work_nums0
以及涉及代码中有读取文件的部分都需要给编码中文编码
with open(encoding‘utf-8’)
训练
训练代码
python train.py conf/Chinese_train_conf.json训练后生成的权重文件在配置文件中就写出了 预测 python predict.py conf/Chinese_train_conf.json Chinese_datas/predict_data.json预测结果 可以看出预测效果仅一个错误该模型方便NLP的比赛分类等准确率也很高。
代码获取
下载就是中文分类版在命令界面进行命令行输入训练和预测
链接https://pan.baidu.com/s/1fw_ipmOFWMiTLAFrs9i5ig 提取码2023