我尝试使用分类变量构建管道
import numpy as np
import pandas as pd
import sklearn
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn import linear_model
from sklearn.pipeline import Pipeline
df = pd.DataFrame({'a':range(6), 'c':['a', 'b', 'c']*2, 'd': ['m', 'f']*3 })
X = df[['c', 'd']]
y = df['a']
regressor = linear_model.SGDRegressor()
转换分类变量
class Cat(TransformerMixin):
def transform(self, X, **transform_params):
enc = DictVectorizer(sparse = False)
enc_data = enc.fit_transform(X.T.to_dict().values())
return enc_data
def fit(self, X, y=None, **fit_params):
return self
管道
pipeline = Pipeline([
('categorical', Cat()),
('model_fitting', regressor),
])
pipeline.fit(X, y)
这是正确的。但是当我尝试拟合新数据集时出现错误。例如
contr = pd.DataFrame({'c':['a'], 'd': ['m']})
pred = pipeline.predict(contr)
pred
and
ValueError: shapes (1,2) and (5,) not aligned: 2 (dim 1) != 5 (dim 0)
看到类 Cat(TransformerMixin) 中的问题。如何改进它?