The Road to Machine Learning: Python Linear Regression LinearRegression, Random Parameter Regression SGDRegressor Predicting Boston Housing Prices

 

python3 learn to use api

Linear regression, and random parameter regression

git: https://github.com/linyi0604/MachineLearning

 

 1 from sklearn.datasets import load_boston
 2 from sklearn.cross_validation import train_test_split
 3 from sklearn.preprocessing import StandardScaler
 4 from sklearn.linear_model import LinearRegression, SGDRegressor
 5 from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
 6 import numpy as np
 7 
 8 # 1 准备数据
 9 # 读取波士顿地区房价信息
10 boston =load_boston()
 11  #View the data description 
12  # print(boston.DESCR) # A total of 506 pieces of housing price information in the Boston area, each with 13 numerical feature descriptions and the target house price 
13  #Check the difference between the data 14 # print("Maximum house price: ", np.max(boston.target)) # 50 15 # print("Minimum house price:",np.min(boston.target)) # 5 16 # print("Average house price:", np.mean(boston. target)) # 22.532806324110677 17 18 x = boston.data
 19 y = boston.target
 20 21 # 2 split training data and test data 22 # randomly sample 25% as test 75% as training 23
 
 
 
 
 
 
 
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33 )
 24  
25  
26  # 3 Normalize training data and test data 
27 ss_x = StandardScaler()
 28 x_train = ss_x.fit_transform(x_train)
 29 x_test = ss_x.transform(x_test)
 30  
31 ss_y = StandardScaler()
 32 y_train = ss_y.fit_transform(y_train.reshape(-1, 1 ))
 33 y_test = ss_y.transform(y_test.reshape(-1, 1 ))
 34  
35  # 4 Use two linear regression models for training and prediction 
36  #Initialize LinearRegression linear regression model 
37lr = LinearRegression ()
 38  #training 39 lr.fit 
(x_train, y_train)
 40 #predict and save the prediction result 41 lr_y_predict = lr.predict(x_test)
 42 43 #initialize SGDRRegressor stochastic gradient regression model 44 sgdr = SGDRegressor ()
 45 #training 46 sgdr.fit(x_train, y_train)
 47 #Predict and save the prediction result 48 sgdr_y_predict = sgdr.predict(x_test)
 49 50 # 5 Model evaluation 51 #Evaluate the Linear model 52 lr_score = lr.score(x_test, y_test)
  
 
 
 
  
 
 
 
53  print ( " The default evaluation value for Linear is: " , lr_score)
 54 lr_R_squared = r2_score(y_test, lr_y_predict)
 55  print ( " The R_squared value for Linear is: " , lr_R_squared)
 56 lr_mse = mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(lr_y_predict))
 57  print ( " Linear mean square error is: " , lr_mse)
 58 lr_mae = mean_absolute_error(ss_y.inverse_transform(y_test),ss_y.inverse_transform(lr_y_predict))
 59  print ( " Linear mean absolute error for: ", lr_mae)
 60  
61  #Evaluate the SGD model 
62 sgdr_score = sgdr.score(x_test, y_test)
 63  print ( " The default evaluation value of SGD is: " , sgdr_score)
 64 sgdr_R_squared = r2_score(y_test, sgdr_y_predict)
 65  print ( " SGD The R_squared value is: " , sgdr_R_squared)
 66 sgdr_mse = mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict))
 67  print ( " The mean squared error of SGD is: " , sgdr_mse)
 68 sgdr_mae =mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_predict))
 69  print ( " The mean absolute error of SGD is: " , sgdr_mae)
 70  
71  ''' 
72  Linear's default evaluation value: 0.6763403830998702
 73  Linear's R_squared value is: 0.6763403830998701
 74  Linear mean square error: 25.09698569206773
 75  Linear mean absolute error is: 3.5261239963985433
 76  
77  default evaluation value of SGD: .659795654161198
 78  SGD values of R_squared: .659795654161198
 79  SGD mean square error: 26.379885392159494
 80  SGD The mean absolute error is: 3.5094445431026413
 81  '''

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325067044&siteId=291194637