1. Read data and description
Datasets import load_boston from sklearn.datasets import load_bostonCopy the code
Print (boston.descr)Copy the code
Second, data segmentation and sample construction
Cross_validation import train_test_split import numpy as NP X=boston.data y=boston.target X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=33,test_size=0.25)Copy the code
Third, analyze the differences between target values
Print ("The Max target value is",np.max(boston.target)) print("The min target value is",np.min(boston.target)) print("The average target value is",np.mean(boston.target))Copy the code
Ss_X =StandardScaler() X_train= ss_x.fit_transform (X_train) X_test= ss_x.transform (X_test) X_train= ss_x.fit_transform (X_train) X_test= ss_x.transform (X_test) Y_train = ss_y. Fit_transform (y_train. Reshape (1, 1)) y_test = ss_y. Transform (y_test. Reshape (1, 1))Copy the code
Iv. Model training and evaluation
The LinearRegression model is used to train and forecast Boston housing price data respectively with SGDRegressor. Linear_model import LinearRegression data Lr. Fit (X_train,y_train) # predict=lr.predict(X_test) #SGDRegressor from SGDR =SGDRegressor(); sgdr.fit(X_train,y_train); Sgdr_y_predict =sgdr.predict(X_test) sgDR_y_predict =sgdr.predict(X_test)Copy the code
# Use the evaluation module that comes with the LinearRegression model, Print ("lr_score:", l.score (X_test,y_test)) # r-square,MSE,MAE evaluate LinearRegression performance from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error #r2_score print("r2_score:",r2_score(y_test,lr_y_predict)) #MSE print("MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict))) #MAE print("MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict)))Copy the code
# Use the evaluation module that comes with the LinearRegression model, Print ("sgdr_score:",sgdr.score(X_test,y_test)) # r-square,MSE,MAE evaluate LinearRegression performance from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error #r2_score print("r2_score:",r2_score(y_test,sgdr_y_predict)) #MSE print("MSE:",mean_squared_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(sgdr_y_predict))) #MAE print("MAE:",mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(sgdr_y_predict)))Copy the code