zoukankan      html  css  js  c++  java
  • 案例:波士顿房价预测

    1 案例背景介绍

    • 数据介绍

    给定的这些特征,是专家们得出的影响房价的结果属性。我们此阶段不需要自己去探究特征是否有用,只需要使用这些特征。到后面量化很多特征需要我们自己去寻找

    2 案例分析

    回归当中的数据大小不一致,是否会导致结果影响较大。所以需要做标准化处理。

    • 数据分割与标准化处理
    • 回归预测
    • 线性回归的算法效果评估

    3 回归性能评估

    均方误差(Mean Squared Error)MSE)评价机制:

    注:yi为预测值,overline{y}y​​ 为真实值

    思考:MSE和最小二乘法的区别是?

    • sklearn.metrics.mean_squared_error(y_true, y_pred)
      • 均方误差回归损失
      • y_true:真实值
      • y_pred:预测值
      • return:浮点数结果

    4 代码实现

    4.1 正规方程

    def linear_model1():
        """
        线性回归:正规方程
        :return:None
        """
        # 1.获取数据
        data = load_boston()
    
        # 2.数据集划分
        x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22)
    
        # 3.特征工程-标准化
        transfer = StandardScaler()
        x_train = transfer.fit_transform(x_train)
        x_test = transfer.fit_transform(x_test)
    
        # 4.机器学习-线性回归(正规方程)
        estimator = LinearRegression()
        estimator.fit(x_train, y_train)
    
        # 5.模型评估
        # 5.1 获取系数等值
        y_predict = estimator.predict(x_test)
        print("预测值为:
    ", y_predict)
        print("模型中的系数为:
    ", estimator.coef_)
        print("模型中的偏置为:
    ", estimator.intercept_)
    
        # 5.2 评价
        # 均方误差
        error = mean_squared_error(y_test, y_predict)
        print("误差为:
    ", error)
    
        return None
    

    4.2 梯度下降法

    def linear_model2():
        """
        线性回归:梯度下降法
        :return:None
        """
        # 1.获取数据
        data = load_boston()
    
        # 2.数据集划分
        x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22)
    
        # 3.特征工程-标准化
        transfer = StandardScaler()
        x_train = transfer.fit_transform(x_train)
        x_test = transfer.fit_transform(x_test)
    
        # 4.机器学习-线性回归(特征方程)
        estimator = SGDRegressor(max_iter=1000)
        estimator.fit(x_train, y_train)
    
        # 5.模型评估
        # 5.1 获取系数等值
        y_predict = estimator.predict(x_test)
        print("预测值为:
    ", y_predict)
        print("模型中的系数为:
    ", estimator.coef_)
        print("模型中的偏置为:
    ", estimator.intercept_)
    
        # 5.2 评价
        # 均方误差
        error = mean_squared_error(y_test, y_predict)
        print("误差为:
    ", error)
    
        return None
    

    我们也可以尝试去修改学习率

    estimator = SGDRegressor(max_iter=1000,learning_rate="constant",eta0=0.1)
    

    此时我们可以通过调参数,找到学习率效果更好的值。

  • 相关阅读:
    100 道 Linux 常见面试题
    借助Redis锁,完美解决高并发秒杀问题
    'cnpm'安装install
    Git常用命令及方法大全
    idea controller service impl mapper xml切换跳转快捷键
    idea创建springboot项目用阿里云镜像
    mybatis.type-aliases-package的作用和用法
    MyBatis Generator
    https://antdv.com/components/layout-cn/
    https://mvnrepository.com/search?q=mysql-connector-java //maven
  • 原文地址:https://www.cnblogs.com/deepend/p/14464049.html
Copyright © 2011-2022 走看看