第9关 老司机带你去波士顿买房_机器学习实战项目_人工智能课程 - 小象学院

课程目录 小象学院 - 人工智能

关注公众号【Python家庭】领取1024G整套教材交流群学习商务合作。整理分享了数套四位数培训机构的教材,现免费分享交流学习,并提供解答、交流群

你要的白嫖教程,这里可能都有喔~

 

案例引入

老司机最近买彩票中了5块钱,感觉自己跟中了500万似的,又开始飘了,这次他要去美国的波士顿看看房,今天你就跟着老司机一起去波士顿转悠转悠吧,听说波士顿龙虾不错^_^

老司机对波士顿不是很了解,怕看房的时候被中介骗,所以在看房之前过关系搞到了一批波士顿当地的房价数据,老司机准备先分析下房价数据,对波士顿房价有个大体的了解,然后再去看房。

接下来跟着老司机开始折腾吧,go! go! go!

在波士顿房价的数据集里,总共收集了506套房子的数据,每套房子有13个特征,具体如下:

  • CRIM:城镇人均犯罪率;

  • ZN:城镇超过25,000平方英尺的住宅区域的占地比例;

  • INDUS:城镇非零售用地占地比例;

  • CHAS:是否靠近河边,1 靠近,0 远离;

  • NOX:一氧化氮浓度;

  • RM:每套房产的平均房间个数;

  • AGE:在1940年之前就盖好,且业主自住的房子的比例;

  • DIS:与波士顿市中心的距离;

  • RAD:周边高速公路的便利性指数;

  • TAX:每10,000美元的财产税率;

  • PTRATIO:小学老师的比例;

  • B:城镇黑人的比例;

  • LSTAT:地位较低的人口比例。

导入sklearn中的房价数据集

引入sklearn机器学习库的datasets数据集,并加载波士顿房价数据。

AI_9_0_1

import numpy as np
from sklearn import datasets 

# 加载sklearn自带的波士顿房价数据集
boston = datasets.load_boston()
{'data': array([[6.3200e-03, 1.8000e+01, 2.3100e+00, ..., 1.5300e+01, 3.9690e+02,
         4.9800e+00],
        [2.7310e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9690e+02,
         9.1400e+00],
        [2.7290e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9283e+02,
         4.0300e+00],
        ...,
        [6.0760e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,
         5.6400e+00],
        [1.0959e-01, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9345e+02,
         6.4800e+00],
        [4.7410e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,
         7.8800e+00]]),
 'target': array([24. , 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15. ,
        18.9, 21.7, 20.4, 18.2, 19.9, 23.1, 17.5, 20.2, 18.2, 13.6, 19.6,
        15.2, 14.5, 15.6, 13.9, 16.6, 14.8, 18.4, 21. , 12.7, 14.5, 13.2,
        13.1, 13.5, 18.9, 20. , 21. , 24.7, 30.8, 34.9, 26.6, 25.3, 24.7,
        21.2, 19.3, 20. , 16.6, 14.4, 19.4, 19.7, 20.5, 25. , 23.4, 18.9,
        35.4, 24.7, 31.6, 23.3, 19.6, 18.7, 16. , 22.2, 25. , 33. , 23.5,
        19.4, 22. , 17.4, 20.9, 24.2, 21.7, 22.8, 23.4, 24.1, 21.4, 20. ,
        20.8, 21.2, 20.3, 28. , 23.9, 24.8, 22.9, 23.9, 26.6, 22.5, 22.2,
        23.6, 28.7, 22.6, 22. , 22.9, 25. , 20.6, 28.4, 21.4, 38.7, 43.8,
        33.2, 27.5, 26.5, 18.6, 19.3, 20.1, 19.5, 19.5, 20.4, 19.8, 19.4,
        21.7, 22.8, 18.8, 18.7, 18.5, 18.3, 21.2, 19.2, 20.4, 19.3, 22. ,
        20.3, 20.5, 17.3, 18.8, 21.4, 15.7, 16.2, 18. , 14.3, 19.2, 19.6,
        23. , 18.4, 15.6, 18.1, 17.4, 17.1, 13.3, 17.8, 14. , 14.4, 13.4,
        15.6, 11.8, 13.8, 15.6, 14.6, 17.8, 15.4, 21.5, 19.6, 15.3, 19.4,
        17. , 15.6, 13.1, 41.3, 24.3, 23.3, 27. , 50. , 50. , 50. , 22.7,
        25. , 50. , 23.8, 23.8, 22.3, 17.4, 19.1, 23.1, 23.6, 22.6, 29.4,
        23.2, 24.6, 29.9, 37.2, 39.8, 36.2, 37.9, 32.5, 26.4, 29.6, 50. ,
        32. , 29.8, 34.9, 37. , 30.5, 36.4, 31.1, 29.1, 50. , 33.3, 30.3,
        34.6, 34.9, 32.9, 24.1, 42.3, 48.5, 50. , 22.6, 24.4, 22.5, 24.4,
        20. , 21.7, 19.3, 22.4, 28.1, 23.7, 25. , 23.3, 28.7, 21.5, 23. ,
        26.7, 21.7, 27.5, 30.1, 44.8, 50. , 37.6, 31.6, 46.7, 31.5, 24.3,
        31.7, 41.7, 48.3, 29. , 24. , 25.1, 31.5, 23.7, 23.3, 22. , 20.1,
        22.2, 23.7, 17.6, 18.5, 24.3, 20.5, 24.5, 26.2, 24.4, 24.8, 29.6,
        42.8, 21.9, 20.9, 44. , 50. , 36. , 30.1, 33.8, 43.1, 48.8, 31. ,
        36.5, 22.8, 30.7, 50. , 43.5, 20.7, 21.1, 25.2, 24.4, 35.2, 32.4,
        32. , 33.2, 33.1, 29.1, 35.1, 45.4, 35.4, 46. , 50. , 32.2, 22. ,
        20.1, 23.2, 22.3, 24.8, 28.5, 37.3, 27.9, 23.9, 21.7, 28.6, 27.1,
        20.3, 22.5, 29. , 24.8, 22. , 26.4, 33.1, 36.1, 28.4, 33.4, 28.2,
        22.8, 20.3, 16.1, 22.1, 19.4, 21.6, 23.8, 16.2, 17.8, 19.8, 23.1,
        21. , 23.8, 23.1, 20.4, 18.5, 25. , 24.6, 23. , 22.2, 19.3, 22.6,
        19.8, 17.1, 19.4, 22.2, 20.7, 21.1, 19.5, 18.5, 20.6, 19. , 18.7,
        32.7, 16.5, 23.9, 31.2, 17.5, 17.2, 23.1, 24.5, 26.6, 22.9, 24.1,
        18.6, 30.1, 18.2, 20.6, 17.8, 21.7, 22.7, 22.6, 25. , 19.9, 20.8,
        16.8, 21.9, 27.5, 21.9, 23.1, 50. , 50. , 50. , 50. , 50. , 13.8,
        13.8, 15. , 13.9, 13.3, 13.1, 10.2, 10.4, 10.9, 11.3, 12.3,  8.8,
         7.2, 10.5,  7.4, 10.2, 11.5, 15.1, 23.2,  9.7, 13.8, 12.7, 13.1,
        12.5,  8.5,  5. ,  6.3,  5.6,  7.2, 12.1,  8.3,  8.5,  5. , 11.9,
        27.9, 17.2, 27.5, 15. , 17.2, 17.9, 16.3,  7. ,  7.2,  7.5, 10.4,
         8.8,  8.4, 16.7, 14.2, 20.8, 13.4, 11.7,  8.3, 10.2, 10.9, 11. ,
         9.5, 14.5, 14.1, 16.1, 14.3, 11.7, 13.4,  9.6,  8.7,  8.4, 12.8,
        10.5, 17.1, 18.4, 15.4, 10.8, 11.8, 14.9, 12.6, 14.1, 13. , 13.4,
        15.2, 16.1, 17.8, 14.9, 14.1, 12.7, 13.5, 14.9, 20. , 16.4, 17.7,
        19.5, 20.2, 21.4, 19.9, 19. , 19.1, 19.1, 20.1, 19.9, 19.6, 23.2,
        29.8, 13.8, 13.3, 16.7, 12. , 14.6, 21.4, 23. , 23.7, 25. , 21.8,
        20.6, 21.2, 19.1, 20.6, 15.2,  7. ,  8.1, 13.6, 20.1, 21.8, 24.5,
        23.1, 19.7, 18.3, 21.2, 17.5, 16.8, 22.4, 20.6, 23.9, 22. , 11.9]),
 'feature_names': array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD',
        'TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='<U7'),
 'DESCR': ".. _boston_dataset:\n\nBoston house prices dataset\n---------------------------\n\n**Data Set Characteristics:**  \n\n    :Number of Instances: 506 \n\n    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.\n\n    :Attribute Information (in order):\n        - CRIM     per capita crime rate by town\n        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.\n        - INDUS    proportion of non-retail business acres per town\n        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\n        - NOX      nitric oxides concentration (parts per 10 million)\n        - RM       average number of rooms per dwelling\n        - AGE      proportion of owner-occupied units built prior to 1940\n        - DIS      weighted distances to five Boston employment centres\n        - RAD      index of accessibility to radial highways\n        - TAX      full-value property-tax rate per $10,000\n        - PTRATIO  pupil-teacher ratio by town\n        - B        1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town\n        - LSTAT    % lower status of the population\n        - MEDV     Median value of owner-occupied homes in $1000's\n\n    :Missing Attribute Values: None\n\n    :Creator: Harrison, D. and Rubinfeld, D.L.\n\nThis is a copy of UCI ML housing dataset.\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/housing/\n\n\nThis dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.\n\nThe Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic\nprices and the demand for clean air', J. Environ. Economics & Management,\nvol.5, 81-102, 1978.   Used in Belsley, Kuh & Welsch, 'Regression diagnostics\n...', Wiley, 1980.   N.B. Various transformations are used in the table on\npages 244-261 of the latter.\n\nThe Boston house-price data has been used in many machine learning papers that address regression\nproblems.   \n     \n.. topic:: References\n\n   - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.\n   - 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.\n",
 'filename': '/opt/conda/lib/python3.7/site-packages/sklearn/datasets/data/boston_house_prices.csv'}

只取每个样本的第6个特征:RM房间数,生成一个包含506个样本只有房间数一个特征的特征集x。

AI_9_0_2

import numpy as np
import matplotlib.pyplot as plt 
from sklearn import datasets 
%matplotlib inline

boston = datasets.load_boston()

# 特征数据集
x = boston.data[:,5] 
 
# 样本标签数据集,包含所有房子的交易价钱
y = boston.target 

# 特征数据集和标签数据集绘制散点图
plt.scatter(x,y)
plt.show()
x

array([6.575, 6.421, 7.185, 6.998, 7.147, 6.43 , 6.012, 6.172, 5.631,
       6.004, 6.377, 6.009, 5.889, 5.949, 6.096, 5.834, 5.935, 5.99 ,
       5.456, 5.727, 5.57 , 5.965, 6.142, 5.813, 5.924, 5.599, 5.813,
       6.047, 6.495, 6.674, 5.713, 6.072, 5.95 , 5.701, 6.096, 5.933,
       5.841, 5.85 , 5.966, 6.595, 7.024, 6.77 , 6.169, 6.211, 6.069,
       5.682, 5.786, 6.03 , 5.399, 5.602, 5.963, 6.115, 6.511, 5.998,
       5.888, 7.249, 6.383, 6.816, 6.145, 5.927, 5.741, 5.966, 6.456,
       6.762, 7.104, 6.29 , 5.787, 5.878, 5.594, 5.885, 6.417, 5.961,
       6.065, 6.245, 6.273, 6.286, 6.279, 6.14 , 6.232, 5.874, 6.727,
       6.619, 6.302, 6.167, 6.389, 6.63 , 6.015, 6.121, 7.007, 7.079,
       6.417, 6.405, 6.442, 6.211, 6.249, 6.625, 6.163, 8.069, 7.82 ,
       7.416, 6.727, 6.781, 6.405, 6.137, 6.167, 5.851, 5.836, 6.127,
       6.474, 6.229, 6.195, 6.715, 5.913, 6.092, 6.254, 5.928, 6.176,
       6.021, 5.872, 5.731, 5.87 , 6.004, 5.961, 5.856, 5.879, 5.986,
       5.613, 5.693, 6.431, 5.637, 6.458, 6.326, 6.372, 5.822, 5.757,
       6.335, 5.942, 6.454, 5.857, 6.151, 6.174, 5.019, 5.403, 5.468,
       4.903, 6.13 , 5.628, 4.926, 5.186, 5.597, 6.122, 5.404, 5.012,
       5.709, 6.129, 6.152, 5.272, 6.943, 6.066, 6.51 , 6.25 , 7.489,
       7.802, 8.375, 5.854, 6.101, 7.929, 5.877, 6.319, 6.402, 5.875,
       5.88 , 5.572, 6.416, 5.859, 6.546, 6.02 , 6.315, 6.86 , 6.98 ,
       7.765, 6.144, 7.155, 6.563, 5.604, 6.153, 7.831, 6.782, 6.556,
       7.185, 6.951, 6.739, 7.178, 6.8  , 6.604, 7.875, 7.287, 7.107,
       7.274, 6.975, 7.135, 6.162, 7.61 , 7.853, 8.034, 5.891, 6.326,
       5.783, 6.064, 5.344, 5.96 , 5.404, 5.807, 6.375, 5.412, 6.182,
       5.888, 6.642, 5.951, 6.373, 6.951, 6.164, 6.879, 6.618, 8.266,
       8.725, 8.04 , 7.163, 7.686, 6.552, 5.981, 7.412, 8.337, 8.247,
       6.726, 6.086, 6.631, 7.358, 6.481, 6.606, 6.897, 6.095, 6.358,
       6.393, 5.593, 5.605, 6.108, 6.226, 6.433, 6.718, 6.487, 6.438,
       6.957, 8.259, 6.108, 5.876, 7.454, 8.704, 7.333, 6.842, 7.203,
       7.52 , 8.398, 7.327, 7.206, 5.56 , 7.014, 8.297, 7.47 , 5.92 ,
       5.856, 6.24 , 6.538, 7.691, 6.758, 6.854, 7.267, 6.826, 6.482,
       6.812, 7.82 , 6.968, 7.645, 7.923, 7.088, 6.453, 6.23 , 6.209,
       6.315, 6.565, 6.861, 7.148, 6.63 , 6.127, 6.009, 6.678, 6.549,
       5.79 , 6.345, 7.041, 6.871, 6.59 , 6.495, 6.982, 7.236, 6.616,
       7.42 , 6.849, 6.635, 5.972, 4.973, 6.122, 6.023, 6.266, 6.567,
       5.705, 5.914, 5.782, 6.382, 6.113, 6.426, 6.376, 6.041, 5.708,
       6.415, 6.431, 6.312, 6.083, 5.868, 6.333, 6.144, 5.706, 6.031,
       6.316, 6.31 , 6.037, 5.869, 5.895, 6.059, 5.985, 5.968, 7.241,
       6.54 , 6.696, 6.874, 6.014, 5.898, 6.516, 6.635, 6.939, 6.49 ,
       6.579, 5.884, 6.728, 5.663, 5.936, 6.212, 6.395, 6.127, 6.112,
       6.398, 6.251, 5.362, 5.803, 8.78 , 3.561, 4.963, 3.863, 4.97 ,
       6.683, 7.016, 6.216, 5.875, 4.906, 4.138, 7.313, 6.649, 6.794,
       6.38 , 6.223, 6.968, 6.545, 5.536, 5.52 , 4.368, 5.277, 4.652,
       5.   , 4.88 , 5.39 , 5.713, 6.051, 5.036, 6.193, 5.887, 6.471,
       6.405, 5.747, 5.453, 5.852, 5.987, 6.343, 6.404, 5.349, 5.531,
       5.683, 4.138, 5.608, 5.617, 6.852, 5.757, 6.657, 4.628, 5.155,
       4.519, 6.434, 6.782, 5.304, 5.957, 6.824, 6.411, 6.006, 5.648,
       6.103, 5.565, 5.896, 5.837, 6.202, 6.193, 6.38 , 6.348, 6.833,
       6.425, 6.436, 6.208, 6.629, 6.461, 6.152, 5.935, 5.627, 5.818,
       6.406, 6.219, 6.485, 5.854, 6.459, 6.341, 6.251, 6.185, 6.417,
       6.749, 6.655, 6.297, 7.393, 6.728, 6.525, 5.976, 5.936, 6.301,
       6.081, 6.701, 6.376, 6.317, 6.513, 6.209, 5.759, 5.952, 6.003,
       5.926, 5.713, 6.167, 6.229, 6.437, 6.98 , 5.427, 6.162, 6.484,
       5.304, 6.185, 6.229, 6.242, 6.75 , 7.061, 5.762, 5.871, 6.312,
       6.114, 5.905, 5.454, 5.414, 5.093, 5.983, 5.983, 5.707, 5.926,
       5.67 , 5.39 , 5.794, 6.019, 5.569, 6.027, 6.593, 6.12 , 6.976,
       6.794, 6.03 ])

数据预处理

上面这个散点图中,顶部 y 等于50的那些点看起来有些问题,原因是在采集数据的时候有可能有一些最大值的限制,高于某个值的时候,全都被标记成最大值50,这样的样本显然是有问题的,所以要去除掉这些异常样本。

AI_9_0_3

import numpy as np
import matplotlib.pyplot as plt 
from sklearn import datasets 
%matplotlib inline

boston = datasets.load_boston()

x = boston.data[:,5] 
y = boston.target 

# 取房价小于50的点作为样本点
x=x[y<50]
y = y[y<50]

plt.scatter(x,y)
plt.show()

训练集和测试集划分

经过前面对数据的分析和理解,已经准备好了老司机要的房价数据,接下来就是划分训练数据集和测试数据集。

AI_9_0_4

from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt 
from sklearn import datasets 

boston = datasets.load_boston()
x = boston.data[:,5] 
y = boston.target 
# 取房价小于50的点作为样本点
x = x[y<50]
y = y[y<50]

# 划分数据集,test_size=0.2 测试数据集占比20%
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=111)

print(x_train[:5])

# 打印数据集的形状
print("x_train shape:",x_train.shape)
print("x_test shape:",x_test.shape)
[6.242 6.434 7.185 7.42  6.556]
x_train shape: (392,)
x_test shape: (98,)

sklearn提供的线性回归模型有个要求:如果特征数据集是一个只有一个特征值的一维数组,要通过reshape(-1, 1)改变数组形状为具有一列特征的二维数组。

所以还要对训练特征数据集和测试特征数据集的形状进行修改。

AI_9_0_5

from sys import path
path.append(r"../data/course_util")
from ai_course_9_1 import *

# 使用reshape函数将x_train转换为具有1列特征的二维数组
x_train = x_train.reshape(-1, 1)

#打印结果是(392, 1)表示x_train是一个二维数组,第一维有392行,第二维有1列,也就是特征数据集有392个样本,每个样本有1个特征
print("x_train new shape:",x_train.shape) 

x_test = x_test.reshape(-1, 1)

print("x_test new shape:",x_test.shape) 
x_train new shape: (392, 1)
x_test new shape: (98, 1)

训练模型

引入sklearn提供的线性回归算法模型LinearRegression,创建线性回归算法对象,后续我们要训练这个对象,让它具备预测房价的能力。

AI_9_0_6

from sklearn.linear_model import LinearRegression

# 创建线性回归算法对象
linear_reg = LinearRegression()

训练算法模型,得到线性回归算法模型的系数(简单线性回归公式y=ax+b中的a)和截距(简单线性回归公式y=ax+b中的b)

AI_9_0_7

from sys import path
path.append(r"../data/course_util")
from ai_course_9_2 import *

from sklearn.linear_model import LinearRegression

# 创建线性回归算法对象
linear_reg = LinearRegression()
# 训练算法模型
linear_reg.fit(x_train,y_train)

print("系数:",linear_reg.coef_) #LinearRegression对象提供的 coef_ 属性就是经过训练得到的系数
print("截距:",linear_reg.intercept_) #LinearRegression对象提供的 intercept_ 属性就是经过训练得到的截距
系数: [7.89092595]
截距: -27.89838286920032

画图展示

通过训练算法模型,我们已经得到了y = ax + b中的系数a和截距b,为了更加直观地观察模型训练出来的直线与数据之间的关系,接下来我们在二维平面上画出他们之间的关系。

AI_9_0_8

from sys import path
path.append(r"../data/course_util")
from ai_course_9_2 import *

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt 
%matplotlib inline

# 创建线性回归算法对象
linear_reg = LinearRegression()
# 训练算法模型
linear_reg.fit(x_train,y_train)

# 使用训练出来的模型 y=ax+b,在训练数据集x_train上预测出对应的房价,然后绘制成散点图和折线图
plt.scatter(x_train,y_train) # 横坐标表示房屋数,纵坐标表示房价,根据训练数据集画出来的散点图,每个点是一套房子
plt.plot(x_train,linear_reg.predict(x_train),color="r") # 训练特征集与模型预测的房价画一条红色的直线
plt.show()

经过训练之后,我们已经得到了一个线性回归算法模型,接下来就要时候用测试数据集来验证这个模型的效果。

回归模型的评价指标

之前我们学习的k近邻算法属于分类算法,当时我们用的是准确度来衡量算法模型的好坏。那问题来了,线性回归属于回归算法,我们应该用什么指标来衡量线性回归算法模型的好坏呢?

R2评价指标

这个问题难不倒经验丰富的老司机,线性回归最好的评价指标是:​​,那什么是呢?

接下来我来解释下​​的意义是什么,它为什么好。

​​计算公式的分子部分:​​ 表示使用预测结果与真实值的差求平方和,那么它的意义就是使用我们的模型预测产生的错误

​​计算公式的分母部分:​​ 表示使用均值与真实值的差求平方和,那么它的意义就是使用 ​​ 预测产生的错误,意思就是说不管你的特征x是什么,预测值都是均值。在机器学习里对于这种直接用均值作为预测值的方式叫做Baseline Model,也就是最基本的模型。

Baseline Model它的错误肯定是比较多的,因为它完全不考虑样本的特征,直接就输出y的均值作为预测结果。把它作为公式的分母的意思是:在不考虑任何样本特征的情况下,产生的误差总和作为模型最大的误差数,把它作为一个误差的参考标准。

而我们自己训练出来的模型的预测值中错误的结果按道理应该是比较少的,因为它充分地考虑了特征x与y之间的关系。

R2评价指标的表达式

那么 ​​ 这个公式表达的含义是:

为了更加清晰的解释​​的含义,我再用一个小故事来解释下​​公式的含义。

北京市朝阳区的二手房平均价格是4万元/平米,一个要卖房的人找到了房产中介小李,让小李给他评估下房价,小李没有问房主任何关于房子的问题,直接跟房主说你的房子每平米4万元。房主很无奈,大骂小李:我的房子在CBD,学区房,小区环境优美,24小时保安,180平米的南北通透户型,你给我4万一平米,你疯啦!这样的错误小李经常犯,一个月能犯100次(总错误数)。

小李的领导老王发现这个问题,老王让公司的机器学习算法工程师小马开发一个房屋价格预测工具,小马以简单线性回归算法模型为核心,仅用1天时间就实现了这个房屋价格预测工具,老王命令小李接下来的一个月就用这个工具给客户评估房价。接下来的一个月里,大部分房主对小李给出的评估价钱都比较满意,小李被房主骂的次数也明显减少了,这个月小李一共才被客户骂了20次(模型产生的错误总数)。

小马把具体的数据带入公式 ,然后跟老王解释说:我给出的这个评价的值域是0到1之间,0分是最低分表示小李之前没有使用任何工具时的得分,满分1分表示我这个工具预测的房价都很准确,房主都很满意没有投诉,评价的值越接近1就越好

老王听了小马的这个评价标准表示很满意,然后老王问小马:那使用你这个工具的得分是多少呀?小马面带微笑说:不是很高,才0.8分。老王听了非常满意,拍拍小马的肩膀说:小同志,很谦虚嘛,你这个得分已经很不错了,继续加油,这月给你多发10个月奖金!

R2指标的意义

好了,通过刚才的小故事相信你对​​的原理已经理解了,那我们来总结下​​的意义。

1.​​ 的值小于等于 1,也就是最大值是1

2.​​ 越大越接近1越好,当预测模型不犯任何错误时,也就是公式中的分子是0,此时 ​​ 得到最大值1。

3.当使用最基本的模型(Baseline Model)预测时,也就是不考虑任何样本特征,直接使用样本标签的平均值,也就是公式中的分子分母相同,那么

4.如果预测模型产生的错误比使用Baseline Model产生的错误还多,也就是公式中的分子大于分母,那么 。这种情况说明我们使用的预测模型还没有直接使用样本标签的平均值效果好,有可能我们的数据本身就不存在任何线性关系。

好了,我们已经掌握了衡量线性回归模型最好的指标​​

代码实现

接下来我们用测试数据来测试下模型的效果如何,这里做个小提示,在sklean中的LinearRegression 本身提供的模型评价指标就是​​,可以调用模型对象的score方法直接使用​​对模型进行评价。

AI_9_0_9

import numpy as np
from sklearn import datasets 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt 
%matplotlib inline

boston = datasets.load_boston()
x = boston.data[:,5] 
y = boston.target 
# 取房价小于50的点作为样本点
x = x[y<50]
y = y[y<50]
# 划分数据集,test_size=0.2 测试数据集占比20%
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=111)
# 使用reshape函数将x_train转换为具有1列特征的二维数组
x_train = x_train.reshape(-1, 1)
# 创建线性回归算法对象
linear_reg = LinearRegression()
# 训练算法模型
linear_reg.fit(x_train,y_train)

x_test = x_test.reshape(-1, 1)

# 使用测试数据集通过R方衡量模型效果
r2_score = linear_reg.score(x_test,y_test) 

# 打印R方评价结果
print("r2_score:",r2_score)
r2_score: 0.6160507035382776

老司机对模型的评价结果还算比较满意,毕竟在看房子的时候可以有个参考价钱,防止在人生地不熟的波士顿遇到黑心中介被坑。

好了,今天老司机就先表演到这里吧,老司机该去买彩票筹钱买房子啦~

今天我们先到这里,拜拜~

联系我们,一起学Python吧

分享Python实战代码,入门资料,进阶资料,基础语法,爬虫,数据分析,web网站,机器学习,深度学习等等。


​关注公众号「Python家庭领取1024G整套教材交流群学习商务合作

猜你喜欢

转载自blog.csdn.net/qq_34409973/article/details/114981008