NLP(中文文本多标签文本分类)
本文主要是说明中文文本多标签分类的具体流程,结果不理想暂不考虑,后续再进行优化(刚接触NLP)。
先来说说多标签分类和二分类,多分类的区别。二分类,多分类的标签只有一个,换句话说就是只属于一个类别,多标签分类则是属于多个类别。
数据集采用的是头条新闻的数据(处理起来有点坑。。。)头条新闻的数据
总体流程分为主要是三个步骤:
1.数据预处理。(删除缺失值,删除非中文字符,文本划分)
2.文本向量化。(one-hot,tf_idf,word2vec等)
3.采用机器学习的方法(主要是神经网络)
上述理论知识不再介绍。
神经网络模型参数:
import pandas as pd
import re
import numpy as np
from sklearn.preprocessing import MultiLabelBinarizer
import jieba
from sklearn.model_selection import train_test_split
from gensim.models import Word2Vec
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
filepath = 'G:\\Alops\DataSet\\toutiao-multilevel-text-classfication-dataset-master\\mlc_dataset_part_aa'
cloNames = ['newsId', 'newsClass', 'newsTitle', 'newsFeatures', 'newsLabel', 'other']
stopWord_filepath = "G:\\Alops\DataSet\\toutiao-multilevel-text-classfication-dataset-master\\stopWord.json"
def readFile(filepath):
f = open(filepath, encoding='utf-8')
text_list = []
line = f.readline().strip()
while line:
text_list.append(line)
line = f.readline().strip()
f.close()
result = []
for list in text_list:
result.append(list.split('|,|'))
return result
# 删除缺失值
def drop_null(data_use):
index1 = data_use[data_use.isnull