Find file History
Latest commit d16d88e Jan 10, 2019
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md Add Fashion MNIST demo for logistic regression. Dec 25, 2018
__init__.py Fixing Pylint errors. Dec 23, 2018
logistic_regression.py Fix PyLint issues. Jan 10, 2019

README.md

Logistic Regression

Jupyter Demos

▶️ Demo | Logistic Regression With Linear Boundary - predict Iris flower class based on petal_length and petal_width

▶️ Demo | Logistic Regression With Non-Linear Boundary - predict microchip validity based on param_1 and param_2

▶️ Demo | Multivariate Logistic Regression | MNIST - recognize handwritten digits from 28x28 pixel images.

▶️ Demo | Multivariate Logistic Regression | Fashion MNIST - recognize clothes types from 28x28 pixel images.

Definition

Logistic regression is the appropriate regression analysis to conduct when the dependent variable is dichotomous (binary). Like all regression analyses, the logistic regression is a predictive analysis. Logistic regression is used to describe data and to explain the relationship between one dependent binary variable and one or more nominal, ordinal, interval or ratio-level independent variables.

Logistic Regression is used when the dependent variable (target) is categorical.

For example:

  • To predict whether an email is spam (1) or (0).
  • Whether online transaction is fraudulent (1) or not (0).
  • Whether the tumor is malignant (1) or not (0).

In other words the dependant variable (output) for logistic regression model may be described as:

Logistic Regression Output

Logistic Regression

Logistic Regression

Training Set

Training set is an input data where for every predefined set of features x we have a correct classification y.

Training Set

m - number of training set examples.

Training Set

For convenience of notation, define:

x-zero

Logistic Regression Output

Hypothesis (the Model)

The equation that gets features and parameters as an input and predicts the value as an output (i.e. predict if the email is spam or not based on some email characteristics).

Hypothesis

Where g() is a sigmoid function.

Sigmoid

Sigmoid

Now we my write down the hypothesis as follows:

Hypothesis

Predict 0

Predict 1

Cost Function

Function that shows how accurate the predictions of the hypothesis are with current set of parameters.

Cost Function

Cost Function

Cost Function

Cost function may be simplified to the following one-liner:

Cost Function

Batch Gradient Descent

Gradient descent is an iterative optimization algorithm for finding the minimum of a cost function described above. To find a local minimum of a function using gradient descent, one takes steps proportional to the negative of the gradient (or approximate gradient) of the function at the current point.

Picture below illustrates the steps we take going down of the hill to find local minimum.

Gradient Descent

The direction of the step is defined by derivative of the cost function in current point.

Gradient Descent

Once we decided what direction we need to go we need to decide what the size of the step we need to take.

Gradient Descent

We need to simultaneously update Theta for j = 0, 1, ..., n

Gradient Descent

Gradient Descent

alpha - the learning rate, the constant that defines the size of the gradient descent step

x-i-j - jth feature value of the ith training example

x-i - input (features) of ith training example

yi - output of ith training example

m - number of training examples

n - number of features

When we use term "batch" for gradient descent it means that each step of gradient descent uses all the training examples (as you might see from the formula above).

Multi-class Classification (One-vs-All)

Very often we need to do not just binary (0/1) classification but rather multi-class ones, like:

  • Weather: Sunny, Cloudy, Rain, Snow
  • Email tagging: Work, Friends, Family

To handle these type of issues we may train a logistic regression classifier Multi-class classifier several times for each class i to predict the probability that y = i.

One-vs-All

Regularization

Overfitting Problem

If we have too many features, the learned hypothesis may fit the training set very well:

overfitting

But it may fail to generalize to new examples (let's say predict prices on new example of detecting if new messages are spam).

overfitting

Solution to Overfitting

Here are couple of options that may be addressed:

  • Reduce the number of features
    • Manually select which features to keep
    • Model selection algorithm
  • Regularization
    • Keep all the features, but reduce magnitude/values of model parameters (thetas).
    • Works well when we have a lot of features, each of which contributes a bit to predicting y.

Regularization works by adding regularization parameter to the cost function:

Cost Function

regularization parameter - regularization parameter

Note that you should not regularize the parameter theta zero.

In this case the gradient descent formula will look like the following:

Gradient Descent

References