python基于svm的情感分析
时间: 2023-12-15 17:33:03 浏览: 98
基于Python实现酒店评论的情感分析
以下是基于SVM的情感分析Python代码示例:
```python
# 导入所需的库
import pandas as pd
import numpy as np
import re
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, roc_curve, auc
import matplotlib.pyplot as plt
# 读取数据
data = pd.read_csv('data.csv')
# 数据预处理
data['text'] = data['text'].apply(lambda x: re.sub('[^a-zA-Z]', ' ', x))
data['text'] = data['text'].apply(lambda x: x.lower())
# 特征提取
cv = CountVectorizer(stop_words='english')
X = cv.fit_transform(data['text'])
y = data['label']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=11)
# 训练模型
svm = SVC(kernel='linear', probability=True)
svm.fit(X_train, y_train)
# 预测结果
y_pred = svm.predict(X_test)
# 评估模型
print('Accuracy:', accuracy_score(y_test, y_pred))
print('Confusion Matrix:\n', confusion_matrix(y_test, y_pred))
print('Classification Report:\n', classification_report(y_test, y_pred))
# 绘制ROC曲线
y_score = svm.predict_proba(X_test)[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
```
阅读全文