文章目录
项目背景
项目的目的,是为了对情感评论数据集进行预测打标。在训练之前,需要对数据进行数据清洗环节,前面已对数据进行清洗,详情可移步至NLP_情感分类_数据清洗
下面对已清洗的数据集,用机器学习方案进行处理
数据清洗
导包
python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm import tqdm
import pickle
import numpy as np
import gc
#import swifter
from sklearn.neural_network import MLPClassifier
import os
from sklearn.metrics import accuracy_score,f1_score,recall_score,precision_score
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier
from lightgbm import LGBMClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm
导入数据
python
df = pd.read_csv('data/sentiment_analysis_clean.csv')
df = df.dropna()
切分评论及标签
python
X_tfidf = df['text']
y_tfidf = df['label']
TF-IDF
TfidfVectorizer是用于文本处理的重要工具之一,它将文本数据转换成数值向量形式,以便于后续的机器学习模型进行训练。TF-IDF代表Term Frequency-Inverse Document Frequency,即词频-逆文档频率算法。
TF-IDFVectorizer可以根据应用场景灵活地控制特征提取的过程,提高文本分析的效率和精度。
TF-IDF 分数由两部分组成:第一部分是词语频率(Term Frequency),第二部分是逆文档频率(Inverse Document Frequency)。其中计算语料库中文档总数除以含有该词语的文档数量,然后再取对数就是逆文档频率。
TF(t)= 该词语在当前文档出现的次数 / 当前文档中词语的总数
IDF(t)= log_e(文档总数 / 出现该词语的文档总数)
构造TF-IDF特征
python
from sklearn.feature_extraction.text import TfidfVectorizer
vec_tfidf = TfidfVectorizer(use_idf=True, smooth_idf=True, norm=None, max_features=1000)
array_TFIDF = vec_tfidf.fit_transform(X_tfidf)
数据切分
python
from sklearn.model_selection import train_test_split, StratifiedKFold
# 这里指定了random_state是为了保证每次切分数据集的结果都是一样的
Xidf_train, Xidf_test, yidf_train, yidf_test = train_test_split(array_TFIDF, y_tfidf,test_size=0.2,random_state=2024)
Xidf_train = Xidf_train.toarray()
Xidf_test = Xidf_test.toarray()
del df,array_TFIDF
gc.collect()
模型训练
python
def train_model(model, X_train, X_test, y_train, y_test):
dic = {'lr':'Logistic Regression',
'nb':'Naive Bayes',
'svm':'Support Vector Machine',
'dt':'Decision Tree',
'rf':'Random Forest',
'lgb':'LightGBM'}
train_acc, test_acc = [], []
if model == 'lr':
clf = LogisticRegression(C=0.01, solver='liblinear')
elif model == 'nb':
clf = MultinomialNB(alpha=100)
elif model == 'svm':
clf = svm.LinearSVC(C=0.01)
elif model == 'dt':
clf = DecisionTreeClassifier(max_depth=100, min_samples_split= 2)
elif model == 'rf':
clf = RandomForestClassifier(max_depth=100,min_samples_split=5)
elif model == 'lgb':
clf = LGBMClassifier(learning_rate=1.0)
else:
print('Model doesn\'t exist')
clf.fit(X_train, y_train)
# predict using train data
train_pred = clf.predict(X_train)
train_acc = accuracy_score(y_train, train_pred)
# predict using test data
test_pred = clf.predict(X_test)
test_acc = accuracy_score(y_test, test_pred)
print()
print("Model: ", dic[model])
print("Training accuracy: {}".format(train_acc))
print("Test accuracy: {}".format(test_acc))
print()
return {
'model_name':dic[model],
'Train Accuracy':train_acc,
'Test Accuracy':test_acc
}
查看结果
python
metric_df = pd.DataFrame(columns=['model_name','Train Accuracy','Test Accuracy'])
for model in ['lr', 'nb', 'svm', 'dt', 'rf', 'lgb']:
metric = train_model(model ,Xidf_train, Xidf_test, yidf_train, yidf_test)
# 将metric转换成一个DataFrame
metric_df = pd.concat([metric_df, pd.DataFrame([metric])], ignore_index=True)
python
metric_df
同类型项目
阿里云-零基础入门NLP【基于深度学习的文本分类3-BERT】
也可以参考进行学习
学习的参考资料:
深度之眼