能否优化以下程序,使用test.csv和train.csv,程序如下:import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.model_selection import train_test_split from sklearn.naive_bayes import MultinomialNB # 加载 CSV 文件 df = pd.read_csv('news.csv', encoding='utf-8', error_bad_lines=False) # 删除无效行 df.dropna(inplace=True) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(df['content'], df['category'], test_size=0.2) # 特征提取 vectorizer = TfidfVectorizer() X_train = vectorizer.fit_transform(X_train) X_test = vectorizer.transform(X_test) # 训练模型 model = MultinomialNB() model.fit(X_train, y_train) # 测试模型 score = model.score(X_test, y_test) print('Accuracy:', score)
时间: 2023-12-03 14:47:29 浏览: 166
pd_split_train_test.rar_pandas_pandas对数据分类_pd.split_split_数据分类
以下是优化后的程序:
```python
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
# 加载 CSV 文件
train_df = pd.read_csv('train.csv', encoding='utf-8', error_bad_lines=False)
test_df = pd.read_csv('test.csv', encoding='utf-8', error_bad_lines=False)
# 删除无效行
train_df.dropna(inplace=True)
test_df.dropna(inplace=True)
# 划分训练集和测试集
X_train = train_df['content']
y_train = train_df['category']
X_test = test_df['content']
y_test = test_df['category']
# 特征提取
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)
# 训练模型
model = MultinomialNB()
model.fit(X_train, y_train)
# 测试模型
score = model.score(X_test, y_test)
print('Accuracy:', score)
```
优化点如下:
1. 加载了 train.csv 和 test.csv 文件,分别作为训练集和测试集。
2. 删除了无效行的代码,应用到了 train.csv 和 test.csv 中。
3. 划分训练集和测试集时,直接使用 train_df 和 test_df 中的数据,不需要先合并再划分。
4. 代码格式更加清晰易读。
阅读全文