线性回归闭式解推导

单词:

multivariate linear regression  多元线性回归

Here I want to show how the normal equation is derived. 此处是如何获得该等式。

Given the hypothesis function. 给出假设函数。 [haɪˈpɒθəsɪs]

 

多元线性回归应用举例:

幸福度预测:有身体、财富、学历等等自变量因素,有幸福度因变量因素,有一些样本数据,希望得到一个从这些自变量到幸福度这个因变量的映射函数。

 

解析过程:

回归函数:

\begin{equation}
h_{\theta}(x)=\theta_{0} x_{0}+\theta_{1} x_{1}+\cdots+\theta_{n} x_{n}
\end{equation}

最小化平方差损失:

\begin{equation}
J\left(\theta_{0 \ldots n}\right)=\frac{1}{2 m} \sum_{i=1}^{m}\left(h_{\theta}\left(x^{(i)}\right)-y^{(i)}\right)^{2}
\end{equation}

此处的$x^{(i)}$和$y^{(i)}$是第i个样本数据。

我们需要学习的参数$\theta$可以,可以视为一个列向量:

\begin{equation}
\left( \begin{array}{c}{\theta_{0}} \\ {\theta_{1}} \\ {\dots} \\ {\theta_{n}}\end{array}\right)
\end{equation}

这样回归函数就是:$h_{\theta}(x)=x{\theta}$ 。${x}$是行向量形式。

 对于求和运算,实际上也可以变换成矩阵相乘的形式。上面的最小平方差损失,可以变换为:

\begin{equation}
J(\theta)=\frac{1}{2 m}(X \theta-y)^{T}(X \theta-y)
\end{equation}

这个地方的$X$是$m$行,$n$列的,$m$是样本数目,$n$是样本中的变量数目。$y$是一个列向量。

不去考虑前面的$\frac{1}{2 m}$这个系数。利用线性代数的知识将括号去掉:

\begin{equation}
\begin{array}{c}{J(\theta)=\left((X \theta)^{T}-y^{T}\right)(X \theta-y)} \\ {J(\theta)=(X \theta)^{T} X \theta-(X \theta)^{T} y-y^{T}(X \theta)+y^{T} y}\end{array}
\end{equation}

注意到:$X \theta$实际上一个列向量,$y$也是一个列向量,那么$(X \theta)^{T} y$和$y^{T}(X \theta)$是相等的。上式可以简化为:

\begin{equation}
J(\theta)=\theta^{T} X^{T} X \theta-2(X \theta)^{T} y+y^{T} y
\end{equation}

此处的$\theta$是未知数,可以对其求取偏微分运算,但是$\theta$是一个向量,这就涉及到向量求导了。

\begin{equation}
\frac{\partial J}{\partial \theta}=2 X^{T} X \theta-2 X^{T} y=0
\end{equation}

\begin{equation}
X^{T} X \theta=X^{T} y
\end{equation}

\begin{equation}
\theta=\left(X^{T} X\right)^{-1} X^{T} y
\end{equation}

posted @ 2019-04-26 09:41  阿刚的代码进阶之旅  阅读(2782)  评论(0编辑  收藏  举报