zoukankan      html  css  js  c++  java
  • 房价预测Task5-7

    房价预测Task5-7

    Task 5

    从X1-X6中选择合适的项,作为对Y预测的Feature Names;划分训练集和测试集

    如何选择

    我们使用相关函数来测试X1-X6和Y之间的相关程度。

    df_5 = df
    corr_matrix = df_5.corr()   # (1)
    corr_matrix['Y house price of unit area'].sort_values()     # (2)
    
    # return 
    X3 distance to the nearest MRT station   -0.673613
    X2 house age                             -0.210567
    AgentId                                   0.077259
    X1 transaction date                       0.087491
    X6 longitude                              0.523287
    X5 latitude                               0.546307
    X4 number of convenience stores           0.571005
    Y house price of unit area                1.000000
    Name: Y house price of unit area, dtype: float64
    

    (1)直接使用.corr()函数,研究相关性;

    (2)这里是研究各参数X1-X6和Y的相关系数。所以结果中,Y处的相关系数为1。

    相关系数越趋近于1,代表正相关性越强,也就是正比;趋近于-1,代表负相关性越强,也就是反比。

    根据上面的结果,我们选择X2,X3,X4,X5,X6作为Feature Name.

    划分数据集

    要划分训练集和测试集。由于我们要使用X来预测Y,所以,X和Y要分离

    训练集有两部分:没有Y的训练数据train_x;只有Y的数据集train_y

    我们通过train_x训练得到一个线性模型,和train_y不断对比来校正系数。

    测试集也有两部分:没有Y的test_x;只有Y的test_y

    我们把test_x代入到训练好的模型中,会得到y的预测值,这个预测值就能和实际值test_y进行对比,来测试模型的成熟度。

    from sklearn.model_selection import train_test_split
    
    df_5 = df.drop(['AgentId', 'X1 transaction date'], axis=1)
    
    data_x = df_5.drop('Y house price of unit area', axis=1)    
    data_y = df_5[['Y house price of unit area']]               
    train_x, test_x, train_y, test_y = train_test_split(data_x, data_y, test_size=0.2)  # (1)
    

    (1)我们使用train_test_split()进行数据集的分割。这个函数会把data_xdata_y各分成两部分:训练集+测试集。

    data_x = train_x + test_x; data_y = train_y + test_y;

    其中,train_x和train_y的序号是对应的;test_x和test_y的序号是对应的。

    test_size代表测试集占整个数据集的占比。

    Task 6

    使用线性回归模型进行学习;并分析MAE和MAPE参数。

    from sklearn import linear_model
    from sklearn import metrics
    
    # Linear Regeression
    LR_reg = linear_model.LinearRegression()        # (1)
    LR_reg.fit(train_x, train_y)                    # (2)
    print("LR:", LR_reg.coef_, LR_reg.intercept_)   # (3)
    
    # Error measure
    preds = LR_reg.predict(test_x)                  # (4)
    mse = metrics.mean_squared_error(test_y, preds) # (5)
    mae = metrics.mean_absolute_error(test_y, preds)
    mape = np.mean(np.abs((preds-test_y)/test_y))*100 
    print(mse, mae, mape)
    

    (1)新建线性回归模型,也就是实例化;

    (2)训练要使用.fit()函数,输入训练集的X和Y即可。

    (3).coef_:线性回归函数的系数。我们这里选取了5个X,所以会有5个系数;

    .intercept_:线性回归函数的截距。

    (4)测试要使用.predict()函数,输入test_x,即可得到对应的预测值preds

    同时,我们还有真实值test_y,上面的预测值和这里的真实值一对比,即可得到各种误差;

    (5)至于下面这些误差,都是使用metrics这个库自带的函数得到的。这里我们不再展开。

    Ridge Model

    # Ridge Regression
    # This model is better than Ridge
    model_ridge =linear_model.Ridge(alpha = .5)
    model_ridge.fit(train_x,train_y)
    print("RR:", model_ridge.coef_, model_ridge.intercept_)
    
    # Test
    preds_ridge = model_ridge.predict(test_x)
    mse_ridge = metrics.mean_squared_error(test_y, preds_ridge)
    mae_ridge = metrics.mean_absolute_error(test_y, preds_ridge)
    mape_ridge = np.mean(np.abs((preds_ridge-test_y)/test_y))*100
    print(mse_ridge, mae_ridge, mape_ridge)
    

    Ridge 比起线性回归的效果要好。这两个的使用方法差不多。

    Task 7

    分析是否为过拟合?欠拟合

    import matplotlib.pyplot as plot 
    plot.figure('model')
    plot.plot(test_y, preds, '.')   # (1)
    # plot.scatter(test_y, preds)   # (2)
    plot.plot([test_y.min(), test_y.max()], [test_y.min(), test_y.max()], 'k--', lw =2) # (3)
    plot.show()
    

    这里,我们先画一下预测值和实际值的图像。根据分布来判断。

    (1)以test_y为横坐标,preds为纵坐标作图,图形的样式是散点图;

    (2)我们也可以使用.scatter()做散点图,这个散点图和上面的区别:点的默认粗细比较粗。

    (3)以(test_y.min(), test_y.min()),(test_y.max(), test_y.max())为起点和终点,做一条直线。

    注意:这样写的矩阵都是以对应点作为起点和终点的直线。

  • 相关阅读:
    设计模式的分类
    SQL Server 2005 TSQL 学习笔记:排名函数
    JS正则表达式语法
    魅族 “拜产品教”公司的优秀与局限
    jqueryMobile初始化组件
    Loading Scripts Without Blocking
    LabJs学习笔记:分析图
    翻转的css3样式
    IE(IE6/IE7/IE8)支持HTML5标签
    我的空间轨迹!
  • 原文地址:https://www.cnblogs.com/rongyupan/p/12884454.html
Copyright © 2011-2022 走看看