【问题标题】:SKLearn Pipeline w/ ColumnTransformer: 'numpy.ndarray' object has no attribute 'lower'带有 ColumnTransformer 的 SKLearn 管道:'numpy.ndarray' 对象没有属性'lower'
【发布时间】:2019-06-29 00:37:32
【问题描述】:

我正在尝试使用 SKLearn 0.20.2 来制作管道,同时使用新的 ColumnTransformer 功能。我的问题是我不断收到错误:

AttributeError: 'numpy.ndarray' object has no attribute 'lower'

我有一列名为 text 的文本块。我所有的其他专栏本质上都是数字的。我正在尝试在我的管道中使用Countvectorizer,我认为这就是问题所在。非常感谢您的帮助。

from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
# plus other necessary modules

# mapped to column names from dataframe
numeric_features = ['hasDate', 'iterationCount', 'hasItemNumber', 'isEpic']
numeric_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='median'))
])

# mapped to column names from dataframe
text_features = ['text']
text_transformer = Pipeline(steps=[
    ('imputer', SimpleImputer(strategy='most_frequent”')),
    ('vect', CountVectorizer())
])

preprocessor = ColumnTransformer(
    transformers=[('num', numeric_transformer, numeric_features),('text', text_transformer, text_features)]
)

clf = Pipeline(steps=[('preprocessor', preprocessor),
                      ('classifier', MultinomialNB())
                     ])

x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.33)
clf.fit(x_train,y_train)

【问题讨论】:

  • SimpleImputer 不适用于文本。试试text_transformer = Pipeline([('vect', CountVectorizer())]) 看看会发生什么。
  • 谢谢@SergeyBushmanov!现在我有一个新错误:ValueError: all the input array dimensions except for the concatenation axis must match exactly。我将更新我的 sn-p 以删除 imputer。
  • @SergeyBushmanov 实际上,我继续将有问题的代码放回去,并在您的说明中留下了答案,因为它确实修复了初始错误。
  • 就您的最新错误而言。你能追踪吗,例如使用fit_transform 方法,preprocessorclf 哪一行会产生错误?
  • @SergeyBushmanov 我将此问题标记为已回答,因为您确实为我提供了该错误的解决方案。我在这里开始了一个新问题,其中包含新错误的详细信息:stackoverflow.com/questions/54541490/…

标签: python scikit-learn pipeline


【解决方案1】:

@SergeyBushmanov 帮助我诊断了标题中的错误,它是由在文本上运行 SimpleImputer 引起的。

我还有一个错误,我会写一个新问题。

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2020-09-22
    • 2014-12-09
    • 2020-10-06
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2021-08-23
    相关资源
    最近更新 更多