前言

sklearn模型的保存和加载API

  • from sklearn.externals import joblib
    • 保存:joblib.dump(estimator, ‘test.pkl’)
    • 加载:estimator = joblib.load(‘test.pkl’)

但是目前我电脑上的sklearn中已经移除了from sklearn.externals import joblib这个导入方法

sklearn.externals.joblib在0.21中已弃用,在0.23中被删除

现在保存和加载模型的api是单独的库

现在使用import joblib来导入api

dump与load

具体使用可以看下面的代码

代码举例:

保存模型

from skimage.metrics import mean_squared_error
from sklearn.datasets import load_boston
from sklearn.linear_model import RidgeCV, Ridge
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import joblib


def dump_load_demo():
# 加载数据
boston = load_boston()
# 数据分割,保证数据分割的一致性
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, train_size=0.2,random_state=22)
# 数据标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 模型训练
estimator = Ridge()
estimator.fit(x_train, y_train)

# 模型保存
joblib.dump(estimator, './data/test.pkl')

# 得出预测值
y_pre = estimator.predict(x_test)
# 模型评估
print("预测值 \n", y_pre)
score = estimator.score(x_test, y_test)
print("准确率\n", score)
print("偏置\n", estimator.intercept_)
print("系数\n", estimator.coef_)
ret = mean_squared_error(y_test, y_pre)
print("均方误差\n", ret)


if __name__ == '__main__':
dump_load_demo()

注:模型保存格式为pkl

运行结果:

预测值 
[23.64469576 27.97409384 17.55168799 28.80358367 17.99377237 17.16911389
18.08838244 18.5484773 18.39159086 30.11882442 19.02379724 26.14281853
12.91653465 16.42264835 33.05693686 16.30877287 6.37751888 16.82204542
28.50466918 22.99403455 16.57065921 28.10613493 27.20987256 15.88197605
29.39165138 23.83772485 28.28820203 26.13717053 15.34475384 17.2592326
24.96024219 11.92509187 31.5877362 4.22575655 13.85279407 15.45154778
4.20339965 19.19572767 36.43206568 27.38697842 23.16484782 15.50800593
32.61396957 4.76928343 20.61280174 22.98552067 18.00938256 17.47797758
15.65941555 21.53036514 8.07350341 23.7631832 27.89948024 12.60928859
6.00939238 29.66164389 25.73749604 22.21461849 15.86568871 21.70506535
22.32167003 21.54997175 18.75814107 34.35036868 23.93330189 16.4650795
10.29497693 4.81134268 37.24939583 21.04504805 14.40856753 21.70178409
33.82983682 21.38429774 30.8300285 23.97655944 21.06166321 17.23171262
24.16127528 22.93301994 27.46787702 19.09761525 21.58895637 26.70861218
23.91545352 18.99584879 25.89840907 21.06068455 22.76976898 16.54404802
22.34618208 20.5520392 17.00269605 11.11408577 10.80942164 16.88787311
21.36466922 14.08334209 18.34541038 22.49992866 16.6009481 14.76856955
22.98225728 20.82099466 18.09167202 31.25449524 14.7748573 21.37510721
29.48724174 30.27555742 19.35717062 23.41591865 18.92675356 14.08811569
19.50876796 20.75844963 24.66902078 23.28548242 21.37184963 12.61360164
11.84374119 1.97389286 26.75034249 18.36677866 20.22284006 25.65136456
26.76853678 21.01965862 24.76838538 19.2719079 0.20758938 33.66047857
33.62514941 24.10607586 25.27714009 19.36420085 32.79293209 12.7253308
16.4697096 10.07572615 34.63725968 17.51742591 30.81214014 22.7464453
7.47271236 22.0566537 26.5021439 29.02030002 14.90958575 6.34319256
21.72255731 15.312686 26.12292152 28.03322847 23.81482954 14.76050315
26.25298872 33.36534552 5.38378434 19.48867011 17.22952014 26.73584653
13.75318646 32.2262328 30.80102394 30.85734971 19.13265617 18.97112331
23.78360179 18.36212696 11.02997263 20.49077695 20.0010755 33.14860384
23.48461798 25.93285901 33.6286644 14.55281723 5.71533379 23.80222741
12.74528738 29.87644788 17.36057156 17.68386619 24.48905778 15.40371636
17.1681389 14.56256338 22.20362029 24.07938957 16.13989928 20.30403757
13.11742038 19.76011695 10.15959589 5.05451631 24.98773317 22.05933162
17.38635448 17.95889724 11.85983575 23.09467263 10.5421983 12.56199688
20.46920769 22.89551792 11.35987849 28.36158041 22.46150768 22.07052047
18.96479228 15.94762742 29.55993025 18.18280727 26.60883362 28.91338874
29.0243314 27.11588917 23.41267873 22.16089044 15.87275538 19.55923911
10.01955695 25.36241049 37.09399692 9.21076403 28.7250581 23.1210652
16.10488274 20.98532986 11.92937281 16.63409543 13.39946236 18.10252834
30.20768813 27.01016297 23.78388552 15.90408559 25.88867096 23.46240636
22.98550277 17.90243108 30.69975106 20.54447287 17.24878101 22.6405577
18.78662547 26.43378753 6.1415722 13.46113754 11.01274483 27.46925322
31.75050402 11.44302638 9.15277255 30.32661424 22.23993372 3.59762111
21.18431804 22.32873001 24.91183653 31.66081008 20.11883814 23.58860016
23.39144498 20.95717749 28.62287194 30.25468985 18.60827558 17.29159623
20.8459336 29.53135771 29.1735858 25.86670544 20.47761831 22.00175334
25.46210862 20.76591231 18.48931061 23.21360354 28.04039153 14.76240119
5.56366719 19.02971742 23.60960253 25.61011124 17.93010868 18.35299157
14.99314051 24.55852344 15.73398244 17.9936638 15.78653015 26.86927323
21.83288484 17.09786581 24.1455792 23.21781793 4.89715057 14.55190726
29.53097939 16.96942402 29.97092011 20.56134909 23.61695306 13.90547522
28.94524778 13.9490235 25.14367973 17.81456951 26.92882448 21.97947553
16.37959719 25.58285606 18.4004159 16.81711119 21.41056759 20.52708367
10.18563418 24.72279293 7.65824257 -5.75882408 28.92725713 15.23172694
-0.80822785 1.1885204 16.05592378 31.02687874 22.11833608 25.257107
21.11962573 27.17319156 14.23418317 35.29815094 16.76025589 23.88193103
11.49969608 11.005055 11.00317386 18.27016267 24.9635937 14.54733798
22.23443933 21.72653137 18.11347747 22.50164981 30.66773175 17.84627447
19.36076926 27.85693006 33.87880818 25.66176459 19.14387897 22.66753296
14.84930104 27.39448909 33.97151416 18.97839843 16.93696433 23.93610382
27.80296883 10.97564404 33.33567959 18.90876462 21.50046582 28.38223075
15.20285187 16.59362028 19.48364863 27.52004471 20.99718401 24.00002741
27.2015801 22.39667384 23.24447674 23.08634542 11.51804889 14.09160002
30.71290202 30.53124258 1.59429213 19.2223747 23.92469427 30.52913878
13.84632587 17.93078843 28.58855707 16.2168317 8.57194758 19.94632304
21.58821976 20.44036427 27.38200144 24.42298241 6.31193462 26.26904066
28.46064924 21.90708324 11.52479134 15.93255738 25.56393581 23.51065591
31.34782385 7.78903776 21.15999469]
准确率
0.6384683499205781
偏置
20.605940594059394
系数
[-0.91782309 -0.17382552 0.26572243 0.6906218 -2.23367615 1.81562646
0.39996906 -1.14481067 2.18240602 -0.79484838 -2.12801148 0.82343198
-4.1643924 ]
均方误差
32.38410204009493

加载模型

from skimage.metrics import mean_squared_error
from sklearn.datasets import load_boston
from sklearn.linear_model import RidgeCV, Ridge
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import joblib


def dump_load_demo():
# 加载数据
boston = load_boston()
# 数据分割,保证数据分割的一致性
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, train_size=0.2,random_state=22)
# 数据标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 模型训练
estimator = Ridge()
estimator.fit(x_train, y_train)

# 模型加载
estimator=joblib.load('./data/test.pkl')
# 得出预测值
y_pre = estimator.predict(x_test)
# 模型评估
print("预测值 \n", y_pre)
score = estimator.score(x_test, y_test)
print("准确率\n", score)
print("偏置\n", estimator.intercept_)
print("系数\n", estimator.coef_)
ret = mean_squared_error(y_test, y_pre)
print("均方误差\n", ret)


if __name__ == '__main__':
dump_load_demo()

运行结果:

预测值 
[23.64469576 27.97409384 17.55168799 28.80358367 17.99377237 17.16911389
18.08838244 18.5484773 18.39159086 30.11882442 19.02379724 26.14281853
12.91653465 16.42264835 33.05693686 16.30877287 6.37751888 16.82204542
28.50466918 22.99403455 16.57065921 28.10613493 27.20987256 15.88197605
29.39165138 23.83772485 28.28820203 26.13717053 15.34475384 17.2592326
24.96024219 11.92509187 31.5877362 4.22575655 13.85279407 15.45154778
4.20339965 19.19572767 36.43206568 27.38697842 23.16484782 15.50800593
32.61396957 4.76928343 20.61280174 22.98552067 18.00938256 17.47797758
15.65941555 21.53036514 8.07350341 23.7631832 27.89948024 12.60928859
6.00939238 29.66164389 25.73749604 22.21461849 15.86568871 21.70506535
22.32167003 21.54997175 18.75814107 34.35036868 23.93330189 16.4650795
10.29497693 4.81134268 37.24939583 21.04504805 14.40856753 21.70178409
33.82983682 21.38429774 30.8300285 23.97655944 21.06166321 17.23171262
24.16127528 22.93301994 27.46787702 19.09761525 21.58895637 26.70861218
23.91545352 18.99584879 25.89840907 21.06068455 22.76976898 16.54404802
22.34618208 20.5520392 17.00269605 11.11408577 10.80942164 16.88787311
21.36466922 14.08334209 18.34541038 22.49992866 16.6009481 14.76856955
22.98225728 20.82099466 18.09167202 31.25449524 14.7748573 21.37510721
29.48724174 30.27555742 19.35717062 23.41591865 18.92675356 14.08811569
19.50876796 20.75844963 24.66902078 23.28548242 21.37184963 12.61360164
11.84374119 1.97389286 26.75034249 18.36677866 20.22284006 25.65136456
26.76853678 21.01965862 24.76838538 19.2719079 0.20758938 33.66047857
33.62514941 24.10607586 25.27714009 19.36420085 32.79293209 12.7253308
16.4697096 10.07572615 34.63725968 17.51742591 30.81214014 22.7464453
7.47271236 22.0566537 26.5021439 29.02030002 14.90958575 6.34319256
21.72255731 15.312686 26.12292152 28.03322847 23.81482954 14.76050315
26.25298872 33.36534552 5.38378434 19.48867011 17.22952014 26.73584653
13.75318646 32.2262328 30.80102394 30.85734971 19.13265617 18.97112331
23.78360179 18.36212696 11.02997263 20.49077695 20.0010755 33.14860384
23.48461798 25.93285901 33.6286644 14.55281723 5.71533379 23.80222741
12.74528738 29.87644788 17.36057156 17.68386619 24.48905778 15.40371636
17.1681389 14.56256338 22.20362029 24.07938957 16.13989928 20.30403757
13.11742038 19.76011695 10.15959589 5.05451631 24.98773317 22.05933162
17.38635448 17.95889724 11.85983575 23.09467263 10.5421983 12.56199688
20.46920769 22.89551792 11.35987849 28.36158041 22.46150768 22.07052047
18.96479228 15.94762742 29.55993025 18.18280727 26.60883362 28.91338874
29.0243314 27.11588917 23.41267873 22.16089044 15.87275538 19.55923911
10.01955695 25.36241049 37.09399692 9.21076403 28.7250581 23.1210652
16.10488274 20.98532986 11.92937281 16.63409543 13.39946236 18.10252834
30.20768813 27.01016297 23.78388552 15.90408559 25.88867096 23.46240636
22.98550277 17.90243108 30.69975106 20.54447287 17.24878101 22.6405577
18.78662547 26.43378753 6.1415722 13.46113754 11.01274483 27.46925322
31.75050402 11.44302638 9.15277255 30.32661424 22.23993372 3.59762111
21.18431804 22.32873001 24.91183653 31.66081008 20.11883814 23.58860016
23.39144498 20.95717749 28.62287194 30.25468985 18.60827558 17.29159623
20.8459336 29.53135771 29.1735858 25.86670544 20.47761831 22.00175334
25.46210862 20.76591231 18.48931061 23.21360354 28.04039153 14.76240119
5.56366719 19.02971742 23.60960253 25.61011124 17.93010868 18.35299157
14.99314051 24.55852344 15.73398244 17.9936638 15.78653015 26.86927323
21.83288484 17.09786581 24.1455792 23.21781793 4.89715057 14.55190726
29.53097939 16.96942402 29.97092011 20.56134909 23.61695306 13.90547522
28.94524778 13.9490235 25.14367973 17.81456951 26.92882448 21.97947553
16.37959719 25.58285606 18.4004159 16.81711119 21.41056759 20.52708367
10.18563418 24.72279293 7.65824257 -5.75882408 28.92725713 15.23172694
-0.80822785 1.1885204 16.05592378 31.02687874 22.11833608 25.257107
21.11962573 27.17319156 14.23418317 35.29815094 16.76025589 23.88193103
11.49969608 11.005055 11.00317386 18.27016267 24.9635937 14.54733798
22.23443933 21.72653137 18.11347747 22.50164981 30.66773175 17.84627447
19.36076926 27.85693006 33.87880818 25.66176459 19.14387897 22.66753296
14.84930104 27.39448909 33.97151416 18.97839843 16.93696433 23.93610382
27.80296883 10.97564404 33.33567959 18.90876462 21.50046582 28.38223075
15.20285187 16.59362028 19.48364863 27.52004471 20.99718401 24.00002741
27.2015801 22.39667384 23.24447674 23.08634542 11.51804889 14.09160002
30.71290202 30.53124258 1.59429213 19.2223747 23.92469427 30.52913878
13.84632587 17.93078843 28.58855707 16.2168317 8.57194758 19.94632304
21.58821976 20.44036427 27.38200144 24.42298241 6.31193462 26.26904066
28.46064924 21.90708324 11.52479134 15.93255738 25.56393581 23.51065591
31.34782385 7.78903776 21.15999469]
准确率
0.6384683499205781
偏置
20.605940594059394
系数
[-0.91782309 -0.17382552 0.26572243 0.6906218 -2.23367615 1.81562646
0.39996906 -1.14481067 2.18240602 -0.79484838 -2.12801148 0.82343198
-4.1643924 ]
均方误差
32.38410204009493

这样代码运行的结果都是一样的

注:在加载和保存模型时,为了保证运行结果的一致性,在数据集分割的时候,需要指定随机数种子也就是random_state这个参数必须写

不然保存或者加载时如果结果不一致,那保存的意义也就不大了

本文完