python - 在新的(看不见的)文本上部署文本分类模型
问题描述
我正在研究文本分类问题。我附上了一个我训练过的文本分类模型的简单虚拟片段。
如何在 new_text 上部署模型?当模型在 上使用时check_predictions
,它对文本进行了正确分类,但是,当使用新数据时,分类不正确。
这是因为new_text
需要矢量化吗?我错过了一些基本的东西吗?
from collections import Counter
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score, precision_score, recall_score
df = pd.read_csv("/Users/veg.csv")
print (df)
X_train, X_test, y_train, y_test = train_test_split(df['Text'], df['Label'],random_state=1, test_size=0.2)
cv = CountVectorizer()
X_train_vectorized = cv.fit_transform(X_train)
X_test_vectorized = cv.transform(X_test)
naive_bayes = MultinomialNB()
naive_bayes.fit(X_train_vectorized, y_train)
predictions = naive_bayes.predict(X_test_vectorized)
print("Accuracy score: ", accuracy_score(y_test, predictions))
print('accuracy %s' % accuracy_score(predictions, y_test))
print(classification_report(y_test, predictions))
check_predictions = []
for i in range(len(X_test)):
if predictions[i] == 0:
check_predictions.append('vegetable')
if predictions[i] == 1:
check_predictions.append('fruit')
if predictions[i] == 2:
check_predictions.append('tree')
dummy_df = pd.DataFrame({'actual_label': list(y_test), 'prediction': check_predictions, 'Text':list(X_test)})
dummy_df.replace(to_replace=0, value='vegetable', inplace=True)
dummy_df.replace(to_replace=1, value='fruit', inplace=True)
dummy_df.replace(to_replace=2, value='tree', inplace=True)
print("DUMMY DF")
print(dummy_df.head(10))
new_data=['carrot', 'grapes',
'banana', 'potato',
'birch','carrot', 'grapes',
'banana', 'potato', 'birch','carrot','grapes',
'banana', 'potato',
'birch','carrot', 'grapes',
'banana', 'potato', 'birch','grapes',
'banana', 'potato', 'birch']
new_predictions = []
for i in range(len(new_data)):
if predictions[i] == 0:
new_predictions.append('vegetable')
if predictions[i] == 1:
new_predictions.append('fruit')
if predictions[i] == 2:
new_predictions.append('tree')
new_df = pd.DataFrame({'actual_label': list(y_test), 'prediction': new_predictions, 'Text':list(new_data)})
new_df.replace(to_replace=0, value='vegetable', inplace=True)
new_df.replace(to_replace=1, value='fruit', inplace=True)
new_df.replace(to_replace=2, value='tree', inplace=True)
print("NEW DF")
print(new_df.head(10))
解决方案
无论您输入模型的任何(新)文本都必须经过与您的训练数据完全相同的预处理步骤 - 这里 CountVectorizer
已经适合您的X_train
:
new_data_vectorized = cv.transform(new_data) # NOT fit_transform
new_predictions = naive_bayes.predict(new_data_vectorized)
推荐阅读
- javascript - 如何在反应钩子数组中获取其对象的值?
- python - 读取 csv 文件时 Kaggle 中的错误消息
- keras - 如何在单个代码中使用 3 个不同版本的 keras?
- angular - 如何访问在父模块中声明的组件
- javascript - 如何使用函数 reverseArray 反转数组
- python - 值错误:对已关闭文件的 I/O 操作
- javascript - 升级 .then .catch 到 async await 并尝试 catch
- swift - 资产目录中的图像名称未显示在自动完成中 - xcode 12
- sql - Redshift,十进制值的转换不四舍五入
- python - 在数据科学中,使用 x X 有什么区别?