1 #加载模块 2 from sklearn import datasets 3 from sklearn.externals import joblib 4 from sklearn.linear_model import LinearRegression 5 from sklearn.cross_validation import train_test_split 6 #分割数据集 7 data_x,data_y = datasets.load_iris(return_X_y=True) 8 train_X,test_X,train_y,test_y = train_test_split(data_x,data_y,test_size=0.2,random_state=2,stratify=data_y) 9 #训练模型 10 lr = LinearRegression() 11 lr.fit(train_X,train_y) 12 13 #将训练的模型保存 14 direction = joblib.dump(lr,"H:/lr_model_20180911.pkl") 15 16 #下载模型 17 lr = joblib.load("".join(direction)) 18 #模型预测 19 lr.predict(test_X) 20 #重新设置模型参数并训练 21 lr.set_params(normalize=True).fit(train_X,train_y) 22 #新模型做预测 23 lr.predict(test_X)