利用sklearn中的支持向量机回归模型(linear,poly,rbf三种核)对boston房价进行预测并作出评估
2017-09-12 11:04
651 查看
from sklearn.datasets import load_boston boston = load_boston() print(boston.DESCR) import numpy as pd x = boston.data y = boston.target from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33) #从sklearn.preprocessing导入数据标准化模块 from sklearn.preprocessing import StandardScaler ss_x = StandardScaler() ss_y = StandardScaler() 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)) from sklearn.svm import SVR linear_svr = SVR(kernel='linear') linear_svr.fit(x_train, y_train.ravel()) linear_svr_predict = linear_svr.predict(x_test) poly_svr = SVR(kernel='poly') poly_svr.fit(x_train, y_train.ravel()) poly_svr_predict = poly_svr.predict(x_test) rbf_svr = SVR(kernel='rbf') rbf_svr.fit(x_train, y_train.ravel()) rbf_svr_predict = rbf_svr.predict(x_test) from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error print('The value of default measurement of linear SVR is', linear_svr.score(x_test, y_test)) print('R-squared value of linear SVR is', r2_score(y_test, linear_svr_predict)) print('The mean squared error of linear SVR is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(linear_svr_predict))) print('The mean absolute error of linear SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(linear_svr_predict))) print('\nThe value of default measurement of poly SVR is', poly_svr.score(x_test, y_test)) print('R-squared value of poly SVR is', r2_score(y_test, poly_svr_predict)) print('The mean squared error of poly SVR is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(poly_svr_predict))) print('The mean absolute error of poly SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(poly_svr_predict))) print('\nThe value of default measurement of rbf SVR is', rbf_svr.score(x_test, y_test)) print('R-squared value of rbf SVR is', r2_score(y_test, rbf_svr_predict)) print('The mean squared error of rbf SVR is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rbf_svr_predict))) print('The mean absolute error of rbf SVR is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(rbf_svr_predict)))
最终的运行结果如下:
Boston House Prices dataset
===========================
Notes
------
Data Set Characteristics:
:Number of Instances: 506
:Number of Attributes: 13 numeric/categorical predictive
:Median Value (attribute 14) is usually the target
:Attribute Information (in order):
- CRIM per capita crime rate by town
- ZN proportion of residential land zoned for lots over 25,000 sq.ft.
- INDUS proportion of non-retail business acres per town
- CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
- NOX nitric oxides concentration (parts per 10 million)
- RM average number of rooms per dwelling
- AGE
4000
proportion of owner-occupied units built prior to 1940
- DIS weighted distances to five Boston employment centres
- RAD index of accessibility to radial highways
- TAX full-value property-tax rate per $10,000
- PTRATIO pupil-teacher ratio by town
- B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
- LSTAT % lower status of the population
- MEDV Median value of owner-occupied homes in $1000's
:Missing Attribute Values: None
:Creator: Harrison, D. and Rubinfeld, D.L.
This is a copy of UCI ML housing dataset. http://archive.ics.uci.edu/ml/datasets/Housing
This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.
The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic
prices and the demand for clean air', J. Environ. Economics & Management,
vol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics
...', Wiley, 1980. N.B. Various transformations are used in the table on
pages 244-261 of the latter.
The Boston house-price data has been used in many machine learning papers that address regression
problems.
**References**
- Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.
- Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.
- many more! (see http://archive.ics.uci.edu/ml/datasets/Housing)
The value of default measurement of linear SVR is 0.65171709743
R-squared value of linear SVR is 0.65171709743
The mean squared error of linear SVR is 27.0063071393
The mean absolute error of linear SVR is 3.42667291687
The value of default measurement of poly SVR is 0.404454058003
R-squared value of poly SVR is 0.404454058003
The mean squared error of poly SVR is 46.179403314
The mean absolute error of poly SVR is 3.75205926674
The value of default measurement of rbf SVR is 0.756406891227
R-squared value of rbf SVR is 0.756406891227
The mean squared error of rbf SVR is 18.8885250008
The mean absolute error of rbf SVR is 2.60756329798
相关文章推荐
- 利用回归树对Boston房价进行预测,并对结果进行评估
- K近邻回归模型对Boston房价进行预测,同时对性能进行评估(1.使用普通的算术平均法2.考虑距离差异进行加权平均)
- 机器学习分类之结合实际应用介绍KNN算法原理以及利用sklearn进行分类预测
- 使用sklearn进行数据挖掘-房价预测(4)—数据预处理
- 利用sklearn做房价预测
- 分别用普通线性回归、岭回归、Lasso回归对boston房价进行预测,比较效果
- 使用三种继承回归模型对美国波士顿房价训练数据进行学习,并对测试数据进行预测
- Python多元线性回归-sklearn.linear_model,并对其预测结果评估
- 使用三种不同核函数配置的支持向量机(回归)模型对美国波士顿地区房价进行预测
- 如何利用机器学习预测房价?
- 利用KNC对iris数据进行分裂及预测
- TensorFlow 1.4利用Keras+Estimator API进行训练和预测
- keras系列︱迁移学习:利用InceptionV3进行fine-tuning及预测、完美案例(五)
- keras系列︱迁移学习:利用InceptionV3进行fine-tuning及预测、完美案例(五)
- 用深度神经网络对boston housing data进行回归预测的程序--tensorflow
- 在linux 内核中做开关变量的三种方法—— 利用proc 、sys文件系统,字符设备等与内核进行交互
- 分别用6种回归方法对波士顿房价进行预测
- 使用线性回归模型LinearRegression和SGDRegression分别对美国波士地区房价进行预测
- 利用Python sklearn的SVM对AT&T人脸数据进行人脸识别
- 蛙蛙推荐:利用Excel进行销售量预测