The Road to Machine Learning: Python Support Vector Machine Regression SVR Predicting Housing Prices in the Boston Area

 

python3 learn to use api

Two kernel function models of support vector machine for prediction

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

 

from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
import numpy as np

# 1 Prepare data 
#Read the housing price information in the Boston area 
boston = load_boston() #View the
 data description 
# print(boston.DESCR) # A total of 506 pieces of housing price information in the Boston area, each with 13 numerical feature descriptions and target housing prices 
# View the data Differences 
# print("Maximum house price:", np.max(boston.target)) # 50 
# print("Minimum house price:",np.min(boston.target)) # 5 
# print("Average house price:" , np.mean(boston.target)) # 22.532806324110677 

x = boston.data
y = boston.target

# 2 Split training data and test data 
# randomly sample 25% as test 75% as training 
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33 )


# 3 Standardize training data and test data 
ss_x = StandardScaler()
x_train = ss_x.fit_transform(x_train)
x_test = ss_x.transform(x_test)

ss_y = StandardScaler()
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.transform(y_test.reshape(-1, 1))

# 4.1 Support vector machine model for learning and prediction 
#Linear kernel function configuration support vector machine 
linear_svr = SVR(kernel= " linear " )
 #training linear_svr.fit 
(x_train, y_train) #prediction
 save prediction results 
linear_svr_y_predict = linear_svr.predict(x_test )

#Polynomial kernel function configuration support vector machine 
poly_svr = SVR(kernel= " poly " )
 #training poly_svr.fit 
(x_train, y_train) #predict and
 save the prediction result 
poly_svr_y_predict = linear_svr.predict(x_test)

# 5 Model evaluation 
# Linear kernel function model evaluation 
print ( " The default evaluation value of the linear kernel function support vector machine is: " , linear_svr.score(x_test, y_test))
 print ( " The R_squared value of the linear kernel function support vector machine is: " , r2_score(y_test, linear_svr_y_predict))
 print ( " The mean squared error of the linear kernel function SVM is: " , mean_squared_error(ss_y.inverse_transform(y_test),
                                              ss_y.inverse_transform(linear_svr_y_predict)))
print ( " The mean absolute error of the linear kernel function SVM is: " , mean_absolute_error(ss_y.inverse_transform(y_test),
                                                 ss_y.inverse_transform(linear_svr_y_predict)))
#Evaluate the polynomial kernel function model 
print ( " The default evaluation value of the polynomial kernel function is: " , poly_svr.score(x_test, y_test))
 print ( " The R_squared value of the polynomial kernel function is: " , r2_score(y_test, poly_svr_y_predict ))
 print ( " The mean squared error for the polynomial kernel function is: " , mean_squared_error(ss_y.inverse_transform(y_test),
                                           ss_y.inverse_transform(poly_svr_y_predict)))
print ( " The mean absolute error for the polynomial kernel function is: " , mean_absolute_error(ss_y.inverse_transform(y_test),
                                              ss_y.inverse_transform(poly_svr_y_predict)))

'''
The default evaluation value for a linear kernel SVM is: 0.651717097429608
The R_squared value of the Linear Kernel Support Vector Machine is: 0.651717097429608
The mean square error of the linear kernel function SVM is: 27.0063071393243
The mean absolute error of the linear kernel SVM is: 3.426672916872753
The default evaluation value for the polynomial kernel function is: 0.40445405800289286
The R_squared value for the polynomial kernel function is: 0.651717097429608
The mean squared error for the polynomial kernel is: 27.0063071393243
The mean absolute error for the polynomial kernel function is: 3.426672916872753
'''

 

Guess you like

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