from sklearn.externals import joblib
- 保存:
joblib.dump(rf, 'test.pkl')
- 加载:
estimator = joblib.load('test.pkl')
eg:用岭回归的模型进行保存和加载
前面都一样
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
# 1.获取数据
boston = load_boston()
print("特征数量:\n",boston.data.shape)
# 2.划分数据集
x_train,x_test,y_train,y_test = train_test_split(boston.data,boston.target,random_state=22)
# 3.标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 岭回归对波士顿放假的预测优化方法
# 4.预估器
estimator = Ridge()
estimator.fit(x_train,y_train)
- 保存
# 保存模型
import joblib
joblib.dump(estimator,"my_ridge.pkl")
- 加载
# 加载模型
estimator = joblib.load("my_ridge.pkl")
# 5.得出模型
print("岭回归权重系数为:\n",estimator.coef_)
print("岭回归偏置为:\n",estimator.intercept_)
# 6.模型评估
y_predict = estimator.predict(x_test)
print("预测房价:\n",y_predict)
error = mean_squared_error(y_test,y_predict)
print("岭回归的均方误差为:\n",error)
最后的输出结果一样
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)