Machine learning linear regression (six): Linear regression from entry to the master - the normal equation optimization and gradient descent optimization method

Introduction to Linear Regression

Linear regression scenarios

  • Prices forecast

  • Sales Prediction

  • Loan amount forecast

    For example:

Here Insert Picture Description

What is the linear regression

Definitions and formulas

Linear Regression (Linear regression) using the regression equation (function) for between the independent variables (characteristic value) and the dependent variable (target value) of one or more relationships for modeling an analytical way.

  • Features: with only one independent variable is called a single-variable regression, more than one independent variable is called multiple regression case

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-RsMPZw2P-1582644074633) (../ images /% E7% BA% BF% E6% 80% A7% E5 % 9B% 9E% E5% BD% 92% E5% 85% AC% E5% BC% 8F.png)]

  • Examples denoted by linear regression matrix

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-7zfrOpB6-1582644074633) (../ images /% E7% BA% BF% E6% 80% A7% E5 % 9B% 9E% E5% BD% 92% E7% 9F% A9% E9% 98% B5% E8% A1% A8% E7% A4% BA% E6% B3% 95.png)]

So how to understand it? Let's look at a few examples

  • Final grade: 0.7 × test scores + 0.3 × normal results
  • House prices from the central area = 0.02 × + 0.04 × city nitric oxide concentration + (-0.12 × average rate from the housing) + 0.254 × urban crime

Two examples above, we see the establishment of a relationship between the characteristic value and the target value, this relationship can be understood as a linear model .

Analysis of the relationship between characteristics and objectives of the linear regression

There are two main linear regression model which, ** one is a linear relationship, the other is a non-linear relationship. ** Here we can only draw a plane to be better understood, the features are for example a single or two sub-features.

  • Linear relationship

    • Univariate linear relationship:

    [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-1zufibCT-1582644074634) (../ images /% E7% BA% BF% E6% 80% A7% E5 % 85% B3% E7% B3% BB% E5% 9B% BE.png)]

    • Multivariate linear relationship

      [Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-kartzUtu-1582644074634) (../ images /% E5% A4% 9A% E5% 8F% 98% E9 % 87% 8F% E7% BA% BF% E6% 80% A7% E5% 85% B3% E7% B3% BB.png)]

NOTE: Relation between the target characteristics and a linear relationship, or wherein the target value exhibits two planar relationship

The higher dimensions we do not have to think, you can remember this relationship

  • Non-linear relationship

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4QI4a51A-1582644074635)(../images/%E9%9D%9E%E7%BA%BF%E6%80%A7%E5%85%B3%E7%B3%BB.png)]

Comment: Why is this relationship? what is the reason?

If the non-linear relationship, the regression equation can be understood as: w 1 x 1 + w 2 x 2 2 + w3x3 2

Preliminary using linear regression api

Linear regression API

  • sklearn.linear_model.LinearRegression()
    • LinearRegression.coef_:回归系数

举例

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JUx4Y2dp-1582644173290)(../images/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92api%E4%BD%BF%E7%94%A8%E4%B8%BE%E4%BE%8B.png)]

步骤分析

  • 1.获取数据集
  • 2.数据基本处理(该案例中省略)
  • 3.特征工程(该案例中省略)
  • 4.机器学习
  • 5.模型评估(该案例中省略)

代码过程

  • 导入模块
from sklearn.linear_model import LinearRegression
  • 构造数据集
x = [[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]]
y = [84.2, 80.6, 80.1, 90, 83.2, 87.6, 79.4, 93.4]
  • 机器学习-- 模型训练
# 实例化API
estimator = LinearRegression()
# 使用fit方法进行训练
estimator.fit(x,y)

estimator.coef_

estimator.predict([[100, 80]])

数学:求导

常见函数的导数

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u27MomXt-1582644216404)(../images/%E6%B1%82%E5%AF%BC%E5%85%AC%E5%BC%8F.png)]

导数的四则运算

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2V1EC4Yr-1582644216405)(../images/%E6%B1%82%E5%AF%BC%E5%9B%9B%E5%88%99%E8%BF%90%E7%AE%97.png)]

练习

  • 3.1 y = x3-2x2+sinx,求f`(x)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m0ah97RN-1582644216406)(../images/%E6%B1%82%E5%AF%BC%E4%B8%BE%E4%BE%8B1.png)]

  • 3.2 y=ln(sinx), 求dy/dx

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mbecJNj8-1582644216406)(../images/%E6%B1%82%E5%AF%BC%E4%B8%BE%E4%BE%8B2.png)]

矩阵(向量)求导 [了解]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jf2SoSYM-1582644216406)(../images/%E7%9F%A9%E9%98%B5%E6%B1%82%E5%AF%BC.png)]

线性回归的损失和优化

假设刚才的房子例子,真实的数据之间存在这样的关系

真实关系:真实房子价格 = 0.02×中心区域的距离 + 0.04×城市一氧化氮浓度 + (-0.12×自住房平均房价) + 0.254×城镇犯罪率

那么现在呢,我们随意指定一个关系(猜测)

随机指定关系:预测房子价格 = 0.25×中心区域的距离 + 0.14×城市一氧化氮浓度 + 0.42×自住房平均房价 + 0.34×城镇犯罪率

请问这样的话,会发生什么?真实结果与我们预测的结果之间是不是存在一定的误差呢?类似这样样子

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KyKx08Hm-1582644298832)(../images/%E6%88%BF%E5%B1%8B%E4%BB%B7%E6%A0%BC%E5%9B%9E%E5%BD%92%E4%B8%BE%E4%BE%8B.png)]

既然存在这个误差,那我们就将这个误差给衡量出来

损失函数

总损失定义为:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ojZUeI4o-1582644298834)(../images/%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E6%8D%9F%E5%A4%B1%E5%87%BD%E6%95%B0.png)]

  • yi为第i个训练样本的真实值
  • h(xi)为第i个训练样本特征值组合预测函数
  • 又称最小二乘法

如何去减少这个损失,使我们预测的更加准确些?既然存在了这个损失,我们一直说机器学习有自动学习的功能,在线性回归这里更是能够体现。这里可以通过一些优化方法去优化(其实是数学当中的求导功能)回归的总损失!!!

优化算法

如何去求模型当中的W,使得损失最小?(目的是找到最小损失对应的W值)

线性回归经常使用的两种优化算法

正规方程

什么是正规方程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-47oY5KPH-1582644298834)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B.png)]

理解:X为特征值矩阵,y为目标值矩阵。直接求到最好的结果

缺点:当特征过多过复杂时,求解速度太慢并且得不到结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-18b7LGfy-1582644298835)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B%E6%B1%82%E8%A7%A3%E5%9B%BE%E7%A4%BA.png)]

正规方程求解举例

以下表示数据为例:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WLFiJmyE-1582644298835)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B%E4%B8%BE%E4%BE%8B1.png)]

即:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e3tKQthV-1582644298835)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B%E4%B8%BE%E4%BE%8B2.png)]

运用正规方程方法求解参数:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-N0aCmnQe-1582644298836)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B%E4%B8%BE%E4%BE%8B3.png)]

正规方程的推导

  • 推导方式一:

把该损失函数转换成矩阵写法:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6R7MGI26-1582644298836)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B%E6%8E%A8%E5%AF%BC1.png)]

其中y是真实值矩阵,X是特征值矩阵,w是权重矩阵

对其求解关于w的最小值,起止y,X 均已知二次函数直接求导,导数为零的位置,即为最小值。

求导:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cyOWZpj7-1582644298836)(../images/%E6%AD%A3%E8%A7%84%E6%96%B9%E7%A8%8B%E6%8E%A8%E5%AF%BC2.png)]

Note: (1) to the formula (2) derivation, X is a matrix of m rows and n columns, does not guarantee that there is an inverse matrix, but the right to take it into a matrix XT, to ensure that there is an inverse matrix .

Formula (5) to Formula (6) derivation process, and the similar.

  • Derivation of the two [expand]:

https://www.jianshu.com/p/2b6633bd4d47

Published 529 original articles · won praise 659 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104508107