如何在带有作为参数的变压器的管道上执行gridsearch?

时间:2017-03-28 14:16:51

标签: methods parameters scikit-learn pipeline

我已经构建了一个提取某些功能然后执行随机梯度下降的管道。变换器采用方法作为参数。现在我想对SGD的参数执行gridsearch。但是我收到以下错误: pickle.PicklingError:不能发现:它找不到内置 .instancemethod

我认为它不会腌制,因为输入是一种方法。有没有办法可以编辑变压器以使其有效?

class extraFeature(BaseEstimator, TransformerMixin):
def __init__(self, func):
    self.func = func

def transform(self, X, y=None):
    #Use function passed in pipeline
    ret = [self.func(sentence) for sentence in X]
    ret = np.array(normalize(ret)[0]).reshape(len(ret),1)
    return ret

def fit(self, X, y=None):
    return self

Pipeline([
    ('features',FeatureUnion([
        ('words',Pipeline([   
        ('vect', CountVectorizer(ngram_range=(1,2))),                                           
        ('tfidf', TfidfTransformer(use_idf=True))                                               
        ])),
       ('fleshReadingEase',extraFeature(textstat.flesch_reading_ease)),
       ('smogIndex',extraFeature(textstat.smog_index))
    ])),
    ('clf',SGDClassifier(learning_rate='optimal',eta0=0.001,class_weight='balanced',loss='modified_huber', penalty='l2',alpha=1e-3, n_iter=5, random_state=42))
]) 

0 个答案:

没有答案