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

    from sklearn.datasets import load_boston
    boston = load_boston()
    boston.keys()
    
    dict_keys(['data', 'target', 'feature_names', 'DESCR'])
    
    print(boston.DESCR)
    
    data=boston.data
    x=data[:,5]
    y=boston.target
    import matplotlib.pyplot as plt
    plt.scatter(x,y)
    plt.plot(x,w*x+b)
    plt.show()
    
    
    #加载分类库
    from sklearn.linear_model import LinearRegression
    
    LineR=LinearRegression()
    #对库进行分类,从1开始,的第一列
    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) 
    w = lineR.coef_
    b = lineR.intercept_        
    
    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,'green')
    print(w,b)
    plt.show()
    

      

    #4.  一元多项式回归模型,建立一个变量与房价之间的预测模型,并图形化显示。
    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.plot(x,y_poly_pred,'r')
    plt.show()
    
    from sklearn.preprocessing import PolynomialFeatures
    poly = PolynomialFeatures(degree=2)
    x_poly = poly.fit_transform(x)
    
    lrp = LinearRegression()
    lrp.fit(x_poly,y)
    plt.scatter(x,y)
    plt.scatter(x,y_pred)
    plt.scatter(x,y_poly_pred)   #多项回归
    plt.show()

      

  • 相关阅读:
    Jmeter 接口测试实战-有趣的cookie
    Jmeter输出完美报告
    记忆-走进古镇
    JMeter接口测试实战-动态数据验证
    JMeter写入文件
    正则表达式匹配任意字符(包括换行符)
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"
    Unable to get the CMake version located at
    adb 查看 android手机的CPU架构
    java.lang.UnsatisfiedLinkError:dlopen failed: “**/*/arm/*.so” has unexpected e_machine: 3
  • 原文地址:https://www.cnblogs.com/cc013/p/10095503.html
Copyright © 2011-2022 走看看