Linear Regression(一)——

Linear Regression(一)——

定义

回归的定义

在平面上存在这些点我希望能用一条直线尽可能经过它们。
enter description here

于是我们画了下面的一条直线
enter description here
这样的过程就叫做回归。

这个过程中我们的目的其实就是寻找输入变量(自变量)和输出变量的关系(因变量)

线性回归的定义

线性回归:上图中我们进行的回归就是线性回归
线性回归实际上是假设输入变量x和输出变量y存在着这样的关系1113598-20170226122112054-1002595027.png
在刚刚的情况下,横坐标为1113598-20170226122112288-2046151264.png,那么公式上图可以表示为1113598-20170226122112507-2007813057.png,w实际上是这样1113598-20170226122112710-643716671.png的一个矩阵,而x实际上是1113598-20170226122112913-1464066703.png的这样一个矩阵,二者相乘即1113598-20170226122113132-1513182383.png

线性回归模型

损失函数

我们已经知道了线性回归的定义,那么如何寻找这样的一条直线呢?什么样的直线是最好的?
enter description here
这张图我们可以明显的看出蓝色的直线是最优的,我们判断的依据是什么?就是直线到各个点的距离
因此我们引出了损失函数
损失函数
1113598-20170226122113632-696241346.png各样本的输出,1113598-20170226122113851-1139692793.png各变量权重,1113598-20170226122114304-496363334.png输入变量

1113598-20170226122114507-378685220.png

我们的目的即最小化损失函数,使得到的直线到各点的距离最小

1113598-20170226122114726-9986607.png

损失函数最小化

最简便求得损失函数最小值的方法当然是求导,因此我们以一元线性模型进行说明
存在如下样本训练集T={(3,3),(4,3),(1,1)},求出它的回归直线
我们可以得到以下公式

1113598-20170226122115023-669972621.png

我们的目标是1113598-20170226122115273-757376283.png的最小化
1113598-20170226122115601-2098286820.png分别求a,b的偏导得
1113598-20170226122115851-988242301.png
1113598-20170226122116054-552732319.png
另两个偏导等于0可以求得a,b
*a=0.71428571,b=0.42857143
使用matplotlib画出图像
enter description here

使用最小二乘法求得最优解

上文对于一元线性回归方程使用基础的数学方法可得出结果,当求解多元线性回归方程,为了计算的简便,我们通常会将数据转化为矩阵,通过最小二乘法求出数据的结果。
我们已经知道损失函数的矩阵形式表示为
1113598-20170226122116710-1826228792.png
那么我们使用矩阵的求导方法即可得出最优的W
Wiki已经给出了推导,对于没有梯子的可以参照该博文最小二乘法的矩阵形式推导

通过最小二乘法我们得出最优的w为
1113598-20170226122116945-1691109272.png

# X,Y以上文举例分别为[3,2],[3,1]的矩阵
import numpy as np
X = [[3,1],[4,1],[1,1]]
Y= [[3],[3],[1]
X = np.mat(X)
Y = np.mat(Y)
w = (X.T*X).I*X.T*Y

enter description here

下载.png

转载于:https://www.cnblogs.com/lynsyklate/p/6444052.html

Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] warmUpExercise.m - Simple example function in Octave/MATLAB [?] plotData.m - Function to display the dataset [?] computeCost.m - Function to compute the cost of linear regression [?] gradientDescent.m - Function to run gradient descent [†] computeCostMulti.m - Cost function for multiple variables [†] gradientDescentMulti.m - Gradient descent for multiple variables [†] featureNormalize.m - Function to normalize features [†] normalEqn.m - Function to compute the normal equations ? indicates files you will need to complete † indicates optional exercises
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值