我想基于我拥有的不同特征(文本和数字)进行二进制分类。训练数据是熊猫数据框的形式。我的管道看起来是这样的:Sklearn:FeatureUnion的异质特性给管道中的分类器带来不兼容的行尺寸误差
final_pipeline = Pipeline([('union', FeatureUnion(
transformer_list=[('body_trans', Pipeline([('selector', ItemSelector(key='body')),
('count_vect', CountVectorizer())])),
('body_trans2', Pipeline([('selector', ItemSelector(key='body2')),
('count_vect', TfidfVectorizer())])),
('length_trans', Pipeline([('selector', ItemSelector(key='length')),
('min_max_scaler', MinMaxScaler())]))],
transformer_weights={'body_trans': 1.0,'body_trans2': 1.0,'length_trans': 1.0})),
('svc', SVC())])
ItemSelector看起来是这样的:
class ItemSelector(BaseEstimator, TransformerMixin):
def __init__(self, key):
self.key = key
def fit(self, x, y=None):
return self
def transform(self, data_frame):
return data_frame[[self.key]]
现在,当我尝试final_pipeline.fit(X_train, y_train)
,它给我的ValueError: blocks[0,:] has incompatible row dimensions
例外。
X_train, X_test, y_train, y_test = train_test_split(train_set, target_set)
是我如何得到我的训练数据。 train_set
是一个数据框,其字段为body
,body2
,length
等。target_set
是一个数据帧,只有一个字段叫做label
,这是我的实际分类标签。
编辑:
我觉得我的数据输入到管道是不正确的格式。
train_set
是我与功能训练数据,样本:
body length body2
0 blah-blah 193 blah-blah-2
1 blah-blah-blah 153 blah-blah-blah-2
和target_set
,这与分类标签
label
0 True
1 False
数据框如果对输入格式的任何教程一个Pipeline的使用DataFrames的拟合参数,请给我提供一个链接!我找不到有关如何将DataFrame作为管道输入加载的适当文档,同时将多个列用作单独的功能。
任何帮助表示赞赏!
请发布一些示例数据,并容易复制和运行代码以及完整的堆栈跟踪错误。 –
已添加一些数据样本!谢谢 – void
问题出在你的ItemSelector中。它输出一个2-d数据帧,但CountVectorizer和TfidfVectorizer需要一个1-d字符串数组。 –