我使用sklearn.linear_model.LinearRegression
保存pickle
的模型,然后使用以下方式反向加载:
i = 1
Xtr_num, Xte_num, Xtr_cat, Xte_cat, Ytr, Yte, Xvl_num, Xvl_cat, Yvl = data.get_splitted_data(i)
lm = linear_model.LinearRegression()
pickle.dump(lm, open('pre_model/y'+str(i)+'-skl.pickle.sav', 'wb'))
X = np.concatenate((Xtr_num, Xtr_cat), axis=1)
model = lm.fit(X,Ytr)
loaded_model = pickle.load(open('pre_model/y'+str(1)+'-skl.pickle.sav', 'rb'))
print loaded_model.predict(X)
它给出了以下错误。你能给些建议么。
---------------------------------------------------------------------------
NotFittedError Traceback (most recent call last)
<ipython-input-14-8d246f04f2b7> in <module>()
1 loaded_model = pickle.load(open('pre_model/y'+str(1)+'-skl.pickle.sav', 'rb'))
----> 2 print loaded_model.predict(X)
/usr/lib64/python2.7/site-packages/sklearn/linear_model/base.pyc in predict(self, X)
254 Returns predicted values.
255 """
--> 256 return self._decision_function(X)
257
258 _preprocess_data = staticmethod(_preprocess_data)
/usr/lib64/python2.7/site-packages/sklearn/linear_model/base.pyc in _decision_function(self, X)
235
236 def _decision_function(self, X):
--> 237 check_is_fitted(self, "coef_")
238
239 X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
/usr/lib64/python2.7/site-packages/sklearn/utils/validation.pyc in check_is_fitted(estimator, attributes, msg, all_or_any)
766
767 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 768 raise NotFittedError(msg % {'name': type(estimator).__name__})
769
770
NotFittedError: This LinearRegression instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
答案 0 :(得分:1)
尝试加载模型时,但是当您调用predict
时,不会出现错误。您需要首先使用一些训练数据来拟合您的模型。之后你可以预测:
loaded_model.fit(training_data, target_values)
loaded_model.predict(X)
更新
为了澄清,我将重写您的代码。看看它现在是否有效:
i = 1
Xtr_num, Xte_num, Xtr_cat, Xte_cat, Ytr, Yte, Xvl_num, Xvl_cat, Yvl = data.get_splitted_data(i)
lm = linear_model.LinearRegression()
X = np.concatenate((Xtr_num, Xtr_cat), axis=1)
lm.fit(X,Ytr) # the position of this line has been changed
pickle.dump(lm, open('pre_model/y'+str(i)+'-skl.pickle.sav', 'wb'))
loaded_model = pickle.load(open('pre_model/y'+str(1)+'-skl.pickle.sav', 'rb'))
print loaded_model.predict(X)