# AI News, Machine Learning FAQ

- On Thursday, June 7, 2018
- By Read More

## Machine Learning FAQ

Index There are several different reasons why implementing algorithms from scratch can be useful: Let us narrow down the phrase “implementing from scratch” a bit further in context of the 6 points I mentioned above.

although we spend so much time to implement the algorithm, we probably want to use an established library if we want to perform some serious analysis in our research lab and/or company.

Established libraries are typically more trustworthy – they have been battle-tested by many people, people who may have already encountered certain edge cases and made sure that there are no weird surprises.

But improvements concerning computational efficiency does not necessarily need to be in terms of modifications of the algorithms, but we could use lower-level programming languages, for example, Scala instead of Python, or Fortran instead of Scala, … this can go all down to assembly or machine code, or designing a chip that is optimized for running such kind of analysis.

- On Friday, June 8, 2018
- By Read More

## Machine Learning FAQ

Index There are several different reasons why implementing algorithms from scratch can be useful: Let us narrow down the phrase “implementing from scratch” a bit further in context of the 6 points I mentioned above.

although we spend so much time to implement the algorithm, we probably want to use an established library if we want to perform some serious analysis in our research lab and/or company.

Established libraries are typically more trustworthy – they have been battle-tested by many people, people who may have already encountered certain edge cases and made sure that there are no weird surprises.

But improvements concerning computational efficiency does not necessarily need to be in terms of modifications of the algorithms, but we could use lower-level programming languages, for example, Scala instead of Python, or Fortran instead of Scala, … this can go all down to assembly or machine code, or designing a chip that is optimized for running such kind of analysis.

- On Friday, June 8, 2018
- By Read More

## Implementing a Neural Network from Scratch in Python – An Introduction

Get the code: To follow along, all the code is also available as an iPython notebook on Github.

You can think of the blue dots as male patients and the red dots as female patients, with the x- and y- axis being medical measurements.

Our goal is to train a Machine Learning classifier that predicts the correct class (male of female) given the x- and y- coordinates.

This means that linear classifiers, such as Logistic Regression, won’t be able to fit the data unless you hand-engineer non-linear features (such as polynomials) that work well for the given dataset.

(Because we only have 2 classes we could actually get away with only one output node predicting 0 or 1, but having 2 makes it easier to extend the network to more classes later on).

The input to the network will be x- and y- coordinates and its output will be two probabilities, one for class 0 (“female”) and one for class 1 (“male”).

Because we want our network to output probabilities the activation function for the output layer will be the softmax, which is simply a way to convert raw scores to probabilities.

Our network makes predictions using forward propagation, which is just a bunch of matrix multiplications and the application of the activation function(s) we defined above.

is the input of layer and is the output of layer after applying the activation function.

If we have training examples and classes then the loss for our prediction with respect to the true labels is given by:

The formula looks complicated, but all it really does is sum over our training examples and add to the loss if we predicted the incorrect class.

The further away the two probability distributions (the correct labels) and (our predictions) are, the greater our loss will be.

We can use gradient descent to find the minimum and I will implement the most vanilla version of gradient descent, also called batch gradient descent with a fixed learning rate.

As an input, gradient descent needs the gradients (vector of derivatives) of the loss function with respect to our parameters: , , , .

To calculate these gradients we use the famous backpropagation algorithm, which is a way to efficiently calculate the gradients starting from the output.

We start by defining some useful variables and parameters for gradient descent: First let’s implement the loss function we defined above.

If we were to evaluate our model on a separate test set (and you should!) the model with a smaller hidden layer size would likely perform better due to better generalization.

Here are some things you can try to become more familiar with the code: All of the code is available as an iPython notebook on Github. Please leave questions or feedback in the comments!

- On Friday, June 8, 2018
- By Read More

## How To Implement Logistic Regression With Stochastic Gradient Descent From Scratch With Python

Logistic regression is the go-to linear classification algorithm for two-class problems.

It is easy to implement, easy to understand and gets great results on a wide variety of problems, even when the expectations the method has of your data are violated.

This section will give a brief description of the logistic regression technique, stochastic gradient descent and the Pima Indians diabetes dataset we will use in this tutorial.

This can be simplified as: Where e is the base of the natural logarithms (Euler’s number), yhat is the predicted output, b0 is the bias or intercept term and b1 is the coefficient for the single input value (x1).

The yhat prediction is a real value between 0 and 1, that needs to be rounded to an integer value and mapped to a predicted class value.

In machine learning, we can use a technique that evaluates and updates the coefficients every iteration called stochastic gradient descent to minimize the error of a model on our training data.

The model makes a prediction for a training instance, the error is calculated and the model is updated in order to reduce the error for the next prediction.

Each iteration, the coefficients (b) in machine learning language are updated using the equation: Where b is the coefficient or weight being optimized, learning_rate is a learning rate that you must configure (e.g.

yhat) is the prediction error for the model on the training data attributed to the weight, yhat is the prediction made by the coefficients and x is the input value.

This will be needed both in the evaluation of candidate coefficient values in stochastic gradient descent and after the model is finalized and we wish to start making predictions on test data or new data.

The prediction equation we have modeled for this problem is: or, with the specific coefficient values we chose by hand as: Running this function we get predictions that are reasonably close to the expected output (y) values and when rounded make correct predictions of the class.

There is one coefficient to weight each input attribute, and these are updated in a consistent way, for example: The special coefficient at the beginning of the list, also called the intercept, is updated in a similar way, except without an input as it is not associated with a specific input value: Now we can put all of this together.

You can see, that in addition, we keep track of the sum of the squared error (a positive value) each epoch so that we can print out a nice message each outer loop.

We use a larger learning rate of 0.3 and train the model for 100 epochs, or 100 exposures of the coefficients to the entire training dataset.

k value of 5 was used for cross-validation, giving each fold 768/5 = 153.6 or just over 150 records to be evaluated upon each iteration.

- On Friday, June 8, 2018
- By Read More

## Simple Guide to Logistic Regression in R

Every machine learning algorithm works best under a given set of conditions.

It is used to predict a binary outcome (1 / 0, Yes / No, True / False) given a set of independent variables.

You can also think of logistic regression as a special case of linear regression when the outcome variable is categorical, where we are using log of odds as dependent variable.

In simple words, it predicts the probability of occurrence of an event by fitting data to a logit function.

In 1972, Nelder and Wedderburn proposed this model with an effort to provide a means of using linear regression to the problems which were not directly suited for application of linear regression.

Infact, they proposed a class of different models (linear regression, ANOVA, Poisson Regression etc) which included logistic regression as a special case.

The fundamental equation of generalized linear model is: Here, g() is the link function, E(y) is the expectation of target variable and α + βx1 + γx2 is the linear predictor ( α,β,γ to be predicted).

To start with logistic regression, I’ll first write the simple linear regression equation with dependent variable enclosed in a link function: Note: For ease of understanding, I’ve considered ‘Age’

In logistic regression, we are only concerned about the probability of outcome dependent variable ( success or failure).

p should meet following criteria: Now, we’ll simply satisfy these 2 conditions and get to the core of logistic regression.

This (d) is the Logit Function If p is the probability of success, 1-p will be the probability of failure which can be written as: where q is the probability of failure On dividing, (d) / (e), we get,

Source: (plug –

ROC Curve: Receiver Operating Characteristic(ROC) summarizes the model’s performance by evaluating the trade offs between true positive rate (sensitivity) and false positive rate(1- specificity).

The area under curve (AUC), referred to as index of accuracy(A) or concordance index, is a perfect performance metric for ROC curve.

It indicates goodness of fit as its value approaches one, and a poor fit of the data as its value approaches zero.

Without going deep into feature engineering, here’s the script of simple logistic regression model: This data require lots of cleaning and feature engineering.

- On Friday, June 8, 2018
- By Read More

## How to Implement the Backpropagation Algorithm From Scratch In Python

The backpropagation algorithm is the classical feed-forward artificial neural network.

The principle of the backpropagation approach is to model a given function by modifying internal weightings of input signals to produce an expected output signal.

The system is trained using a supervised learning method, where the error between the system’s output and a known expected output is presented to the system and used to modify its internal state.

The scale for each numeric input value vary, so some data normalization may be required for use with algorithms that weight inputs like the backpropagation algorithm.

Update, download the dataset in CSV format directly: This tutorial is broken down into 6 parts: These steps will provide the foundation that you need to implement the backpropagation algorithm from scratch and apply it to your own predictive modeling problems.

We will need to store additional properties for a neuron during training, therefore we will use a dictionary to represent each neuron and store properties by names such as ‘weights‘

You can see that for the hidden layer we create n_hidden neurons and each neuron in the hidden layer has n_inputs + 1 weights, one for each input column in a dataset and an additional one for the bias.

We can calculate an output from a neural network by propagating an input signal through each layer until the output layer outputs its values.

It is the technique we will need to generate predictions during training that will need to be corrected, and it is the method we will need after the network is trained to make predictions on new data.

Where weight is a network weight, input is an input, i is the index of a weight or an input and bias is a special weight that has no input to multiply with (or you can think of the input as always being 1.0).

We can transfer an activation function using the sigmoid function as follows: Where e is the base of the natural logarithms (Euler’s number).

You can also see that we collect the outputs for a layer in an array named new_inputs that becomes the array inputs and is used as inputs for the following layer.

We define our network inline with one hidden neuron that expects 2 input values and an output layer with two neurons.

These errors are then propagated backward through the network from the output layer to the hidden layer, assigning blame for the error and updating weights as they go.

The math for backpropagating error is rooted in calculus, but we will remain high level in this section and focus on what is calculated and how rather than why the calculations take this particular form.

We are using the sigmoid transfer function, the derivative of which can be calculated as follows: Below is a function named transfer_derivative() that implements this equation.

The first step is to calculate the error for each output neuron, this will give us our error signal (input) to propagate backwards through the network.

The error for a given neuron can be calculated as follows: Where expected is the expected output value for the neuron, output is the output value for the neuron and transfer_derivative() calculates the slope of the neuron’s output value, as shown above.

The back-propagated error signal is accumulated and then used to determine the error for the neuron in the hidden layer, as follows: Where error_j is the error signal from the jth neuron in the output layer, weight_k is the weight that connects the kth neuron to the current neuron and output is the output for the current neuron.

You can see that the error signal for neurons in the hidden layer is accumulated from neurons in the output layer where the hidden neuron number j is also the index of the neuron’s weight in the output layer neuron[‘weights’][j].

This involves multiple iterations of exposing a training dataset to the network and for each row of data forward propagating the inputs, backpropagating the error and updating the network weights.

This part is broken down into two sections: Once errors are calculated for each neuron in the network via the back propagation method above, they can be used to update weights.

Network weights are updated as follows: Where weight is a given weight, learning_rate is a parameter that you must specify, error is the error calculated by the backpropagation procedure for the neuron and input is the input value that caused the error.

This increases the likelihood of the network finding a good set of weights across all layers rather than the fastest set of weights that minimize error (called premature convergence).

Below is a function named update_weights() that updates the weights for a network given an input row of data, a learning rate and assume that a forward and backward propagation have already been performed.

Below is a function that implements the training of an already initialized neural network with a given training dataset, learning rate, fixed number of epochs and an expected number of output values.

We can put together an example that includes everything we’ve seen so far including network initialization and train a network on a small dataset.

We can put this together with our code above for forward propagating input and with our small contrived dataset to test making predictions with an already-trained network.

For this we will use the helper function load_csv() to load the file, str_column_to_float() to convert string numbers to floats and str_column_to_int() to convert the class column to integer values.

It is generally good practice to normalize input values to the range of the chosen transfer function, in this case, the sigmoid function that outputs values between 0 and 1.

new function named back_propagation() was developed to manage the application of the Backpropagation algorithm, first initializing a network, training it on the training dataset and then using the trained network to make predictions on a test dataset.

You can see that backpropagation and the chosen configuration achieved a mean classification accuracy of 95.238% which is dramatically better than the Zero Rule algorithm that did slightly better than 28.095% accuracy.

- On Monday, February 24, 2020

**Random Forests - The Math of Intelligence (Week 6)**

This is one of the most used machine learning models ever. Random Forests can be used for both regression and classification, and our use case will be to ...

**SkLearn Linear Regression (Housing Prices Example)**

We will be learning how we use sklearn library in python to apply machine learning algorithms in python. scikit learn has Linear Regression in linear model class ...

**Linear Regression Machine Learning Method Using Scikit-learn & Pandas in Python - Tutorial 30**

In this tutorial on Python for Data Science, You will learn about Multiple linear regression Model using Scikit learn and pandas in Python. You will learn about ...

**Linear and Polynomial Regression in Python**

This brief tutorial demonstrates how to use Numpy and SciPy functions in Python to regress linear or polynomial functions that minimize the least squares ...

**K-Means Clustering - The Math of Intelligence (Week 3)**

Let's detect the intruder trying to break into our security system using a very popular ML technique called K-Means Clustering! This is an example of learning ...

**Marc Garcia - CART: Not only Classification and Regression Trees**

PyData Amsterdam 2016 Description Decision trees are very simple methods compared to Support Vector Machines, or Deep Learning. But they have some ...

**Python Exercise on Decision Tree and Linear Regression**

This is the first Machine Learning with Python Exercise of the Introduction to Machine Learning MOOC on NPTEL. It teaches how to perform use linear models ...

**Dimensionality Reduction - The Math of Intelligence #5**

Most of the datasets you'll find will have more than 3 dimensions. How are you supposed to understand visualize n-dimensional data? Enter dimensionality ...

**Support Vector Machines - The Math of Intelligence (Week 1)**

Support Vector Machines are a very popular type of machine learning model used for classification when you have a small dataset. We'll go through when to use ...

**How to Predict Stock Prices Easily - Intro to Deep Learning #7**

We're going to predict the closing price of the S&P 500 using a special type of recurrent neural network called an LSTM network. I'll explain why we use ...