线性回归算法数学原理

内部AI (Inside AI)

Linear regression is one of the most popular algorithms used in different fields well before the advent of computers. Today with the powerful computers, we can solve multi-dimensional linear regression which was not possible earlier. In single or multidimensional linear regression, the basic mathematical concept is quite the same.

在计算机出现之前,线性回归是在不同领域中使用最广泛的算法之一。 如今,借助功能强大的计算机,我们可以解决以前无法实现的多维线性回归。 在一维或多维线性回归中,基本的数学概念完全相同。

Today with machine learning libraries, like Scikit-learn, it is possible to use the linear regression in modelling without understanding the mathematical concept behind it. In my opinion, it is quite essential for a data scientist and machine learning professional to understand the mathematical concept and logic behind an algorithm before using it.

如今, 借助Scikit-learn之类的机器学习库,可以在建模中使用线性回归而无需了解其背后的数学概念。 我认为,对于数据科学家和机器学习专业人员来说,在使用算法之前了解其数学概念和逻辑非常重要。

Most of us may not have studied advanced mathematics and statistics, and we get scared by seeing the mathematical notation and jargon behind the algorithms. In this article, I will explain the math and logic behind linear regressions with simplified python code and easy math to build your understanding

我们大多数人可能没有研究过高级数学和统计学,而看到算法背后的数学符号和行话让我们感到害怕。 在本文中,我将通过简化的python代码和简单的数学方法解释线性回归背后的数学和逻辑,以帮助您理解

Overview

总览

We will start with a simple linear equation with one variable and without any intercept/bias. First, we will learn the step by step approach taken by packages like Scikit-learn to solve linear regression. During this walkthrough, we will understand the important concept of Gradient Descent. Further, we will see an example with a simple linear equation with one variable and an intercept/bias.

我们将从具有一个变量且没有任何截距/偏差的简单线性方程式开始。 首先,我们将学习Scikit-learn之类的软件包采用的逐步方法来解决线性回归问题。 在本演练中,我们将了解“梯度下降”的重要概念。 此外,我们将看到一个带有一个变量和一个截距/偏置的简单线性方程的示例。

Step 1: We will use the python package NumPy for working with a sample dataset and Matplotlib to plot various graphs for visualisation.

步骤1:我们将使用python软件包NumPy处理示例数据集,并使用Matplotlib绘制各种图形以进行可视化。

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt

Step 2: Let us consider a simple scenario where a single input /independent variable controls the outcome/dependent variable value. In the code below, we have declared two NumPy arrays to hold the values of the independent and dependent variables.

步骤2:让我们考虑一个简单的场景,其中单个输入/独立变量控制结果/因变量值。 在下面的代码中,我们声明了两个NumPy数组来保存自变量和因变量的值。

Independent_Variable=np.array([1,2,3,12,15,17,20,21,5,7,9,10,3,12,15,17,20,7])Dependent_Variable=np.array([7,14,21,84,105,116.1,139,144.15,32.6,50.1,65.4,75.4,20.8,83.4,103.15,110.9,136.6,48.7])

Step 3: Let us quickly draw a scatter plot to understand the data points.

步骤3:让我们快速绘制散点图以了解数据点。

plt.scatter(Independent_Variable, Dependent_Variable,  color='green')plt.xlabel('Independent Variable/Input Parameter')plt.ylabel('Dependent Variable/ Output Parameter')plt.show()

Our goal is to formulate a linear equation which can predict the dependent variable value with minimum error for an independent/input variable.

我们的目标是制定一个线性方程式,该方程式可以预测自变量/输入变量的因变量值,并且误差最小。

Dependent Variable = Constant * Independent Variable

因变量=常数*自变量

In mathematical terms, Y=constant*X

用数学术语,Y =常数* X

In terms of visualisation, we need to find the best fit line to get a minimum error for points.

在可视化方面,我们需要找到最佳拟合线以使点的误差最小。

Minimum error is also known as loss function in the machine learning world.

最小误差在机器学习领域也被称为损失函数。

Loss function formula ( written by the author in word and then taken a screenshot)
损失函数公式(由作者用文字写成,然后截取屏幕截图)

We can calculate the loss in the iteration for each assumed constant value in the equation Y=constant*X with all independent data points. The goal is to find the constant for which this loss is minimum and formulate the equation. Please note that in the loss function equation “m” stands for the number of points. In the current example, we have 18 points hence 1/2m translates to 1/36. Do not get terrified by seeing the loss function formula. We calculate the loss as the summation of the square of the difference between the calculated value and actual value for each data points and then divide it with twice the number of points. We will decipher it step by step in the article below with the help of code in python.

我们可以在方程式Y = constant * X中使用所有独立的数据点,为每个假定的常数值计算迭代中的损失。 目的是找到损耗最小的常数,并公式化。 请注意,在损失函数方程中,“ m”代表点数。 在当前示例中,我们有18个点,因此1 / 2m转换为1/36。 不要因为看到损失函数公式而感到恐惧。 我们将损失计算为每个数据点的计算值与实际值之差的平方之和,然后将其除以点数的两倍。 我们将在下面的文章中借助python中的代码逐步解释它。

Step 4: To understand the core idea and math behind identifying the equation, we will consider the limited set of constant values mentioned in the code below and calculate the loss function.

步骤4:要了解识别方程式背后的核心思想和数学运算,我们将考虑以下代码中提及的有限常量集,并计算损失函数。

In actual linear regression algorithms, in particular gaps, constants are considered for loss function calculation. Initially, the gaps between the two constants considered for loss function calculation is bigger. As we move closer to the actual solution constants with smaller gaps are considered. In the machine learning world, the learning rate is the gap in which constant is increased/decreased for loss function calculation.

在实际的线性回归算法中,尤其是在间隙中,考虑常数用于损失函数计算。 最初,用于损失函数计算的两个常数之间的差距较大。 随着我们越来越接近实际的解决方案常数,将考虑较小的差距。 在机器学习世界中,学习率是指为进行损失函数计算而增加/减少常数的差距。

m=[-5,-3,-1,1,3,5,6.6,7,8.5,9,11,13,15]

Step 5: In the code below, we calculate the loss function for each value of constant (i.e. values in the list m declared in the earlier step) for all input and output data points.

步骤5:在下面的代码中,我们为所有输入和输出数据点的每个常数值(即,在前面的步骤中声明的列表m中的值)计算损耗函数。

We store the calculated loss for each constant in a Numpy array “errormargin”.

我们将计算出的每个常数的损耗存储在Numpy数组“ errormargin”中。

errormargin=np.array([])for slope in m:    counter=0    sumerror=0    cost=sumerror/10    for x in Independent_Variable:        yhat=slope*x        error=(yhat-Dependent_Variable[counter])*(yhat-Dependent_Variable[counter])        sumerror=error+sumerror        counter=counter+1      cost=sumerror/18    errormargin=np.append(errormargin,cost)

Step 6:We will plot the calculated loss function for the constant to determine the actual constant value.

步骤6:我们将为常数绘制计算出的损失函数,以确定实际常数值。

plt.plot(m,errormargin)plt.xlabel("Slope Values")plt.ylabel("Loss Function")plt.show()

The value of the constant for which the curve is at the lowest point is the real constant with which we can formulate the equation of the line.

曲线处于最低点的常数的值是实常数,我们可以用它来表示直线方程。

In our example, for the value of constant 6.8, the curve is at the lowest point.

在我们的示例中,对于常数6.8而言,曲线位于最低点。

A line with this value as Y=6.8*X can best fit the data points with minimum error.

该值为Y = 6.8 * X的线可以以最小的误差最好地拟合数据点。

This approach of plotting the loss function and identifying the true values of the fixed parameters in the equation at the lowest point of the loss curve is known as gradient descent. As an example, we have considered one variable for simplicity, hence the loss function is a 2-dimensional curve. In the case of multiple linear regression, the gradient descent curve will be multi-dimensional.

这种在损失曲线的最低点绘制损失函数并确定方程式中固定参数的真值的方法称为梯度下降 。 例如,为简单起见,我们考虑了一个变量,因此损失函数为二维曲线。 在多元线性回归的情况下,梯度下降曲线将是多维的。

We have learnt the inner working to calculate the coefficient of the independent variable. Next, let us learn the step by step way to calculate the coefficient and intercept/bias in linear regression.

我们已经了解了计算自变量系数的内部工作。 接下来,让我们逐步学习在线性回归中计算系数和截距/偏差的方法。

Step 1: Just like earlier, let us consider a sample set of independent and dependent variable values. These are the input and output data points available our goal is to formulate a linear equation which can predict the dependent variable value with minimum error for an independent/input variable.

步骤1:就像之前一样,让我们​​考虑一组独立变量和因变量值。 这些是可用的输入和输出数据点,我们的目标是建立一个线性方程式,该方程式可以预测自变量/输入变量的因变量值,并且误差最小。

Dependent Variable = (Coefficient*Independent Variable)+ Constant

因变量=(系数*因变量)+常数

In mathematical terms, y=(Coefficient*x)+ c

用数学术语,y =(系数* x)+ c

Please note that coefficient is also a constant term multiplied with the independent variable in the equation.

请注意,系数也是常数项乘以方程式中的独立变量。

Independent_Variable=np.array([1,2,4,3,5])Dependent_Variable=np.array([1,3,3,2,5])

Step 2: We will assume that the initial value of the coefficient and constant “m” and “c” respectively as zero. We will increase the value of m and c after each iteration of error calculation by a small learning rate of 0.001. Epoch is the number of times we want to do this calculation on the entire available data points. As we increase the number of epoch, the solution becomes more accurate, but it consumes time and computing power. Based on the business case, we can decide the acceptable error in the calculated values to stop the iterations.

步骤2:我们假设系数的初始值和常数“ m”和“ c”分别为零。 每次误差计算迭代后,我们将m和c的值增加一个小的学习率0.001。 时期是我们要对整个可用数据点进行此计算的次数。 随着我们增加纪元的数量,解决方案变得更加准确,但是却消耗了时间和计算能力。 根据业务案例,我们可以确定计算值中可接受的误差以停止迭代。

LR=0.001m=0c=0epoch=0

Step 3: In the below code, we run 1100 iterations on the available dataset and calculate the coefficient and constant value.

步骤3:在下面的代码中,我们在可用数据集上运行1100次迭代,并计算系数和常数值。

For each independent data point, we calculate the dependent value (i.e. yhat) and then calculate the error between the calculated and actual dependent value.

对于每个独立数据点,我们计算相关值(即yhat),然后计算所计算的相关值与实际相关值之间的误差。

Based on the error, we change the value of coefficient and constant for the next iteration calculation.

根据该误差,我们更改系数和常数的值,以进行下一次迭代计算。

New coefficient= Current coefficient — (Learning Rate*Error)

新系数=当前系数-(学习率*错误)

New Constant = Current Constant -(Learning Rate*Error*Independent Variable Value)

新常数=当前常数-(学习率*错误*独立变量值)

while epoch<1100:    epoch=epoch+1    counter=0    for x in Independent_Variable:        yhat=(m*x)+c        error=yhat-Dependent_Variable[counter]        c=c-(LR*error)        m=m-(LR*error*x)        counter=counter+1

We check the value of the coefficient and constant after 1100 iterations on the available dataset.

我们在可用数据集上进行1100次迭代后检查系数和常数的值。

print("The final value of  m", m)print("The final value of  c", c)

Mathematically, it can be represented as y=(0.81*x)+0.33

数学上可以表示为y =(0.81 * x)+0.33

Finally, let us compare the earlier output with the Scikit-learn linear regression algorithm result

最后,让我们将早期输出与Scikit-learn线性回归算法的结果进行比较

from sklearn.linear_model import LinearRegressionreg = LinearRegression().fit(Independent_Variable.reshape(-1,1), Dependent_Variable)print(reg.coef_)print(reg.intercept_)

With 1100 iterations on the available dataset the calculated value of the coefficient and constant/bias is very close to the output of Scikit-learn linear regression algorithm.

在可用数据集上进行1100次迭代后,系数和常数/偏置的计算值非常接近Scikit-learn线性回归算法的输出。

I hope this gives article gave you a firm understanding on behind the scene mathematical calculation and concept in linear regression. Also, we have seen the way gradient descent is applied to find the optimal solution. In the case of multiple linear regression, the math and logic remain the same, and it is just scaled further in more dimensions.

我希望本文能使您对线性回归的数学计算和概念有一个深刻的了解。 同样,我们已经看到了应用梯度下降法找到最佳解的方法。 在多元线性回归的情况下,数学和逻辑保持不变,并且只是在更多维度上进行了进一步扩展。

翻译自: https://towardsdatascience.com/linear-regression-algorithm-under-the-hood-math-for-non-mathematicians-c228d244e3f3

线性回归算法数学原理


http://www.taodudu.cc/news/show-997366.html

相关文章:

  • Streamlit —使用数据应用程序更好地测试模型
  • lasso回归和岭回归_如何计划新产品和服务机会的回归
  • 贝叶斯 定理_贝叶斯定理实际上是一个直观的分数
  • 文本数据可视化_如何使用TextHero快速预处理和可视化文本数据
  • 真实感人故事_您的数据可以告诉您真实故事吗?
  • k均值算法 二分k均值算法_使用K均值对加勒比珊瑚礁进行分类
  • 衡量试卷难度信度_我们可以通过数字来衡量语言难度吗?
  • 视图可视化 后台_如何在单视图中可视化复杂的多层主题
  • python边玩边学_边听边学数据科学
  • 边缘计算 ai_在边缘探索AI!
  • 如何建立搜索引擎_如何建立搜寻引擎
  • github代码_GitHub启动代码空间
  • 腾讯哈勃_用Python的黑客统计资料重新审视哈勃定律
  • 如何使用Picterra的地理空间平台分析卫星图像
  • hopper_如何利用卫星收集的遥感数据轻松对蚱hopper中的站点进行建模
  • 华为开源构建工具_为什么我构建了用于大数据测试和质量控制的开源工具
  • 数据科学项目_完整的数据科学组合项目
  • uni-app清理缓存数据_数据清理-从哪里开始?
  • bigquery_如何在BigQuery中进行文本相似性搜索和文档聚类
  • vlookup match_INDEX-MATCH — VLOOKUP功能的升级
  • flask redis_在Flask应用程序中将Redis队列用于异步任务
  • 前馈神经网络中的前馈_前馈神经网络在基于趋势的交易中的有效性(1)
  • hadoop将消亡_数据科学家:适应还是消亡!
  • 数据科学领域有哪些技术_领域知识在数据科学中到底有多重要?
  • 初创公司怎么做销售数据分析_为什么您的初创企业需要数据科学来解决这一危机...
  • r软件时间序列分析论文_高度比较的时间序列分析-一篇论文评论
  • selenium抓取_使用Selenium的网络抓取电子商务网站
  • 裁判打分_内在的裁判偏见
  • 从Jupyter Notebook切换到脚本的5个理由
  • ip登录打印机怎么打印_不要打印,登录。

线性回归算法数学原理_线性回归算法-非数学家的高级数学相关推荐

  1. moead算法流程步骤_匈牙利算法原理与实现

    本文介绍了匈牙利算法的计算流程和代码实现,欢迎各位抛砖.原理部分正在整理中... 分配问题 分配问题涉及将机器分配给任务,将工人分配给工作,将足球运动员分配给职位等. 目标是确定最佳分配,例如,使总成 ...

  2. 算法的优缺点_机器学习算法优缺点 amp; 如何选择

    (点击上方公众号,可快速关注) 转自: 算法与数学之美 主要回顾下几个常用算法的适应场景及其优缺点! 机器学习算法太多了,分类.回归.聚类.推荐.图像识别领域等等,要想找到一个合适算法真的不容易,所以 ...

  3. 匈牙利算法java实现_匈牙利算法(Hungarian Algorithm)

    匈牙利算法是一种在多项式时间内求解任务分配问题的组合优化算法.换句话说就是,在可以接受的时间内去做匹配. 1. 描述问题 给定2个集合A和B,然后将AB中的元素完成一个连线.(这不就是小时候的连线题么 ...

  4. labuladong 的算法小抄_关于算法笔试的几个套路,一点就透

    以下文章来源于labuladong ,作者labuladong 我知道各位是被标题吸引进来的,那就不废话,先说几个算法笔试的硬核套路,再说说语言选择和做题复习的策略. 避实就虚 大家也知道,大部分笔试 ...

  5. 自然哲学的数学原理_慢跑的数学原理(一):珍惜做的每一份功

    嘿嘿,骗你们来听物理了.       --Issac Newton 01 一点前言 好久没有更公众号了,向大家道个歉哈.笔者经历了期中考试数院惨绝人寰的的屠戮,心态-男儿有泪不轻弹,也不对外哈.闲话少 ...

  6. python做线性回归统计推断提取参数_线性回归及其Python实现(最大似然法)

    线性回归及其Python实现(最大似然法) 标签: Python 机器学习 本节内容总结于博主在牛客网机器学习特训营的笔记 参考资料:<机器学习实战> 目录 1.什么是线性回归(Linea ...

  7. java贪心算法 区间调度_贪心算法-区间调度问题解之证明(示例代码)

    一.贪心算法 定义:一个算法是贪心算法,如果它是通过一些小的步骤来一个求解,并且在每一步根据局部情况选择一个决定,使得某些主要的指标得到优化. 二.区间调度问题 1. 问题:我们有一组需求{1,2,3 ...

  8. l bfgs算法java代码_优化算法——拟牛顿法之L-BFGS算法

    一.BFGS算法 BFGS算法的校正公式: 利用Sherman-Morrison公式可对上式进行变换,得到 令 ,则得到: 二.BGFS算法存在的问题 在BFGS算法中.每次都要存储近似Hesse矩阵 ...

  9. python验证数学原理_一起学opencv-python九(性能的测量和优化与图像处理的数学原理)...

    图像混合 其实用的就是cv2.addWeighted函数,前面也介绍过这个函数.不过首先得把图片的形状搞成一样的,这个画图可以做到. 接下来是一块比较重要的内容. 性能的测量和优化 参考 https: ...

最新文章

  1. invalid dts/pts combination
  2. Database之SQL:自定义创建数据库的各种表demo集合(以方便理解和分析sql的各种增删改查语法的具体用法)
  3. 后端技术:盘点HashMap 的7种遍历方式
  4. oracle中sql语句的优化(转帖)
  5. Jmeter如何在大并发测试下,让登录或者后续接口只执行一次?
  6. 【Log4j】Jboss下配置log4j简记
  7. (对比PDF)Adobe Acrobat DC 离线对比PDF、draftable.com/compare 在线对比PDF
  8. Objective-C 协议(protocol)
  9. Caused by: java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected
  10. Linux实现黑客帝国背景炫酷效果
  11. MySQL卸载不干净问题
  12. MAC协议微信自动生成带备注固定收款码免挂机监控协议回调
  13. 解决“npm WARN requires a peer of XXX“问题
  14. 小攻是鸿蒙小受是鲲鹏,洪荒之鸿蒙大天尊
  15. Android WebView 不能弹出alert的对话框
  16. 江苏农村商业银行计算机类笔试考什么时候,2020江苏农商行春季校园招聘笔试考什么?...
  17. 极其简单的响应式的模块化布局、看板布局 js 工具
  18. 公历转农历、生肖、干支纪年、节气算法研究和C程序实现
  19. 田忌赛马 贪心 注意细节
  20. 医院计算机招聘笔试内容是什么,医院招聘考试的“内幕”难道你不想知道吗?...

热门文章

  1. java struts2 ajax_在struts2的Action中返回Ajax数据
  2. Codeforces 408D Long Path (DP)
  3. 人月神话阅读笔记(二)
  4. 对Faster R-CNN的理解(1)
  5. 中英文对照 —— 机械
  6. VS2010中的快捷键
  7. LeetCode 7 Reverse Integer(反转数字)
  8. eclipse编辑jsp文件和javascript代码很卡解决办法
  9. vs2005的webbrowser控件如何接收鼠标事件
  10. VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)