我试图训练一个决策树模型,保存它,并在以后需要时重新加载它。然而,我一直遇到以下错误:
此 DecisionTreeClassifier 实例尚未拟合。请在使用此方法之前调用 ‘fit’ 并提供适当的参数。
这是我的代码:
X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.20, random_state=4)names = ["Decision Tree", "Random Forest", "Neural Net"]classifiers = [ DecisionTreeClassifier(), RandomForestClassifier(), MLPClassifier() ]score = 0for name, clf in zip(names, classifiers): if name == "Decision Tree": clf = DecisionTreeClassifier(random_state=0) grid_search = GridSearchCV(clf, param_grid=param_grid_DT) grid_search.fit(X_train, y_train_TF) if grid_search.best_score_ > score: score = grid_search.best_score_ best_clf = clf elif name == "Random Forest": clf = RandomForestClassifier(random_state=0) grid_search = GridSearchCV(clf, param_grid_RF) grid_search.fit(X_train, y_train_TF) if grid_search.best_score_ > score: score = grid_search.best_score_ best_clf = clf elif name == "Neural Net": clf = MLPClassifier() clf.fit(X_train, y_train_TF) y_pred = clf.predict(X_test) current_score = accuracy_score(y_test_TF, y_pred) if current_score > score: score = current_score best_clf = clfpkl_filename = "pickle_model.pkl" with open(pkl_filename, 'wb') as file: pickle.dump(best_clf, file)from sklearn.externals import joblib# Save to file in the current working directoryjoblib_file = "joblib_model.pkl" joblib.dump(best_clf, joblib_file)print("best classifier: ", best_clf, " Accuracy= ", score)
这是我加载模型并测试它的方法:
#First methodwith open(pkl_filename, 'rb') as h: loaded_model = pickle.load(h) #Second method joblib_model = joblib.load(joblib_file)
如您所见,我尝试了两种保存方法,但都没有成功。
这是我的测试方法:
print(loaded_model.predict(test)) print(joblib_model.predict(test))
您可以清楚地看到,模型实际上是已经拟合的,如果我尝试使用其他模型如SVM或逻辑回归,该方法运作良好。
回答:
问题出在这行代码:
best_clf = clf
您将 clf
传递给了 grid_search
,它会克隆估计器并在这些克隆模型上拟合数据。因此,您的实际 clf
保持未触及和未拟合状态。
您需要的是
best_clf = grid_search
来保存已拟合的 grid_search
模型。
如果您不想保存 grid_search
的全部内容,您可以使用 grid_search
的 best_estimator_
属性来获取实际的克隆拟合模型。
best_clf = grid_search.best_estimator_