在机器学习过程中,一般用来训练模型的过程比较长,所以我们一般会将训练的模型进行保存(持久化),然后进行评估,预测等等,这样便可以节省大量的时间。
在模型持久化过程中,我们使用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')