【问题标题】:Reuse a logistic regression object for different fitted models为不同的拟合模型重用逻辑回归对象
【发布时间】:2016-12-24 04:02:02
【问题描述】:

我有一个 Pipeline 对象,我想将其放入训练和测试标签的不同组合中,因此使用 fit 对象创建不同的预测。但我相信fit 使用相同的分类器对象会摆脱以前的fit 对象。

我的代码示例是:

text_clf = Pipeline([('vect', CountVectorizer(analyzer="word",tokenizer=None,preprocessor=None,stop_words=None,max_features=5000)),
                          ('tfidf', TfidfTransformer(use_idf=True,norm='l2',sublinear_tf=True)),
                          ('clf',LogisticRegression(solver='newton-cg',class_weight='balanced', multi_class='multinomial',fit_intercept=True),
                          )])

    print "Fitting the open multinomial BoW logistic regression model for probability models...\n"
    open_multi_logit_words = text_clf.fit(train_wordlist, train_property_labels)

    print "Fitting the open multinomial BoW logistic regression model w/ ",threshold," MAPE threshold...\n"
    open_multi_logit_threshold_words = (text_clf.copy.deepcopy()).fit(train_wordlist, train_property_labels_threshold)

但是,分类器对象没有deepcopy() 方法。如何在无需定义的情况下实现我所需要的:

text_clf_open_multi_logit = Pipeline([('vect', CountVectorizer(analyzer="word",tokenizer=None,preprocessor=None,stop_words=None,max_features=5000)),
                              ('tfidf', TfidfTransformer(use_idf=True,norm='l2',sublinear_tf=True)),
                              ('clf',LogisticRegression(solver='newton-cg',class_weight='balanced', multi_class='multinomial',fit_intercept=True),
                              )])

对于我所有的 16 个分类器组合?

【问题讨论】:

    标签: python scikit-learn logistic-regression text-classification


    【解决方案1】:

    我会试试的

    text_clf0=copy.deepcopy(text_clf)
    open_multi_logit_threshold_words = text_clf0.fit(train_wordlist, train_property_labels_threshold)
    

    编辑:您可以使用列表

    text_clf_list=[copy.deepcopy(text_clf) for _ in range(16)]
    

    或直接

    copy.deepcopy(text_c‌​lf).fit(train_wordlis‌​t, train_property_label‌​s_threshold) 
    

    【讨论】:

    • 这正是我不想做的。因为我必须为 16 多个模型复制该行:)
    • 并使用1,2,3等
    • 确实如此,发布您的回溯
    • 检查过了!我很抱歉@marmouset
    猜你喜欢
    • 2022-10-04
    • 2023-04-03
    • 1970-01-01
    • 2021-04-17
    • 2019-06-01
    • 2015-04-26
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多