import pandas as pdimport numpy as npfrom csv import readerfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitfilename = input("请输入文件名:")with open(filename, 'rt', encoding='UTF-8') as file: A = file.readlines()data = np.array(A)df = pd.DataFrame(data, columns=['text'])# 分割数据集为训练集和测试集X_train, X_test, y_train, y_test = train_test_split(df['text'], df['category'], random_state=42)# 对文本进行特征提取tfidf = TfidfVectorizer(stop_words='english')X_train_tfidf = tfidf.fit_transform(X_train.astype(str))X_test_tfidf = tfidf.transform(X_test.astype(str))# 使用多项式朴素贝叶斯模型进行训练和预测clf = MultinomialNB()clf.fit(X_train_tfidf, y_train)y_pred = clf.predict(X_test_tfidf.toarray())# 输出模型的准确率print('Accuracy:', accuracy_score(y_test, y_pred))如何修改能导入我计算机上的文件
时间: 2023-06-24 18:04:29 浏览: 121
python数据分析与可视化 import pandas as pd import numpy as np import m
你可以使用文件对话框来获取用户输入的文件名,然后使用该文件名打开文件并读取数据。下面是修改后的代码:
```python
import pandas as pd
import numpy as np
from csv import reader
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from tkinter import filedialog
import tkinter as tk
root = tk.Tk()
root.withdraw()
filename = filedialog.askopenfilename()
with open(filename, 'rt', encoding='UTF-8') as file:
A = file.readlines()
data = np.array(A)
df = pd.DataFrame(data, columns=['text'])
# 分割数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['category'], random_state=42)
# 对文本进行特征提取
tfidf = TfidfVectorizer(stop_words='english')
X_train_tfidf = tfidf.fit_transform(X_train.astype(str))
X_test_tfidf = tfidf.transform(X_test.astype(str))
# 使用多项式朴素贝叶斯模型进行训练和预测
clf = MultinomialNB()
clf.fit(X_train_tfidf, y_train)
y_pred = clf.predict(X_test_tfidf.toarray())
# 输出模型的准确率
print('Accuracy:', accuracy_score(y_test, y_pred))
```
这里我们使用了 Python 自带的 tkinter 模块来创建一个文件对话框,让用户选择要导入的文件。注意,你需要在运行该程序时手动输入 `import tkinter as tk`。
阅读全文