线性回归API再介绍、过拟合欠拟合以及小案例

六、线性回归API再次介绍

  • sklearn.linear_model.LinearRegression(fit_intercept=True)

    • 通过正规方程优化
    • 参数
      • fit_intercept:是否计算偏置
    • 属性
      • LinearRegression.coef_:回归系数
      • LinearRegression.intercept_:偏置
  • sklearn.linear_model.SGDRegressor(loss=‘squared_loss’,fi_intercept=True,learning_rate = ‘invscaling’,eta0=0.01)

    • SDGRegressor类实现了随机梯度下降学习,它支持不同的loss函数和正则化惩罚项来拟合线性回归模型。

    • 参数

      • loss :损失类型
        • loss=‘squared_loss’:普通最小二乘法
      • fit_intercept:是否计算偏置
      • learning_rate :string,optional
        • 学习率填充
        • ‘constant’:eta = eta0
        • ‘optimal’:eta = 1.0/(appha * (t+t0))[default]
        • ‘invscaling’:eta = eta0 / pow(t,power_t)
          • power_t = 0.25:存在父类当中
        • 对于一个常数值学习率来说,可以使用learning_rate=‘constant’,并使用eta0来指定学习率。
    • 属性:

      • SGDRegressor.coef_:回归系数
      • SGDRegressor.intercept_:偏置

七、案例1:波士顿房价预测

1.回归性能评估

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-syssnH6m-1575633334917)(file:///C:/Users/%E6%B8%85%E9%A3%8E/Desktop/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E8%AF%BE%E4%BB%B6/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E8%AE%B2%E4%B9%89/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%EF%BC%88%E7%AE%97%E6%B3%95%E7%AF%87%EF%BC%89/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92/images/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E8%AF%84%E4%BC%B0.png)]

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

  • sklearn.metrics.mean_squared_error(y_true,y_pred)
    • 均方误差回归损失
    • y_true:真实值
    • y_pred:预测值
    • return:浮点数结果
2.代码实现
def linear_demo1():
    
    '''
    线性回归:正规方程
    '''
    
    # 1.获取数据
    data = load_boston()
    
    # 2.数据集划分
    x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=22)
    
    # 3.特征工程 标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transfrom(x_train)
    x_test = transfer.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


def linear_demo2():
    
     '''
    线性回归:梯度下降法
    '''
    
    # 1.获取数据
    data = load_boston()
    
    # 2.数据集划分
    x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=22)
    
    # 3.特征工程 标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transfrom(x_train)
    x_test = transfer.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
    

八、欠拟合和过拟合

1.定义
  • 过拟合:一个假设在训练数据上能够获得比其他假设更好的拟合,但是在测试数据集上却不能很好的拟合数据,此时认为这个假设出现了过拟合的现象。(模型过于复杂)
  • 欠拟合:一个假设在训练数据上不能获得更好的拟合,并且在测试数据集上也不能很好的拟合而数据,此时认为这个假设出现了欠拟合的现象。(模型过于简单)
2.原因及解决办法
  • 欠拟合原因以及解决办法
    • 原因:学习到的数据特征过少
    • 解决办法:
      • 1)添加其他特征项
      • 2)添加多项式特征
  • 过拟合原因以及解决原因
    • 原因:原始特征过多,存在一些嘈杂特征,模型过于复杂是因为模型尝试去兼顾各个测试数据点
    • 解决办法:
      • 1)重新清洗数据
      • 2)增大数据的训练量
      • 3)正则化
      • 4)减少特征维度,防止维灾难
3.正则化

1.什么是正则化

在解决回归过拟合中,我们选择正则化。但是对于其他机器学习算法如分类算法来说也会出现这样的问题,

除了一些算法本身作用之外(决策树、神经网络),我们更多的也是去自己做特征选择,包括之前说的删除、合并一些特征。

2.如何解决?

在学习的时候,数据提供的特征有些影响模型复杂度或者这个特征的数据点异常较多,所以算法在学习的时候尽量减少这个特征的影响(甚至删除某个特征的影响),这就是正则化。

注意:调整时候,算法并不知道某个特征影响,而是去调整参数得出优化的结果。

3.正则化类别

  • L2正则化
    • 作用:可以使得其中一些W的都很小,都接近于0,削弱某个特征的影响
    • 优点:越小的参数说明模型越简单,越简单的模型越不容易产生过拟合现象
    • Ridge回归
  • L1正则化
    • 作用:可以使得其中一些W的值直接为0,删除这个特征的影响
    • LASSO回归
发布了104 篇原创文章 · 获赞 33 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/WangTaoTao_/article/details/103428816