如何保存sklearn管道?无法获得<module'__main __'>上的'predictors'属性

时间:2019-09-11 08:46:13

标签: python scikit-learn pickle

我想保存sklearn管道。为此,我这样做

outfile = open('model', 'wb')
pickle.dump(pipe, outfile)

然后我关闭笔记本,再次打开它,然后尝试

outfile = open('model' ,'rb')
model = pickle.load(outfile)

但是我得到Can't get attribute 'predictors' on <module '__main__'>

我代码的相关部分:

# Custom transformer using spaCy
class predictors(TransformerMixin):
    def transform(self, X, **transform_params):
        # Cleaning Text
        return [clean_text(text) for text in X]

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

    def get_params(self, deep=True):
        return {}

# Basic function to clean the text
def clean_text(text):
    # Removing spaces and converting text into lowercase
    return text.strip().lower()

管道:

# Create pipeline using Bag of Words
pipe = Pipeline([("cleaner", predictors()),
                 ('vectorizer', tfidf_vector),
                 ('clf', clf)])

0 个答案:

没有答案