情感分析是一种文本分析技术,用于确定文本中的情感倾向,例如正面、负面或中性。在本文中,我们将介绍如何使用Python和机器学习算法进行情感分析。
首先,我们需要准备一个情感分析的数据集。可以使用公开可用的数据集,例如IMDB电影评论数据集或Twitter情感分析数据集。在这里,我们将使用IMDB电影评论数据集作为示例。
python
# 导入所需的库和模块
import pandas as pd
import numpy as np
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
# 加载IMDB电影评论数据集
df = pd.read_csv('imdb_dataset.csv')
# 对评论文本进行预处理
def preprocess_text(text):
# 将文本转换为小写
text = text.lower()
# 删除标点符号和数字
text = re.sub('[^a-z]+', ' ', text)
# 分词
tokens = nltk.word_tokenize(text)
# 删除停用词
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens if token not in stop_words]
# 词形还原
lemmatizer = WordNetLemmatizer()
tokens = [lemmatizer.lemmatize(token) for token in tokens]
# 将词列表转换为字符串
text = ' '.join(tokens)
return text
# 对评论文本进行预处理
df['text'] = df['text'].apply(preprocess_text)
# 将数据集拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['sentiment'], test_size=0.2)
# 将文本转换为词袋向量
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
X_test_counts = count_vect.transform(X_test)
# 计算TF-IDF权重
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
# 训练朴素贝叶斯分类器
clf = MultinomialNB().fit(X_train_tfidf, y_train)
# 对测试集进行预测
y_pred = clf.predict(X_test_tfidf)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
在上述代码中,我们首先导入所需的库和模块,然后加载IMDB电影评论数据集。接下来,我们定义了一个preprocess_text函数,用于对评论文本进行预处理。预处理过程包括将文本转换为小写、删除标点符号和数字、分词、删除停用词和词形还原。然后,我们将数据集拆分为训练集和测试集,并使用CountVectorizer将文本转换为词袋向量。接下来,我们计算了TF-IDF权重,并使用MultinomialNB训练了一个朴素贝叶斯分类器。最后,我们对测试集进行预测,并计算了准确率。
本文内容由GPT编写