gpt4 book ai didi

python - sklearn模型数据转换错误: CountVectorizer - Vocabulary wasn't fitted

转载 作者:行者123 更新时间:2023-11-30 08:52:07 25 4
gpt4 key购买 nike

我已经训练了一个主题分类模型。然后,当我要将新数据转换为向量进行预测时,就会出错。它显示“NotFittedError:CountVectorizer - 词汇未安装。”但是,当我通过将训练数据拆分为训练模型中的测试数据来进行预测时,它起作用了。代码如下:

from sklearn.externals import joblib
from sklearn.feature_extraction.text import CountVectorizer

import pandas as pd
import numpy as np

# read new dataset
testdf = pd.read_csv('C://Users/KW198/Documents/topic_model/training_data/testdata.csv', encoding='cp950')

testdf.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1800 entries, 0 to 1799
Data columns (total 2 columns):
keywords 1800 non-null object
topics 1800 non-null int64
dtypes: int64(1), object(1)
memory usage: 28.2+ KB

# read columns
kw = testdf['keywords']
label = testdf['topics']

# 將預測資料轉為向量
vectorizer = CountVectorizer(min_df=1, stop_words='english')
x_testkw_vec = vectorizer.transform(kw)

这里有一个错误

---------------------------------------------------------------------------
NotFittedError Traceback (most recent call last)
<ipython-input-93-cfcc7201e0f8> in <module>()
1 # 將預測資料轉為向量
2 vectorizer = CountVectorizer(min_df=1, stop_words='english')
----> 3 x_testkw_vec = vectorizer.transform(kw)

~\Anaconda3\envs\ztdl\lib\site-packages\sklearn\feature_extraction\text.py in transform(self, raw_documents)
918 self._validate_vocabulary()
919
--> 920 self._check_vocabulary()
921
922 # use the same matrix-building strategy as fit_transform

~\Anaconda3\envs\ztdl\lib\site-packages\sklearn\feature_extraction\text.py in _check_vocabulary(self)
301 """Check if vocabulary is empty or missing (not fit-ed)"""
302 msg = "%(name)s - Vocabulary wasn't fitted."
--> 303 check_is_fitted(self, 'vocabulary_', msg=msg),
304
305 if len(self.vocabulary_) == 0:

~\Anaconda3\envs\ztdl\lib\site-packages\sklearn\utils\validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
766
767 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 768 raise NotFittedError(msg % {'name': type(estimator).__name__})
769
770

NotFittedError: CountVectorizer - Vocabulary wasn't fitted.

最佳答案

您需要调用vectorizer.fit()让计数向量化器在调用 vectorizer.transform() 之前构建单词词典。您也可以调用vectorizer.fit_transform()将两者结合起来。

但是您不应该使用新的向量化器进行测试或任何类型的推理。您需要使用训练模型时使用的相同的,否则您的结果将是随机的,因为词汇不同(缺少一些单词,没有相同的对齐方式等......)

为此,您可以 pickle训练中使用的向量化器并在推理/测试时加载它。

关于python - sklearn模型数据转换错误: CountVectorizer - Vocabulary wasn't fitted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49547715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com