- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经训练了一个主题分类模型。然后,当我要将新数据转换为向量进行预测时,就会出错。它显示“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/
它必须有一个名字。我在考虑退化或未使用(旧的描述性不够)。有没有人想出一些描述性的东西来调用它? 最佳答案 Deprecated (在 Java 中)或 Obsolete (在 C# 中) 关于voc
我正在启动一个公益项目,该项目是世界上最大的琵琶音乐收藏的Web界面,从多个角度来看,这是一个具有挑战性的收藏。这些作品主要是从1400年到1600年,但范围从1200年代中期到今天。毋庸置疑,作品的
以下代码使用spaCy word vectors通过首先计算词汇表(超过一百万)中所有单词的余弦相似度,然后对最相似单词列表进行排序,找到与给定单词最相似的 20 个单词。 parser = Engl
我实例化了一个 sklearn.feature_extraction.text.CountVectorizer通过 vocabulary 参数传递一个词汇表来对象,但我得到一个 sklearn.uti
跨编程语言,我遇到过类似的 composite types不同的名字: Optional / Maybe Any Variant / Sum Record / Product 人们经常使用术语 词汇类
我正在使用ColumnTransformer运行一个非常简单的实验,目的是转换列数组,在此示例中为[“a”]: from sklearn.feature_extraction.text import
我已经训练了一个主题分类模型。然后,当我要将新数据转换为向量进行预测时,就会出错。它显示“NotFittedError:CountVectorizer - 词汇未安装。”但是,当我通过将训练数据拆分为
我正在尝试使用 scikit-learn/pandas 构建一个情感分析器。构建和评估模型有效,但尝试对新样本文本进行分类却行不通。 我的代码: import csv import pandas as
我正在尝试使用 scikit-learn/pandas 构建情绪分析器。构建和评估模型有效,但尝试对新示例文本进行分类却行不通。 我的代码: import csv import pandas as p
我有一个使用 Python 的 Gensim 库训练的 Word2vec 模型。我有一个标记化列表如下。词汇量是 34,但我只给出了 34 个中的几个: b = ['let', 'know', '
是否可以提前知道 CountVectorizer 是否会抛出 ValueError: empty vocabulary? 基本上,我有一个文档语料库,我想过滤掉那些无法通过 CountVectoriz
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我在这里阅读了所有相关问题,但找不到有效的解决方案: 我的分类器创建: class StemmedTfidfVectorizer(TfidfVectorizer): def build_ana
使用 python 训练 word2vec 模型后 gensim ,如何找到模型词汇表中的单词数? 最佳答案 在最近的版本中,model.wv 属性包含单词和向量,并且 can 本身可以报告长度 -
我需要有目的地再创建 2 个表:一个表将存储标签和类别数据(类别可以有层次结构,但标签没有),另一个表存储标签、类别和内容之间的关系。但我对那两张 table 的名称很困惑。我确实是网络开发的新手。经
我正在尝试使用 Spark 的 MLLib 实现词向量化。我按照给出的例子 here . 我有一堆句子,我想将它们作为输入来训练模型。但我不确定这个模型是否采用句子或仅将所有单词作为字符串序列。 我的
在 Keras 文档中的嵌入 https://keras.io/layers/embeddings/ ,对 mask_zero 的解释是 mask_zero: Whether or not the i
我尝试使用 word2vec,但在尝试对任何单词执行任何操作时都会出错。这似乎是一个编码问题,这是我所做的: 初始化word2vec: import gensim, logging logging.b
我正在使用word2vec,我训练的wiki语料库,如果我输入的单词不在word2vec的词汇表中怎么办? 测试一下: model = word2vec.Word2Vec.load('model/'
目标:预测原始数据的标签 背景:我构建了一个 SVM 分类器 我正在使用以下代码: 0) 导入模块 import numpy as np from sklearn import cro
我是一名优秀的程序员,十分优秀!