利用python与线性回归预测房价

本文利用pandas对Ames房价数据集进行数据分析,并挑选其中对房价影响程度前十的特征做了线性回归以预测房价。

一、导入数据集

import pandas as pd
train_data=pd.read_csv('./train.csv')
train_data
IdMSSubClassMSZoningLotFrontageLotAreaStreetAlleyLotShapeLandContourUtilities...PoolAreaPoolQCFenceMiscFeatureMiscValMoSoldYrSoldSaleTypeSaleConditionSalePrice
0160RL65.08450PaveNaNRegLvlAllPub...0NaNNaNNaN022008WDNormal208500
1220RL80.09600PaveNaNRegLvlAllPub...0NaNNaNNaN052007WDNormal181500
2360RL68.011250PaveNaNIR1LvlAllPub...0NaNNaNNaN092008WDNormal223500
3470RL60.09550PaveNaNIR1LvlAllPub...0NaNNaNNaN022006WDAbnorml140000
4560RL84.014260PaveNaNIR1LvlAllPub...0NaNNaNNaN0122008WDNormal250000
..................................................................
1455145660RL62.07917PaveNaNRegLvlAllPub...0NaNNaNNaN082007WDNormal175000
1456145720RL85.013175PaveNaNRegLvlAllPub...0NaNMnPrvNaN022010WDNormal210000
1457145870RL66.09042PaveNaNRegLvlAllPub...0NaNGdPrvShed250052010WDNormal266500
1458145920RL68.09717PaveNaNRegLvlAllPub...0NaNNaNNaN042010WDNormal142125
1459146020RL75.09937PaveNaNRegLvlAllPub...0NaNNaNNaN062008WDNormal147500

1460 rows × 81 columns

二、查看数据的基本情况

train_data.describe()
IdMSSubClassLotFrontageLotAreaOverallQualOverallCondYearBuiltYearRemodAddMasVnrAreaBsmtFinSF1...WoodDeckSFOpenPorchSFEnclosedPorch3SsnPorchScreenPorchPoolAreaMiscValMoSoldYrSoldSalePrice
count1460.0000001460.0000001201.0000001460.0000001460.0000001460.0000001460.0000001460.0000001452.0000001460.000000...1460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.0000001460.000000
mean730.50000056.89726070.04995810516.8280826.0993155.5753421971.2678081984.865753103.685262443.639726...94.24452146.66027421.9541103.40958915.0609592.75890443.4890416.3219182007.815753180921.195890
std421.61000942.30057124.2847529981.2649321.3829971.11279930.20290420.645407181.066207456.098091...125.33879466.25602861.11914929.31733155.75741540.177307496.1230242.7036261.32809579442.502883
min1.00000020.00000021.0000001300.0000001.0000001.0000001872.0000001950.0000000.0000000.000000...0.0000000.0000000.0000000.0000000.0000000.0000000.0000001.0000002006.00000034900.000000
25%365.75000020.00000059.0000007553.5000005.0000005.0000001954.0000001967.0000000.0000000.000000...0.0000000.0000000.0000000.0000000.0000000.0000000.0000005.0000002007.000000129975.000000
50%730.50000050.00000069.0000009478.5000006.0000005.0000001973.0000001994.0000000.000000383.500000...0.00000025.0000000.0000000.0000000.0000000.0000000.0000006.0000002008.000000163000.000000
75%1095.25000070.00000080.00000011601.5000007.0000006.0000002000.0000002004.000000166.000000712.250000...168.00000068.0000000.0000000.0000000.0000000.0000000.0000008.0000002009.000000214000.000000
max1460.000000190.000000313.000000215245.00000010.0000009.0000002010.0000002010.0000001600.0000005644.000000...857.000000547.000000552.000000508.000000480.000000738.00000015500.00000012.0000002010.000000755000.000000

8 rows × 38 columns

train_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1460 entries, 0 to 1459
Data columns (total 81 columns):
Id               1460 non-null int64
MSSubClass       1460 non-null int64
MSZoning         1460 non-null object
LotFrontage      1201 non-null float64
LotArea          1460 non-null int64
Street           1460 non-null object
Alley            91 non-null object
LotShape         1460 non-null object
LandContour      1460 non-null object
Utilities        1460 non-null object
LotConfig        1460 non-null object
LandSlope        1460 non-null object
Neighborhood     1460 non-null object
Condition1       1460 non-null object
Condition2       1460 non-null object
BldgType         1460 non-null object
HouseStyle       1460 non-null object
OverallQual      1460 non-null int64
OverallCond      1460 non-null int64
YearBuilt        1460 non-null int64
YearRemodAdd     1460 non-null int64
RoofStyle        1460 non-null object
RoofMatl         1460 non-null object
Exterior1st      1460 non-null object
Exterior2nd      1460 non-null object
MasVnrType       1452 non-null object
MasVnrArea       1452 non-null float64
ExterQual        1460 non-null object
ExterCond        1460 non-null object
Foundation       1460 non-null object
BsmtQual         1423 non-null object
BsmtCond         1423 non-null object
BsmtExposure     1422 non-null object
BsmtFinType1     1423 non-null object
BsmtFinSF1       1460 non-null int64
BsmtFinType2     1422 non-null object
BsmtFinSF2       1460 non-null int64
BsmtUnfSF        1460 non-null int64
TotalBsmtSF      1460 non-null int64
Heating          1460 non-null object
HeatingQC        1460 non-null object
CentralAir       1460 non-null object
Electrical       1459 non-null object
1stFlrSF         1460 non-null int64
2ndFlrSF         1460 non-null int64
LowQualFinSF     1460 non-null int64
GrLivArea        1460 non-null int64
BsmtFullBath     1460 non-null int64
BsmtHalfBath     1460 non-null int64
FullBath         1460 non-null int64
HalfBath         1460 non-null int64
BedroomAbvGr     1460 non-null int64
KitchenAbvGr     1460 non-null int64
KitchenQual      1460 non-null object
TotRmsAbvGrd     1460 non-null int64
Functional       1460 non-null object
Fireplaces       1460 non-null int64
FireplaceQu      770 non-null object
GarageType       1379 non-null object
GarageYrBlt      1379 non-null float64
GarageFinish     1379 non-null object
GarageCars       1460 non-null int64
GarageArea       1460 non-null int64
GarageQual       1379 non-null object
GarageCond       1379 non-null object
PavedDrive       1460 non-null object
WoodDeckSF       1460 non-null int64
OpenPorchSF      1460 non-null int64
EnclosedPorch    1460 non-null int64
3SsnPorch        1460 non-null int64
ScreenPorch      1460 non-null int64
PoolArea         1460 non-null int64
PoolQC           7 non-null object
Fence            281 non-null object
MiscFeature      54 non-null object
MiscVal          1460 non-null int64
MoSold           1460 non-null int64
YrSold           1460 non-null int64
SaleType         1460 non-null object
SaleCondition    1460 non-null object
SalePrice        1460 non-null int64
dtypes: float64(3), int64(35), object(43)
memory usage: 924.0+ KB
train_data.hist(figsize=(20,20))
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0000000026468E80>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000255EB4E0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000002539A630>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000246C3FD0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000246E18D0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000002331EA20>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000025FEECF8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025851320>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025851358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000002564BE48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026404438>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000264189E8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000025959F98>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025928588>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000024A87B38>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000256C12E8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000002614A6D8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026448C88>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000025CB2278>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026506828>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000257F0DD8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000002619C3C8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026191978>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x000000002522DF28>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000024B11518>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025D49AC8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025A120B8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000260E1668>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000258A1C18>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026816208>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x000000002683AD68>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026611D68>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000024E85358>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000026772908>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000259B3EB8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025C744A8>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x0000000025CFDA58>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025C42710>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025252B70>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025234BA8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x0000000025288198>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x00000000267E7748>]],
      dtype=object)

在这里插入图片描述

三、计算各特征与房价之间的相关系数

corr_df=train_data.corr()['SalePrice'].sort_values(ascending=False)
corr_df
SalePrice        1.000000
OverallQual      0.790982
GrLivArea        0.708624
GarageCars       0.640409
GarageArea       0.623431
TotalBsmtSF      0.613581
1stFlrSF         0.605852
FullBath         0.560664
TotRmsAbvGrd     0.533723
YearBuilt        0.522897
YearRemodAdd     0.507101
GarageYrBlt      0.486362
MasVnrArea       0.477493
Fireplaces       0.466929
BsmtFinSF1       0.386420
LotFrontage      0.351799
WoodDeckSF       0.324413
2ndFlrSF         0.319334
OpenPorchSF      0.315856
HalfBath         0.284108
LotArea          0.263843
BsmtFullBath     0.227122
BsmtUnfSF        0.214479
BedroomAbvGr     0.168213
ScreenPorch      0.111447
PoolArea         0.092404
MoSold           0.046432
3SsnPorch        0.044584
BsmtFinSF2      -0.011378
BsmtHalfBath    -0.016844
MiscVal         -0.021190
Id              -0.021917
LowQualFinSF    -0.025606
YrSold          -0.028923
OverallCond     -0.077856
MSSubClass      -0.084284
EnclosedPorch   -0.128578
KitchenAbvGr    -0.135907
Name: SalePrice, dtype: float64

四、对与房价相关系数前十的特征进行数据可视化(散点图)

import matplotlib.pyplot as plt
import math
plt.figure(figsize=(10,10))
col_names=corr_df.index[1:11]
n=len(col_names)
cols=3
rows=math.ceil(n/cols)
for i in range(n):
    plt.subplot(rows,cols,i+1)
    plt.scatter(train_data[col_names[i]],train_data['SalePrice'])
    plt.title(col_names[i])
plt.tight_layout()

在这里插入图片描述

五、初始化回归模型权重并定义均方误差为评估指标

import numpy as np
theta=np.random.rand(len(col_names)+1)
print(theta)
def f(x):
    return np.dot(theta,x.T)

def mse(x,y):
    return np.sum((f(x)-y)**2)/len(y)
[0.14013852 0.70306677 0.43516643 0.57665082 0.19052374 0.21492676
 0.61924449 0.24905321 0.06569707 0.07960751 0.95983703]

六、设置初始学习率并进行模型训练

learning_rate=0.0001
train_x=train_data[col_names]
train_x=(train_x-train_x.mean())/train_x.std()#标准化
train_x['b']=1 #加上偏置项
train_y=train_data['SalePrice']
for i in range(100):
    theta=theta-learning_rate*np.dot(f(train_x)-train_y,train_x)
    metrics=mse(train_x,train_y)
    print(f'iter:{i},mse:{metrics}')
iter:0,mse:25817371843.540993
iter:1,mse:19070779958.704334
iter:2,mse:14319710788.67177
iter:3,mse:10861663959.744366
iter:4,mse:8337269406.214424
iter:5,mse:6493530020.578001
iter:6,mse:5146444679.488889
iter:7,mse:4161822192.191155
iter:8,mse:3441770537.6153336
iter:9,mse:2914871989.9557214
iter:10,mse:2529019275.7782683
iter:11,mse:2246190793.872005
iter:12,mse:2038639905.947897
iter:13,mse:1886115523.5835316
iter:14,mse:1773834843.9879074
iter:15,mse:1691004653.6156518
iter:16,mse:1629742726.9989412
iter:17,mse:1584291037.725107
iter:18,mse:1550441808.3864832
iter:19,mse:1525118802.0107481
iter:20,mse:1506071846.899198
iter:21,mse:1491653956.3700118
iter:22,mse:1480658696.958829
iter:23,mse:1472201506.2484279
iter:24,mse:1465633072.234535
iter:25,mse:1460476103.0681505
iter:26,mse:1456379162.2852933
iter:27,mse:1453082955.9092643
iter:28,mse:1450395705.962204
iter:29,mse:1448175155.3018467
iter:30,mse:1446315412.7260683
iter:31,mse:1444737331.6370451
iter:32,mse:1443381468.856506
iter:33,mse:1442202927.8996868
iter:34,mse:1441167579.0150025
iter:35,mse:1440249285.4449391
iter:36,mse:1439427865.4210844
iter:37,mse:1438687592.4084334
iter:38,mse:1438016089.381014
iter:39,mse:1437403511.781369
iter:40,mse:1436841942.1847355
iter:41,mse:1436324940.3953006
iter:42,mse:1435847207.818527
iter:43,mse:1435404335.9915845
iter:44,mse:1434992617.215805
iter:45,mse:1434608901.1250343
iter:46,mse:1434250485.328487
iter:47,mse:1433915031.4142568
iter:48,mse:1433600499.9022899
iter:49,mse:1433305099.4212935
iter:50,mse:1433027246.6189718
iter:51,mse:1432765534.220597
iter:52,mse:1432518705.3157291
iter:53,mse:1432285632.44166
iter:54,mse:1432065300.3920307
iter:55,mse:1431856791.9445975
iter:56,mse:1431659275.898459
iter:57,mse:1431471996.956677
iter:58,mse:1431294267.0985336
iter:59,mse:1431125458.166575
iter:60,mse:1430964995.4542654
iter:61,mse:1430812352.1258454
iter:62,mse:1430667044.3346698
iter:63,mse:1430528626.9328084
iter:64,mse:1430396689.6850588
iter:65,mse:1430270853.9163136
iter:66,mse:1430150769.533587
iter:67,mse:1430036112.3737445
iter:68,mse:1429926581.8357728
iter:69,mse:1429821898.7626548
iter:70,mse:1429721803.5430362
iter:71,mse:1429626054.4070165
iter:72,mse:1429534425.8938847
iter:73,mse:1429446707.4724996
iter:74,mse:1429362702.2974653
iter:75,mse:1429282226.0863287
iter:76,mse:1429205106.1047971
iter:77,mse:1429131180.2485106
iter:78,mse:1429060296.2112334
iter:79,mse:1428992310.730462
iter:80,mse:1428927088.9024827
iter:81,mse:1428864503.559771
iter:82,mse:1428804434.7044203
iter:83,mse:1428746768.991961
iter:84,mse:1428691399.260538
iter:85,mse:1428638224.1009636
iter:86,mse:1428587147.4636173
iter:87,mse:1428538078.29861
iter:88,mse:1428490930.2259846
iter:89,mse:1428445621.233077
iter:90,mse:1428402073.3964493
iter:91,mse:1428360212.6260748
iter:92,mse:1428319968.429696
iter:93,mse:1428281273.6954813
iter:94,mse:1428244064.4913058
iter:95,mse:1428208279.8791373
iter:96,mse:1428173861.7431722
iter:97,mse:1428140754.6304965
iter:98,mse:1428108905.6031668
iter:99,mse:1428078264.1007133

七、取前两百个数据查看预测值与真实值之间的误差

plt.figure(figsize=(20,10))
plt.plot(np.arange(200),train_y[:200],c='red',linestyle='--')
plt.plot(np.arange(200),f(train_x[:200]),c='green',linestyle='-.')
[<matplotlib.lines.Line2D at 0x2470af98>]

在这里插入图片描述

八、结论

由于本文使用的是最简单的线性回归模型,并且只在全部八十列特征中选取了十大特征进行训练,所以在某些极值方面的预测效果欠佳,但总体上是基本拟合实际房价的。

若想取得更好的预测结果,一是可以采用复杂度更高多项式回归模型进行训练,二是可以使用多层神经网络模型进行训练。

  • 1
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值