zoukankan      html  css  js  c++  java
  • boston房价预测--大作业一

    1. 读取数据集

    2. 训练集与测试集划分

    3. 线性回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。

    4. 多项式回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。

    5. 比较线性模型与非线性模型的性能,并说明原因。

    #线性回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。
    from sklearn.datasets import load_boston
    import matplotlib.pyplot as plt
    from sklearn.linear_model import LinearRegression
    from sklearn.model_selection import train_test_split
    boston=load_boston()#导入数据集
    x = boston.data
    y = boston.target
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)#划分训练集和测试集
    lineR=LinearRegression()#线性模型
    lineR.fit(x_train,y_train)
    #判断模型的好坏
    print('预测的准确率:',lineR.score(x_test,y_test))

    #4. 多项式回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。
    from sklearn.preprocessing import PolynomialFeatures
    poly=PolynomialFeatures(degree=2)
    from sklearn.linear_model import LinearRegression
    lineR=LinearRegression()
    x= boston.data
    y = boston.target
    x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)#划分训练集和测试集
    #多项式操作
    x_train_poly=poly.fit_transform(x_train)
    x_test_poly=poly.transform(x_test)
    lineR.fit(x_train_poly,y_train)#建立模型
    print('预测的准确率:',lineR.score(x_test_poly,y_test))
    
    #图形化
    from sklearn.linear_model import LinearRegression
    import matplotlib.pyplot as plt
    lineR=LinearRegression()
    lineR.fit(x_train_poly,y_train)
    y_poly_pred=lineR.predict(x_test_poly)
    plt.plot(y,y,'r')
    plt.scatter(y_test,y_poly_pred)
    plt.show()

    线性模型与非线性模型性能的区别:

    一个模型如果是线性的,就意味着它的参数项要么是常数,要么是原参数和要预测的特征之间的乘积加和就是我们要预测的值。

    线性模型计算复杂度较低,不足之处是模型拟合效果相对弱些。非线性模型拟合能力较强,不足之处是数据量不足容易过拟合,计算复杂度高,可解释性不好。

  • 相关阅读:
    bash快捷建-光标移到行首、行尾等
    VitualBox中linux系统ping ip能通域名不通的解决办法
    linux下使用tar命令
    Windows平台下Git服务器搭建
    android 在使用studio 编写百度地图中遇到APP Scode码校验失败 问题
    android 开发中 添加库文件 和so 文件的存放位置和添加依赖
    Volley之 JsonRequest 解析JSON 数据
    利用Volley封装好的图片缓存处理加载图片
    使用Volley执行网络数据传输
    android 测试 Monkey 和 MonkeyRunner 的使用
  • 原文地址:https://www.cnblogs.com/zxcv11/p/10123329.html
Copyright © 2011-2022 走看看