SVM情感极性分析的代码
时间: 2023-08-30 22:10:51 浏览: 92
以下是使用SVM进行情感极性分析的示例代码:
```python
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
# 读取数据集
data = pd.read_csv('sentiment_analysis_data.csv')
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(
data['text'], data['sentiment'], test_size=0.2, random_state=42)
# 特征提取
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)
# 训练模型
clf = LinearSVC()
clf.fit(X_train, y_train)
# 预测
y_pred = clf.predict(X_test)
# 评估
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
```
其中,数据集应该包含两列:一列是文本,另一列是情感极性标签。我们使用TF-IDF向量化文本,并使用线性支持向量机作为分类器。最后评估模型的准确率。
阅读全文