美文网首页
04 使用 sklearn 的线性回归算法实现房价预测

04 使用 sklearn 的线性回归算法实现房价预测

作者: 夏威夷的芒果 | 来源:发表于2018-08-20 21:53 被阅读771次
    image.png image.png image.png 知识点

    代码

    # 人工智能数据源下载地址:https://video.mugglecode.com/data_ai.zip,下载压缩包后解压即可(数据源与上节课相同)
    # -*- coding: utf-8 -*-
    
    """
        任务:房屋价格预测
    """
    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    
    import ai_utils
    
    DATA_FILE = './data_ai/house_data.csv'
    
    # 使用的特征列
    FEAT_COLS = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'sqft_above', 'sqft_basement']
    
    
    def main():
        """
            主函数
        """
        house_data = pd.read_csv(DATA_FILE, usecols=FEAT_COLS + ['price'])  # 仅仅取出来关心的几列来
        ai_utils.plot_feat_and_price(house_data)      
    
        X = house_data[FEAT_COLS].values     #取特征列
        y = house_data['price'].values       #取房价
     
        # 分割数据集
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=10)
    
        # 建立线性回归模型
        linear_reg_model = LinearRegression()   
        # 模型训练
        linear_reg_model.fit(X_train, y_train)
        # 验证模型
        r2_score = linear_reg_model.score(X_test, y_test)
        print('模型的R2值', r2_score)
    
        # 单个样本房价预测
        i = 50
        single_test_feat = X_test[i, :]
        y_true = y_test[i]
        y_pred = linear_reg_model.predict([single_test_feat])
        print('样本特征:', single_test_feat)
        print('真实价格:{},预测价格:{}'.format(y_true, y_pred))
    
    
    if __name__ == '__main__':
        main()
    

    运行结果

    模型的R2值 0.5058817864948697
    样本特征: [3.00e+00 2.00e+00 1.54e+03 6.25e+03 1.54e+03 0.00e+00]
    真实价格:272500.0,预测价格:395059
    
    可以看见基本上都是线性关系的

    需要注意的地方

    • 使用线性模型,验证的是R2模型
    from sklearn.linear_model import LinearRegression
     # 建立线性回归模型
        linear_reg_model = LinearRegression()   
        # 模型训练
        linear_reg_model.fit(X_train, y_train)
        # 验证模型
        r2_score = linear_reg_model.score(X_test, y_test)
        print('模型的R2值', r2_score)
    
    • 仅仅取出来关心的几列来
    import pandas as pd
    house_data = pd.read_csv(DATA_FILE, usecols=FEAT_COLS + ['price']) 
    

    练习:预测糖尿病的患病指标

    • 题目描述:预测糖尿病的患病指标

    • 题目要求:

    • 使用scikit-learn的线型回归模型对糖尿病的指标值进行预测

    • 选取1/5的数据作为测试集

    • 数据文件:

    • 数据源下载地址:https://video.mugglecode.com/diabetes.csv

    • diabetes.csv,包含了442个数据样本。

    • 共11列数据

    • AGE:年龄

    • SEX: 性别

    • BMI: 体质指数(Body Mass Index)

    • BP: 平均血压(Average Blood Pressure)

    • S1~S6: 一年后的6项疾病级数指标

    • Y: 一年后患疾病的定量指标,为需要预测的标签

    参考代码

    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    
    #读取数据
    data  = pd.read_csv('/Users/miraco/PycharmProjects/ai/data_ai/diabetes.csv')
    
    #取出数据
    
    feat_cols = ['AGE','SEX','BMI','BP','S1','S2','S3','S4','S5','S6']
    X = data[feat_cols].values
    
    y = data['Y'].values
    
    #数据划分
    X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=1/5, random_state= 10)
    
    #模型构建
    linear_regress_model = LinearRegression()
    
    #模型训练
    linear_regress_model.fit(X_train,y_train)
    
    #评价模型的指标
    R2 = linear_regress_model.score(X_test,y_test)
    print('模型的R2值:', R2)
    idx = 15
    test_feat = X_test[idx,:]
    y_predict = int(linear_regress_model.predict([test_feat]) ) #测出来结果是向量,得取出来
    y_real = y_test[idx]
    
    print(f'第{idx+1}个测试结果是{y_predict},本来应该是{y_real}')
    
    
    
    
    

    运行结果

    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/miraco/PycharmProjects/ai/diabetes_prediction.py
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
      return f(*args, **kwds)
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py:509: RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
    模型的R2值: 0.5341988244945843
      linalg.lstsq(X, y)
    第16个测试结果是223,本来应该是237
    

    警告结果可以无视。

    房价预测的可视化线性模型(二维的绘制)

    代码

    """
        任务:房屋价格预测
    """
    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    import matplotlib.pyplot as plt
    
    DATA_FILE = './data_ai/house_data.csv'
    
    # 使用的特征列
    FEAT_COLS = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'sqft_above', 'sqft_basement']
    
    
    def plot_fitting_line(linear_reg_model, X, y, feat):  #画图函数,传入参数:模型,X,y,特征名称
        """
            绘制线型回归线
        """
        w = linear_reg_model.coef_    #模型的一次参数
        b = linear_reg_model.intercept_   #模型截距
    
        plt.figure()
        # 样本点
        plt.scatter(X, y, alpha=0.5)       #画散点,透明度50%
    
        # 直线
        plt.plot(X, w * X + b, c='red')      #画线,描点连线,红色线
        plt.title(feat)   #题目
        plt.show()
    
    
    def main():
        """
            主函数
        """
        house_data = pd.read_csv(DATA_FILE, usecols=FEAT_COLS + ['price'])
    
        for feat in FEAT_COLS:
            X = house_data[feat].values.reshape(-1, 1)  
            #house_data[feat].values虽然是一列,但是它会自动转换成行向量,所以要重新塑形成列
            y = house_data['price'].values
    
            X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=10)
            linear_reg_model = LinearRegression()   #模型
            linear_reg_model.fit(X_train, y_train)  #训练
            r2_score = linear_reg_model.score(X_test, y_test)  #R2评分
            print('特征:{},R2值:{}'.format(feat, r2_score))
    
            # 绘制拟合直线
            plot_fitting_line(linear_reg_model, X_test, y_test, feat)
    
    
    if __name__ == '__main__':
        main()
    

    运行结果:

    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/miraco/PycharmProjects/ai/house_1_prediction.py
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
      return f(*args, **kwds)
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py:509: RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
      linalg.lstsq(X, y)
    特征:bedrooms,R2值:0.09258779614933556
    特征:bathrooms,R2值:0.27043501608845366
    特征:sqft_living,R2值:0.4961737855768537
    特征:sqft_lot,R2值:0.006598795645454514
    特征:sqft_above,R2值:0.35340999022703234
    特征:sqft_basement,R2值:0.11806711865379749
    
    Process finished with exit code 0
    

    练习:糖尿病患病指标的可视化线性模型

    • 题目描述:可视化线型回归模型

    • 题目要求:

    • 绘制糖尿病患病的各项属性(特征)的线型拟合曲线

    • 数据文件:

    • 数据源下载地址:https://video.mugglecode.com/diabetes.csv

    • diabetes.csv,包含了442个数据样本。

    • 共11列数据

    • AGE:年龄

    • SEX: 性别

    • BMI: 体质指数(Body Mass Index)

    • BP: 平均血压(Average Blood Pressure)

    • S1~S6: 一年后的6项疾病级数指标

    • Y: 一年后患疾病的定量指标,为需要预测的标签

    参考代码

    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LinearRegression
    from matplotlib import pyplot as plt
    plt.rcParams['savefig.dpi'] = 300 #图片像素
    plt.rcParams['figure.dpi'] = 300 #分辨率
    # 默认的像素:[6.0,4.0],分辨率为100,图片尺寸为 600&400
    # 指定dpi=200,图片尺寸为 1200*800
    # 指定dpi=300,图片尺寸为 1800*1200
    # 设置figsize可以在不改变分辨率情况下改变比例
    
    #读取数据
    data  = pd.read_csv('/Users/miraco/PycharmProjects/ai/data_ai/diabetes.csv')
    
    #取出数据
    
    Feat_cols = ['AGE','SEX','BMI','BP','S1','S2','S3','S4','S5','S6']
    
    fig = plt.figure()
    for feat_col in Feat_cols:
        X = data[feat_col].values.reshape(-1,1)
    
        y = data['Y'].values
    
        #数据划分
        X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=1/5, random_state= 10)
    
        #模型构建
        linear_regress_model = LinearRegression()
    
        #模型训练
        linear_regress_model.fit(X_train,y_train)
    
        #评价模型的指标
        R2 = linear_regress_model.score(X_test,y_test)
        print(f'取{feat_col}的模型的R2值为{R2}')
    
    
        coef = linear_regress_model.coef_
        intercpt = linear_regress_model.intercept_
        names = locals()
        idx = Feat_cols.index(feat_col)
        names[f'subpic_{str(idx)}'] = fig.add_subplot(4, 3, idx + 1)
        names[f'subpic_{str(idx)}'].scatter(X,y,alpha=0.5)
        names[f'subpic_{str(idx)}'].set_title(feat_col)
        names[f'subpic_{str(idx)}'].plot(X, coef * X + intercpt, c = "red")
    
    plt.tight_layout()
    plt.show()
    
    
    
    
    

    运行结果

    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/miraco/PycharmProjects/ai/diabetes_prediction.py
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
      return f(*args, **kwds)
    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py:509: RuntimeWarning: internal gelsd driver lwork query error, required iwork dimension not returned. This is likely the result of LAPACK bug 0038, fixed in LAPACK 3.2.2 (released July 21, 2010). Falling back to 'gelss' driver.
      linalg.lstsq(X, y)
    取AGE的模型的R2值为-0.020315734618997494
    取SEX的模型的R2值为-0.003956873797916982
    取BMI的模型的R2值为0.3706660809195329
    取BP的模型的R2值为0.1964406311230159
    取S1的模型的R2值为0.03139434030416344
    取S2的模型的R2值为0.01765701829206412
    取S3的模型的R2值为0.09229386793043748
    取S4的模型的R2值为0.12310951946304215
    取S5的模型的R2值为0.340972016892513
    取S6的模型的R2值为0.16145688026613692
    

    如果想要屏蔽这个警告,可以输入如下指令

    import warnings
    warnings.filterwarnings(action="ignore", module="scipy", message="^internal gelsd")
    

    相关文章

      网友评论

          本文标题:04 使用 sklearn 的线性回归算法实现房价预测

          本文链接:https://www.haomeiwen.com/subject/ltyliftx.html