zoukankan      html  css  js  c++  java
  • 回归模型与房价预测

    1. 导入boston房价数据集

    from sklearn.datasets import load_boston
    boston = load_boston()
    boston.keys()

    print(boston.DESCR)

    boston.data.shape

    boston.feature_names

    boston.target

    import pandas as pd
    df = pd.DataFrame(boston.data)
    df [12]

    2. 一元线性回归模型,建立一个变量与房价之间的预测模型,并图形化显示。

    import matplotlib.pyplot as plt
    x = boston.data[:,5]
    y = boston.target
    plt.figure(figsize=(10,6))
    plt.scatter(x,y)
    plt.plot(x,9*x-20,'r')
    plt.show()
    x.shape

    from sklearn.linear_model import LinearRegression
    lineR = LinearRegression()
    lineR.fit(x.reshape(-1,1),y)
    w = lineR.coef_ #斜率
    b = lineR.intercept_ #截距

    3. 多元线性回归模型,建立13个变量与房价之间的预测模型,并检测模型好坏,并图形化显示检查结果。

    from sklearn.linear_model import LinearRegression
    lineR = LinearRegression()
    lineR.fit(boston.data,y)
    q = lineR.coef_ #斜率
    d = lineR.intercept_ #截距
    print(q,d)

    import matplotlib.pyplot as plt
    x = boston.data[:,12].reshape(-1,1)
    y = boston.target
    plt.figure( figsize = (10,6))
    plt.scatter(x,y)


    from sklearn.linear_model import LinearRegression
    lineR = LinearRegression()
    lineR.fit(x,y)
    y_pred = lineR.predict(x)
    plt.plot(x,y_pred)
    print(lineR.coef_,lineR.intercept_) #斜率和截距
    plt.show()

    from sklearn.preprocessing import PolynomialFeatures
    poly = PolynomialFeatures(degree=2)
    x_poly = poly.fit_transform(x)

    lrp = LinearRegression()
    lrp.fit(x_poly,y)
    y_poly_pred = lrp.predict(x_poly)
    plt.scatter(x,y)
    plt.scatter(x,y_pred)
    plt.scatter(x,y_poly_pred)
    plt.show()

  • 相关阅读:
    树世界
    清空 NumericUpDown
    没有评论的日子
    GetData.cs

    Hashtable 在程序中控制重复项
    Convert.ToInt32() VS System.Int32.Parse()
    饮食九要素
    添加 or 修改 的一个处理方式
    一个关于 电话号码 的正则表达式
  • 原文地址:https://www.cnblogs.com/SJMHJ/p/10075619.html
Copyright © 2011-2022 走看看