其他分享
首页 > 其他分享> > Andrew Ng Machine Learning Notes

Andrew Ng Machine Learning Notes

作者:互联网

Source: Coursera Machine Learning provided by Stanford University Andrew Ng - Machine Learning | Coursera


Introduction

definition: 

A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.

Tom Mitchell

classification:    supervised learning & unsupervised learning

                       supervised learning: regression & classification

                       unsupervised learning: clustering & non-clustering (e.g. the "Cocktail Party Algorithm")


Supervised Learning - Linear Regression

notations: 

$x^{(i)}$ : the fearture(s) / input variable(s) of the i-th training example   $x_{j}^{(i)}$ : the j-th feature of the i-th training example

$y^{(i)}$ : the output / target variable of the i-th training example

$X$ : the space of features

$Y$ : the space of output variables

$m$ : the number of training examples $abbr.$ # training examples

$n$ : the number of  features

$\theta$ : the parameters of the hypothesis              $\theta_{j}$ : the j-th parameter corresponding to the j-th feature

hypothesis $h_{\theta}(x)$ : 

$$ h_{\theta}(x) = \theta_{0}x_{0} + \theta_{1}x_{1} + ... + \theta_{n}x_{n} = \sum_{j=0}^{n} \theta_{j}x_{j} $$

where $x_{0} = 1$ is an added feature for vectorization.

cost function $J(\theta)$ : 

$$J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)}) - y^{(i)})^{2}$$

This is also called the "squared error function" / "mean squared error". The halved mean ($\frac{1}{2}$) is for the convenience of the computation of gradient descent.

The goal of the algorithm should be to find $\theta$'s, so that $J(\theta)$ is minimized.

gradient descent: 

start with: $\theta = 0$ (or other values)

repeat until convergence: $\theta_{j} = \theta_{j} - \alpha \frac{\partial}{\partial \theta_{j}} J(\theta)$

where $\alpha$ is the learning rate. Larger $\alpha$ results in bigger steps and vice versa. Adjust the value according to:

Computing $\frac{\partial}{\partial \theta_{j}} J(\theta)$ for linear regression:

$$\frac{\partial}{\partial \theta_{j}} J(\theta) = \frac{\partial}{\partial \theta_{j}} \frac{1}{2m} \sum_{i=1}^{m} (h_{\theta}(x^{(i)}) - y^{(i)})^{2} $$

$$= \frac{1}{2m} \frac{\partial}{\partial \theta_{j}} \sum_{i=1}^{m} (\theta_{j}x_{j}^{(i)} + \sum_{k=0,k\neq j}^{n}\theta_{k}x_{k}^{(i)} - y^{(i)})^{2}$$

$$= \frac{1}{2m}  2 \sum_{i=1}^{m}(h_{\theta}(x^{(i)}) - y^{(i)})x_{j}^{(i)}$$

thus, 

$\theta_{j} = \theta_{j} - \alpha \frac{1}{m}  \sum_{i=1}^{m}(h_{\theta}(x^{(i)}) - y^{(i)})x_{j}^{(i)}$

In each iteration, $\theta$'s should be simultaneously updated.

With different starting points, gradient descent may end up at different local extrema. In linear regression problems, the cost function $J(\theta)$ is always a convex function. So gradient descent will correctly find the only global extrema.

Specifically, the above algorithm is called batch gradient descent where each step uses all the training examples.

feature scaling and mean normalization: 

$\theta$ will descend quickly on small ranges and slowly on large ranges, and so will oscillate inefficiently down to the optimum when the variables are very uneven. So we modify the ranges of different features so that they are all roughly the same by applying: 

$$x_{j}^{(i)} = \frac{x_{j}^{(i)} - \mu_{j}}{s_{j}}$$

where $\mu_{j}$ is the mean of the j-th feature, $s_{j}$ is the range or standard deviation of the j-th feature.

polynomial regression: The above algorithm can be applied to solve for situations where the hypothesis $h_{\theta}(x)$ is a polynomial. Just make the terms with other degrees new features.

vectorization for implementation:

Index ranges from 1 to $n+1$ as-is in Matlab.

$ X = \begin{bmatrix} x^{(1)} \\ x^{(2)} \\ ... \\ x^{(m)} \\ \end{bmatrix} = \begin{bmatrix} x_{1}^{(1)} & x_{2}^{(1)} & ... & x_{n+1}^{(1)} \\ x_{1}^{(2)} & x_{2}^{(2)} & ... & x_{n+1}^{(2)} \\ . & . & . & . \\ x_{1}^{(m)} & x_{2}^{(m)} & ... & x_{n+1}^{(m)} \\ \end{bmatrix} $                                $y = \begin{bmatrix} y^{1} \\ y^{2} \\ ... \\ y^{m} \\ \end{bmatrix}$                                $\theta = \begin{bmatrix} \theta_{1} \\ \theta_{2} \\ ... \\ \theta_{n+1} \\ \end{bmatrix}$

$$ h_{\theta}(X) = \begin{bmatrix} h_{\theta}(x^{(1)}) \\ h_{\theta}(x^{(2)}) \\ ... \\ h_{\theta}(x^{(m)})\\ \end{bmatrix} = X \theta $$

thus, J = sum( (X * theta - y) .^ 2 ) / (2 * m);

$$ \theta = \theta - \alpha \frac{1}{m} X^{T}(X\theta - Y) X$$

i.e. theta = theta - alpha / m * X' * (X * theta - y);

normal equation: 

$$\theta = (X^{T}X)^{-1}X^{T}y$$

There is no need to do feature scaling or mean normalization for normal equation. 

If $X^{T}X$ is invertible, the common causes might be having:

The complexity of normal equation is $O(n^{3})$ compared to $O(kn^{2})$ of gradient descent. So when $n$ is too large, gradient descent is more efficient.

 

标签:frac,features,Notes,Andrew,Machine,bmatrix,theta,th,partial
来源: https://www.cnblogs.com/ms-qwq/p/16380094.html