使用带有 TF-IDF 的管道时,CalibratedClassifierCV 有错误吗?
Bug with CalibratedClassifierCV when using a Pipeline with TF-IDF?
首先提前感谢,我真的不知道我是否应该打开一个问题所以我想检查是否有人以前遇到过这个问题。
所以我在使用 CalibratedClassifierCV 进行文本分类时遇到了以下问题。我有一个估算器,它是以这种方式创建的 pipeline(简单示例):
# Import libraries first
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import make_pipeline
from sklearn.calibration import CalibratedClassifierCV
from sklearn.linear_model import LogisticRegression
# Now create the estimators: pipeline -> calibratedclassifier(pipeline)
pipeline = make_pipeline( TfidfVectorizer(), LogisticRegression() )
calibrated_pipeline = CalibratedClassifierCV( pipeline, cv=2 )
现在我们可以创建一个简单的训练集来检查分类器是否有效:
# Create text and labels arrays
text_array = np.array(['Why', 'is', 'this', 'happening'])
outputs = np.array([0,1,0,1])
当我试图适应 calibrated_pipeline 对象时,出现此错误:
ValueError: Found input variables with inconsistent numbers of samples: [1, 4]
如果你愿意,我可以复制整个异常跟踪,但这应该很容易重现。非常感谢!
编辑:我在创建数组时犯了一个错误。现已修复(感谢@ogrisel!)此外,调用:
pipeline.fit(text_array, outputs)
工作正常,但使用校准后的分类器会失败!
np.array(['Why', 'is', 'this', 'happening']).reshape(-1,1)
是一个二维字符串数组,而 docstring of the fit_transform method of the TfidfVectorizer class 表示它期望:
Parameters
----------
raw_documents : iterable
an iterable which yields either str, unicode or file objects
如果您遍历 2D numpy 数组,您会得到一系列一维字符串数组,而不是直接得到字符串:
>>> list(text_array)
[array(['Why'],
dtype='<U9'), array(['is'],
dtype='<U9'), array(['this'],
dtype='<U9'), array(['happening'],
dtype='<U9')]
所以修复很简单,只需通过:
text_documents = ['Why', 'is', 'this', 'happening']
作为矢量化器的原始输入。
Edit: 备注:默认情况下,LogisticRegression
几乎总是一个经过良好校准的分类器。在这种情况下,CalibratedClassifierCV
可能不会带来任何东西。
首先提前感谢,我真的不知道我是否应该打开一个问题所以我想检查是否有人以前遇到过这个问题。
所以我在使用 CalibratedClassifierCV 进行文本分类时遇到了以下问题。我有一个估算器,它是以这种方式创建的 pipeline(简单示例):
# Import libraries first
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import make_pipeline
from sklearn.calibration import CalibratedClassifierCV
from sklearn.linear_model import LogisticRegression
# Now create the estimators: pipeline -> calibratedclassifier(pipeline)
pipeline = make_pipeline( TfidfVectorizer(), LogisticRegression() )
calibrated_pipeline = CalibratedClassifierCV( pipeline, cv=2 )
现在我们可以创建一个简单的训练集来检查分类器是否有效:
# Create text and labels arrays
text_array = np.array(['Why', 'is', 'this', 'happening'])
outputs = np.array([0,1,0,1])
当我试图适应 calibrated_pipeline 对象时,出现此错误:
ValueError: Found input variables with inconsistent numbers of samples: [1, 4]
如果你愿意,我可以复制整个异常跟踪,但这应该很容易重现。非常感谢!
编辑:我在创建数组时犯了一个错误。现已修复(感谢@ogrisel!)此外,调用:
pipeline.fit(text_array, outputs)
工作正常,但使用校准后的分类器会失败!
np.array(['Why', 'is', 'this', 'happening']).reshape(-1,1)
是一个二维字符串数组,而 docstring of the fit_transform method of the TfidfVectorizer class 表示它期望:
Parameters
----------
raw_documents : iterable
an iterable which yields either str, unicode or file objects
如果您遍历 2D numpy 数组,您会得到一系列一维字符串数组,而不是直接得到字符串:
>>> list(text_array)
[array(['Why'],
dtype='<U9'), array(['is'],
dtype='<U9'), array(['this'],
dtype='<U9'), array(['happening'],
dtype='<U9')]
所以修复很简单,只需通过:
text_documents = ['Why', 'is', 'this', 'happening']
作为矢量化器的原始输入。
Edit: 备注:默认情况下,LogisticRegression
几乎总是一个经过良好校准的分类器。在这种情况下,CalibratedClassifierCV
可能不会带来任何东西。