【问题标题】:error ::sklearn.exceptions.NotFittedError: CountVectorizer - Vocabulary wasn't fitted错误 ::sklearn.exceptions.NotFittedError: CountVectorizer - 未安装词汇
【发布时间】:2018-08-31 12:20:51
【问题描述】:

如何解决vectorizor.tranform(fd_norm)中的错误?

encoder = LabelEncoder()
vectorizer = CountVectorizer()
lis=[description]
lis1=[name_predict]
lis2=[text_predict]
lis_df=pd.DataFrame(lis,columns=['description'])
lis1_df=pd.DataFrame(lis1,columns=['name'])
lis2_df=pd.DataFrame(lis2,columns=['text'])
pred_df=pd.concat([lis_df,lis1_df,lis2_df],axis=1)
fd=pred_df.iloc[ : , : ].values    
fd_norm=[normalize_text(s) for s in fd]
predV=vectorizer.transform(fd_norm)
fname='gender_predictor.sav'
model=pickle.load(open(fname,'rb'))
fresnel=model.predict(predV)
fresnel_label=encoder.inverse_transform(fresnel)
self.gender.setText(fresnel_label)

错误:

Traceback (most recent call last):
  File "the_linking.py", line 162, in predict
    predV=self._vectorizer.transform(fd_norm)
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 890, in transform
    self._check_vocabulary()
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 278, in _check_vocabulary
    check_is_fitted(self, 'vocabulary_', msg=msg),
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 690, in check_is_fitted
    raise _NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: CountVectorizer - Vocabulary wasn't fitted.

【问题讨论】:

  • 标题只是错误信息,问题内容只是代码不是好问题,推荐阅读How to Ask
  • 这个问题与 PyQt 无关,而与 sklearn 有关。
  • yaa 我正在尝试从 textEdit 小部件中获取文本并将其矢量化,但它显示此错误
  • 什么是textedit?,我在做之前看到了问题。

标签: python scikit-learn


【解决方案1】:

在调用transform()之前,必须调用fit()fit_transform()一次,这样模型才能学习到数据的频率。 试试:

predV=vectorizer.fit_transform(fd_norm)

编码器也是如此。我认为你之前已经改变了数据然后训练了模型。但从那里,你只保存了model,但不是矢量化器。保存模型时,您需要同时保存vectorizerencoder,以便能够再次使用它们。以与模型相同的方式加载它们并使用它们(在这种情况下不调用 fit()。)

【讨论】:

  • 我已经安装了模型并将其腌制到 .sav 文件中。然后我将其加载到上面的代码 span>中
猜你喜欢
  • 2015-12-16
  • 2017-10-26
  • 1970-01-01
  • 2016-09-25
  • 2020-01-15
  • 2020-06-04
  • 2017-10-11
  • 2016-08-15
相关资源
最近更新 更多