标签:show div 数据集 split his cti print bsp pre
二维线性回归(作了可视化处理):
1 from sklearn.linear_model import LinearRegression 2 import numpy as np 3 import matplotlib.pyplot as plt 4 X=[[1],[4],[3]] 5 y=[3,5,3] 6 lr=LinearRegression() 7 model=lr.fit(X,y) 8 z=np.linspace(0,5,20) 9 plt.scatter(X,y,s=80) 10 plt.plot(z,model.predict(z.reshape(-1,1)),c=‘k‘) 11 plt.title(‘Linear Regression‘) 12 print("y={:.3f}x".format(model.coef_[0])+‘+{:.3f}‘.format(model.intercept_)) 13 plt.show()
1 from sklearn.datasets import make_regression 2 X,y=make_regression(n_samples=50,n_features=1,n_informative=1,noise=50,random_state=1) 3 reg=LinearRegression() 4 reg.fit(X,y) 5 z=np.linspace(-3,3,200).reshape(-1,1) 6 plt.scatter(X,y,c=‘b‘,s=60) 7 plt.plot(z,reg.predict(z),c=‘k‘) 8 plt.title("Linear Regression2") 9 print("y={:.3f}x".format(reg.coef_[0])+‘+{:.3f}‘.format(reg.intercept_)) 10 plt.show()
多维线性回归(分别使用了自造回归数据和糖尿病人的数据集):
1 from sklearn.model_selection import train_test_split 2 from sklearn.linear_model import LinearRegression 3 from sklearn.datasets import make_regression 4 X,y=make_regression(n_samples=100,n_features=2,n_informative=2,random_state=38) 5 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8) 6 lr=LinearRegression().fit(X_train,y_train) 7 print("the coefficient:{}".format(lr.coef_)) 8 print(‘the intercept:{}‘.format(lr.intercept_)) 9 print("the score of this model:{:.3f}".format(lr.score(X_test,y_test)))
1 from sklearn.datasets import load_diabetes 2 X,y=load_diabetes().data,load_diabetes().target 3 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=8) 4 lr=LinearRegression().fit(X_train,y_train) 5 print("the coefficient:{}".format(lr.coef_)) 6 print(‘the intercept:{}‘.format(lr.intercept_)) 7 print("the score of this model:{:.3f}".format(lr.score(X_test,y_test)))
标签:show div 数据集 split his cti print bsp pre
原文地址:https://www.cnblogs.com/St-Lovaer/p/12245925.html