[英]Is it possible to reuse a sklearn model pickled in an other programm that doesn't have sklearn?
[英]python sklearn pickled model doesn't have the same number of features
我使用sklearn創建了一個SVC模型並將其腌制:
clf=LinearSVC(loss='l2', dual=False, tol=1e-3)
clf.fit(X_train, y_train)
#model_file_name='classify_pages_model'
with open('our_classifier.pkl', 'wb') as fid:
cPickle.dump(clf, fid)
我嘗試加載它並在另一個文件中使用它,
with open('our_classifier.pkl', 'rb') as fid:
clf = cPickle.load(fid)
X_test=tfidf_vectorizer.fit_transform((get_text(f) for f in urls))
pred=clf.predict(X_test)
它給了我這個錯誤:
ValueError:X每個樣本具有664個功能; 期待47387
如何確保測試文檔中的功能與模型中的功能相同?
- - 編輯
當我在同一代碼中進行訓練和測試時,不會發生此問題(但僅當我對模型進行酸洗並從另一個代碼中加載它時)
以下代碼正常工作,但是當我給clf腌制時,由於X_test中的功能數量與clf中的功能數量不同,所以我無法執行測試部分
1 - 培訓
X_train=tfidf_vectorizer.fit_transform((read(f) for f in train_files_paths))
clf=LinearSVC(loss='l2', dual=False, tol=1e-3)
clf.fit(X_train, y_train)
2-測試
X_test=tfidf_vectorizer.transform((get_text(f) for f in urls))
pred=clf.predict(X_test)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.