scikit-learn使用joblib持久化模型過程中的問題詳解

在機器學習過程中,一般用來訓練模型的過程比較長,所以我們一般會將訓練的模型進行保存(持久化),然後進行評估,預測等等,這樣便可以節省大量的時間。


在模型持久化過程中,我們使用scikit-learn提供的joblib.dump()方法,但是在使用過程中會出現很多問題。如我們使用如下語句:

[python] view plaincopy
  1. from sklearn.externals import joblib
  2. joblib.dump(clf,'../../data/model/randomforest.pkl')  
此語句將產生大量的模型文件,如下圖所示


然後,我們再使用joblib.load(‘../../data/model/randomforest.pkl’)進行加載.當設置參數時,模型持久化便會壓縮成一個文件。

以下是我們進行模型持久化的正確操作語句:

[python] view plaincopy
  1. from sklearn.externals import joblib

  2. #save model  
  3. joblib.dump(clf,'../../data/model/randomforest.pkl',compress=3)  
  4. #load model to clf  
  5. 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')


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章