0

我有一個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個分類組合?

回答

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) 
+0

這就是我恰恰不想做的事。因爲我必須複製該行16 +型號:) –

+0

並使用1,2,3等 –

+0

它確實發佈你的追蹤 – marmouset