【发布时间】: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方法,preprocessor或clf哪一行会产生错误? -
@SergeyBushmanov 我将此问题标记为已回答,因为您确实为我提供了该错误的解决方案。我在这里开始了一个新问题,其中包含新错误的详细信息:stackoverflow.com/questions/54541490/…
标签: python scikit-learn pipeline