- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习如何通过 sklearn 处理文本数据,但遇到了一个无法解决的问题。
我正在关注的教程是:http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html
输入是具有两列的 pandas df。一种带有文本,一种带有二进制类。
代码:
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
traindf, testdf = train_test_split(nlp_df, stratify=nlp_df['class'])
x_train = traindf['text']
x_test = traindf['text']
y_train = traindf['class']
y_test = testdf['class']
# CV
count_vect = CountVectorizer(stop_words='english')
x_train_modified = count_vect.fit_transform(x_train)
x_test_modified = count_vect.transform(x_test)
# TF-IDF
idf = TfidfTransformer()
fit = idf.fit(x_train_modified)
x_train_mod2 = fit.transform(x_train_modified)
# MNB
mnb = MultinomialNB()
x_train_data = mnb.fit(x_train_mod2, y_train)
text_clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', MultinomialNB()),
])
predicted = text_clf.predict(x_test_modified)
当我尝试运行最后一行时:
---------------------------------------------------------------------------
NotFittedError Traceback (most recent call last)
<ipython-input-64-8815003b4713> in <module>()
----> 1 predicted = text_clf.predict(x_test_modified)
~/anaconda3/lib/python3.6/site-packages/sklearn/utils/metaestimators.py in <lambda>(*args, **kwargs)
113
114 # lambda, but not partial, allows help() to work with update_wrapper
--> 115 out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)
116 # update the docstring of the returned function
117 update_wrapper(out, self.fn)
~/anaconda3/lib/python3.6/site-packages/sklearn/pipeline.py in predict(self, X)
304 for name, transform in self.steps[:-1]:
305 if transform is not None:
--> 306 Xt = transform.transform(Xt)
307 return self.steps[-1][-1].predict(Xt)
308
~/anaconda3/lib/python3.6/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/lib/python3.6/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/lib/python3.6/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.
关于如何修复此错误有什么建议吗?我正在根据测试数据正确转换 CV 模型。我什至检查了词汇列表是否为空,它不是 (count_vect.vocabulary_)
谢谢!
最佳答案
您的问题有几个问题。
对于初学者来说,您实际上并不适合管道,因此会出现错误。更仔细地查看 linked tutorial ,您会看到有一个步骤 text_clf.fit
(其中 text_clf
确实是管道)。
其次,您没有正确使用管道的概念,它恰好适合端到端的整个内容;相反,您将其各个组件一一适配...如果您再次查看教程,您将看到管道适配的代码:
text_clf.fit(twenty_train.data, twenty_train.target)
使用初始形式的数据,而不像您一样使用中间转换;本教程的重点是演示如何将各个转换封装在管道中(并替换为管道),而不是在这些转换之上使用管道...
第三,您应该避免将变量命名为 fit
- 这是一个保留关键字;同样,我们不使用 CV 来缩写 Count Vectorizer(在 ML 术语中,CV 代表交叉验证)。
也就是说,这是使用管道的正确方法:
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
traindf, testdf = train_test_split(nlp_df, stratify=nlp_df['class'])
x_train = traindf['text']
x_test = traindf['text']
y_train = traindf['class']
y_test = testdf['class']
text_clf = Pipeline([('vect', CountVectorizer(stop_words='english')),
('tfidf', TfidfTransformer()),
('clf', MultinomialNB()),
])
text_clf.fit(x_train, y_train)
predicted = text_clf.predict(x_test)
正如您所看到的,管道的目的是让事情变得更简单(与逐个顺序使用组件相比),而不是让它们进一步复杂化......
关于machine-learning - 管道中 CountVectorizer 的 Sklearn NotFittedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51772605/
我正在使用 sklearn Pipeline 和 FeatureUnion 从文本文件创建特征,我想打印出特征名称。 首先,我将所有转换收集到一个列表中。 In [225]:components Ou
基本上,我正在尝试为多元线性回归模型创建一个 GUI,用户可以在其中添加一些值并从中获取电影分数。这是代码: import tkinter as tk import sklearn as sk roo
我正在尝试在 XGBoost 拟合模型上使用 sklearn plot_partial_dependence 函数,即在调用 .fit 之后。但我不断收到错误消息: 未安装错误:此 XGBRegres
我正在尝试学习如何通过 sklearn 处理文本数据,但遇到了一个无法解决的问题。 我正在关注的教程是:http://scikit-learn.org/stable/tutorial/text_ana
我尝试了随机森林回归。 代码如下。 import numpy as np from sklearn.preprocessing import StandardScaler from sklearn.m
多标签分类 我正在尝试使用 scikit-learn/pandas/OneVsRestClassifier/logistic 回归来预测多标签分类。构建和评估模型有效,但尝试对新示例文本进行分类则无效
我正在尝试使用 sklearn.inspection.plot_partial_dependence 创建部分依赖图在我使用 keras 和 keras sklearn 包装实用程序成功构建的模型上(
我是一名优秀的程序员,十分优秀!