## Machine Learning

Reference Course: Machine Learning in Coursera

Machine learning is the field of study that gives computers the ability to learn without being explicitly programmed

### Supervised Learning

In supervised learning, we are given a data set and already know what our correct output shold look like, having the idea that there is a relationship between the input and output.

In supervised learning, for each data in the data set, there shoule be a corresponding correct answer.

Supervised learning problems are categorized into “regression” and “ classfication” problems.

- In a regression problem(predict real-valued output), we are trying to predict results within a continuous output, meaning that we are trying to map input variables to some conntinuous function.
- In a classification problem(discrete-valued output), we are instead trying to predict results in a discrete output. In other words, we are trying to map input variables into discrete categories.

### Unsupervise Learning

Unsupervised learning allows us to approach problems with little or no idea what our results should look like. We can derive structure from data where we don’t necessarily know the effect of the variables.

We can derive this structure by clustering the data based on relationships among the variables in the data.

With unsupervised learning there is no feedback based on the prediction results.

In unsupervised learning, there is no concept of attributes or labels.

Example:

- Clustering: Take a collection of 1,000,000 different genes, and find a way to automatically group these genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on.
- Non-clustering: The “Cocktail Party Algorithm”

### Regression and Classification

- The difference between
`Regression`

problem and`Classification`

problem:

When the target variable that we’re trying to predict is continuous, we call the learning problem a regression problem. When target $y$ can take on only a small nnumber of discrete values, we call it a classification problem.

### Linear Regression with One Variable

Suppose we have a hypothesis function $h_{\theta} = \theta_0 + \theta_1 x$.

#### Cost Function

Then we can measure the accuracy of our hypothesis function by using a `cost function`

. This takes an average difference (actually a fancier version of an average) of all the results of hypothesis with inputs from $x’s$ and the actual outputs $y’s$.

`Cost function`

:

To break it apart, it is $\frac{1}{2} \bar{x}$ where $\bar{x}$ is the mean of the squares of $h_{\theta} (x_i) - y_i$, or the difference between the predicted value and the actual value.

This cost function is otherwise called the “Squared error function”, or “Mean squared error”.

The mean is halved $(\frac{1}{2})$ as a convenience for the computation of the gradient descent, as the derivative term of the square function will cancel out the $\frac{1}{2}$ .

The following image summarizes what the cost function does:

#### Cost Function Visualization

##### First Intuition 1: Scatter Plot

Our training data set is scattered on the `x-y`

plane. We are trying to make a straight line (define by $h_{\theta} = \theta_0 + \theta_1 x$) which passes through these scattered data points.

Our objective is to get the best possible line. The best possible will be such so that the average squared vertical distances of the scattered points from the line will be the least.

- $\theta_1 = 1$:

Ideally, the line should pass through all the points of our training data set, and in such case, the value of our cost function $J(\theta_0, \theta_1$) will be 0. The folloing image shows the ideal situation where we have a cost function of 0

- $\theta_1 = 0.5$:

We see the vertical distance from our fit to the data points increase. This increase our cost function to 0.58.

- Plotting several other points yields to the following graph:

In this case, $\theta_1 = 1$ is our global minimum.

##### First Intuition 2: contour Plot

A contour plot is a graph that contains many contour lines. A contour line of a two variable function has a constant value at all points of the same line.

The circled x displays the value of the cost function.

The three green points found on the green line below have the same value for $J(\theta_0, \theta_1$) and as a result, they are found along the same line.

The graph below minumizes the cost function as much as possible and consequently, the result of $\theta_1$ and $\theta_0$ tend to be around 0.12 and 250 respectively. Plotting those value on our graph to the right seems to put our point in the center of the inner most ‘circle’.

#### “Batch” Gradient Descent

“Batch”: Each step of gradient descent uses all the training examples. ()

Imagine that we graph our hypothesis function based on its fields $\theta_0$ and $\theta_1$ (actually we are graphing the cost function as a function of the parameter estimates). We are not graphing x and y itself, but the parameter range of our hypothesis function and the cost resulting from selecting a particular set of parameters.

We put $\theta_0$ on the x axis and $\theta_1$ on the y axis, with the cost function on the vertical z axis. The points on our graph will be the result of the cost function using our hypothesis with those specific theta parameters. The graph below depicts such a setup.

The red arrows show the minimum points in the graph.

The way we do this is by taking the derivative (the tangential line to a function) of our cost function. The slope of the tangent is the derivative at that point and it will give us a direction to move towards. We make steps down the cost function in the direction with the steepest descent. The size of each step is determined by the parameter $\alpha$, which is called the learning rate.

- Gradient descent algorithm:

Repeat until convergence:

where j = 0, 1 represents the feature index number, m represents the number of training example.

At each iteration j, one should simultaneously update the parameters $\theta_0, \theta_1, \theta_2, …, \theta_n$. Updating a specific parameter prior to calculating another one on the $j^{(th)}$ iteration would yield to a wrong implementation.

##### Learning rate $\alpha$

We should adjust our parameter $\alpha$ to ensure that the gradient descent algorithm converges in a reasonable time. But we should avoid to choose an inappropriate value:

Gradient descent can be converge to a local minimum, even with the learning rate $\alpha$ fixed. As we approach a local minimum, gradient descent will automatically take smaller steps. So, no need to decrease $\alpha$ over time.

#### Conclusion

So, this is simply gradient descent on the original cost function J. This method looks at every example in the entire training set on every step, and is called **batch gradient descent**.

Note that, while gradient descent can be susceptible to local minima in general, the optimization problem we have posed here for linear regression has only one global, and no other local, optima; thus gradient descent always converges (assuming the learning rate α is not too large) to the global minimum.

Indeed, J is a convex quadratic function. Here is an example of gradient descent as it is run to minimize a quadratic function.

The trajectory taken by gradient descent (contour of a quadratic function):

The x’s in the figure (joined by straight lines) mark the successive values of $\theta$ that gradient descent went through as it converged to its minimum.