在機器學習過程中,一般用來訓練模型的過程比較長,所以我們一般會將訓練的模型進行保存(持久化),然後進行評估,預測等等,這樣便可以節省大量的時間。
在模型持久化過程中,我們使用scikit-learn提供的joblib.dump()方法,但是在使用過程中會出現很多問題。如我們使用如下語句:
-
from sklearn.externals
import joblib
- joblib.dump(clf,'../../data/model/randomforest.pkl')
然後,我們再使用joblib.load(‘../../data/model/randomforest.pkl’)進行加載.當設置參數時,模型持久化便會壓縮成一個文件。
以下是我們進行模型持久化的正確操作語句:
-
from sklearn.externals import joblib
-
- #save model
- joblib.dump(clf,'../../data/model/randomforest.pkl',compress=3)
- #load model to clf
- clf = joblib.load('../../data/model/randomforest.pkl')
<ol start="1" class="dp-py" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 18px; line-height: 26px; padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><pre name="code" class="python" style="color: rgb(51, 51, 51); font-size: 18px; line-height: 26px;">
from sklearn.externals import joblib
#load model to clf clf = joblib.load('../../data/model/randomforest.pkl')