pyhton代码文本分类预测实验
时间: 2023-09-22 14:05:59 浏览: 68
以下是使用Python实现文本分类预测的示例代码:
```python
# 导入必要的库
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
# 读取数据集
data = pd.read_csv("data.csv")
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data["text"], data["label"], test_size=0.2, random_state=42)
# 特征提取
vectorizer = TfidfVectorizer()
X_train_vectors = vectorizer.fit_transform(X_train)
X_test_vectors = vectorizer.transform(X_test)
# 构建模型
model = LinearSVC()
model.fit(X_train_vectors, y_train)
# 预测并评估模型
y_pred = model.predict(X_test_vectors)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
其中,数据集需要按照以下格式存储为CSV文件:
| text | label |
|------|-------|
| ... | ... |
其中,text为文本内容,label为所属类别。
阅读全文