机器学习之二用sk-learn实现波士顿房价预测(单变量)

使用sk-learn进行波士顿房价预测(单变量)

文章目录

  • 使用sk-learn进行波士顿房价预测(单变量)
      • 1、预测过程
      • 2、回归性能评价
      • 3、代码

1、预测过程

(1)、波士顿地区房价数据获取,数据来自于sklearn自带数据集;
(2)、波士顿地区房价数据分割;
(3)、训练与测试数据标准化处理;
(4)、使用最简单的线性回归模型LinearRegression对房价进行预测。

2、回归性能评价

MSE(Mean Squared Error):均方误差。 是真实值与预测值的差值的平方和的平均值。常被用作线性回归的损失函数。
在这里插入图片描述
MAE(Mean Absolute Error ):平均绝对误差。 是预测值和真实值差的平均值。
在这里插入图片描述
RMSE(Root Mean Square Error):均方根误差。 是预测值与真实值偏差的平方和的均值的平方根
在这里插入图片描述
r2_score(R Squared):决定系数。 将预测值跟只使用均值的情况下相比,看能好多少。该数字越接近于1,模型越好

在这里插入图片描述

3、代码

import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error #平方绝对误差


boston = datasets.load_boston()
print(boston.DESCR)       #获得关于房价的描述信息
x = boston.data       #获得数据集的特征属性列
y = boston.target       #获得数据集的label列
df = pd.DataFrame(data = np.c_[x,y],columns=np.append(boston.feature_names,['MEDV'])) #np.c_是按列连接两个矩阵,就是把两矩阵左右相加,要求列数相等
df = df[['RM','MEDV']]      #选择房间数属性列和房价属性列
print(df[:5])         #查看前5行的数据格式

x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.4)     #划分数据集

scaler = StandardScaler()        #作用:去均值和方差归一化。可保存训练集中的均值、方差参数,然后直接用于转换测试集数据。
x_train = scaler.fit_transform(x_train)
x_test = scaler.fit_transform(x_test)

linreg = LinearRegression()
model = linreg.fit(x_train,y_train)

print("MSE均方误差:",mean_squared_error(y_train,model.predict(x_train)))
print("RMSE均方根误差:",mean_squared_error(y_train,model.predict(x_train)) ** 0.5)
print("MAE平均绝对误差:",mean_absolute_error(y_train,model.predict(x_train)))
print("r2_score决定系数:",r2_score(y_train,model.predict(x_train)))

输出:

C:\Users\Admin\Anaconda3\python.exe "E:/2019/May 1/boston.py"
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      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)

      RM  MEDV
0  6.575  24.0
1  6.421  21.6
2  7.185  34.7
3  6.998  33.4
4  7.147  36.2
MSE均方误差: 20.43961203750119
RMSE均方根误差: 4.521018915853061
MAE平均绝对误差: 3.1345166771782367
r2_score决定系数: 0.7419672307888006

Process finished with exit code 0

你可能感兴趣的:(机器学习)