Monday, September 30, 2019

Simple Linear regression


Let’s take a look at this dataset. It’s related to the Co2 emission of different cars. It includes Engine size, Cylinders, Fuel Consumption and Co2 emissions for various car models. The question is: Given this dataset, can we predict the Co2 emission of a car, using another field, such as Engine size? Quite simply, yes! We can use linear regression to predict a continuous value such as Co2 Emission, by using other variables. Linear regression is the approximation of a linear model used to describe the relationship between two or more variables. In simple linear regression, there are two variables: a dependent variable and an independent variable. The key point in the linear regression is that our dependent value should be continuous and cannot be a discreet value. However, the independent variable(s) can be measured on either a categorical or continuous measurement scale.
There are two types of linear regression models. They are simple regression and multiple regression. Simple linear regression is when one independent variable is used to estimate a dependent variable. For example, predicting Co2 emission using the EngineSize variable. When more than one independent variable is present, the process is called multiple linear regression.


To understand linear regression, we can plot our variables here. We show Engine size as an independent variable and Emission as the target value that we would like to predict. A scatterplot clearly shows the relationship between variables where changes in one variable "explain" or possibly "cause" changes in the other variable. Also, it indicates that these variables are linearly related.
With linear regression, you can fit a line through the data. For instance, as the EngineSize increases, so do the emissions. With linear regression, you can model the relationship between these variables.
A good model can be used to predict what the approximate emission of each car is.

We’re going to predict the target value, y. In our case, using the independent variable, "Engine Size," represented by x1. The fit line is shown traditionally as a polynomial. In a simple regression problem (a single x), the form of the model would be θ0 +θ1 x1. In this equation, y ̂ is the dependent variable
or the predicted value, and x1 is the independent variable; θ0 and θ1 are the parameters of the line that we must adjust. θ1 is known as the "slope" or "gradient" of the fitting line and θ0 is known as the "intercept." θ0 and θ1 are also called the coefficients of the linear equation. You can interpret this equation as y ̂ being a function of x1, or y ̂ being dependent of x1. Now the questions are: "How would you draw a line through the points?" And, "How do you determine which line ‘fits best’?"
Linear regression estimates the coefficients of the line. This means we must calculate θ0 and θ1 to find the best line to ‘fit’ the data. This line would best estimate the emission of the unknown data points. Let’s see how we can find this line, or to be more precise, how we can adjust the
parameters to make the line the best fit for the data.
For a moment, let’s assume we’ve already found the best fit line for our data. Now, let’s go through all the points and check how well they align with this line. Best fit, here, means that if we have, for instance, a car with engine size x1=5.4, and actual Co2=250, its Co2 should be predicted very close to the actual value, which is y=250, based on historical data. But, if we use the fit line, or better to say, using our polynomial with known parameters to predict the Co2 emission, it will return y ̂ =340.
Now, if you compare the actual value of the emission of the car with what we predicted using our model, you will find out that we have a 90-unit error. This means our prediction line is not accurate. This error is also called the residual error. So, we can say the error is the distance from the data point to the fitted regression line. The mean of all residual errors shows how poorly the line fits with the whole dataset. Mathematically, it can be shown by the equation, mean squared error, shown as (MSE). Our objective is to find a line where the mean of all these errors is minimized. In other words, the mean error of the prediction using the fit line should be minimized. Let’s re-word it more technically. The objective of linear regression is to minimize this MSE equation and to minimize it, we should find the best parameters, θ0 and θ1. Now, the question is, how to find θ0 and θ1 in such a way that it minimizes this error? How can we find such a perfect line? Or, said another way, how should we find the best parameters for our line? Should we move the line a lot randomly and calculate the MSE value every time, and choose the minimum one?
Not really! Actually, we have two options here: Option 1 - We can use a mathematic approach. Or, Option 2 - We can use an optimization approach.

Let’s see how we can easily use a mathematic formula to find the θ0 and θ1. As mentioned before, θ0 and θ1, in the simple linear regression, are the coefficients of the fit line. We can use a simple equation to estimate these coefficients. That is, given that it’s a simple linear regression, with only 2 parameters, and knowing that θ0 and θ1 are the intercept and slope of the line, we can estimate them directly from our data. It requires that we calculate the mean of the independent and dependent or target columns, from the dataset. Notice that all of the data must be available to traverse and calculate the parameters. It can be shown that the intercept and slope can be calculated using these equations. We can start off by estimating the value for θ1. This is how you can find the slope of a line based on the data. x ̅  is the average value for the engine size in our dataset. Please consider that we have 9 rows here, row 0 to 8. First, we calculate the average of x1 and average of y. Then we plug it into the slope equation, to find θ1. The xi and yi in the equation refer to the fact that we need to repeat these calculations across all values in our dataset and i refers to the i’th value of x or y. Applying all values, we find θ1=39; it is our second parameter. It is used to calculate the first parameter, which is the intercept of the line. Now, we can plug θ1 into the line equation to find θ0. It is easily calculated that θ0=125.74. So, these are the two parameters for the line, where θ0 is also called the bias coefficient and θ1 is the coefficient for the Co2 Emission column. As a side note, you really don’t need to remember the formula for calculating these parameters, as most of the libraries used for machine learning in Python, R, and Scala can easily find these parameters for you. But it’s always good to understand how it works.
Now, we can write down the polynomial of the line. So, we know how to find the best fit for our data, and its equation. Now the question is: "How can we use it to predict the emission of a new car based on its engine size?"
After we found the parameters of the linear equation, making predictions is as simple as solving the equation for a specific set of inputs. Imagine we are predicting Co2 Emission(y) from EngineSize(x) for the Automobile in record number 9. Our linear regression model representation for this problem would be: y ̂ = θ0 + θ1 x1. Or if we map it to our dataset, it would be Co2Emission = θ0 + θ1  EngineSize. As we saw, we can find θ0, θ1 using the equations that we just talked about.
Once found, we can plug in the equation of the linear model.
For example, let’s use θ0=125 and θ1=39. So, we can rewrite the linear model as 𝐶𝑜2𝐸𝑚𝑖𝑠𝑠𝑖𝑜𝑛=125+39𝐸𝑛𝑔𝑖𝑛𝑒𝑆𝑖𝑧𝑒. Now, let’s plug in the 9th row of our dataset and calculate the Co2 Emission for a car with an EngineSize of 2.4. So Co2Emission = 125 + 39 × 2.4.
Therefore, we can predict that the Co2 Emission for this specific car would be 218.6.


Wednesday, September 25, 2019

Regression Models

The regression model is a powerful method that allows you to examine the relationship between two or more variables of interest. In other word, Regression models (both linear and non-linear) are used for predicting a real value, like salary for example. If your independent variable is time, then you are forecasting future values, otherwise, your model is predicting present but unknown values.

Regression analysis is a form of predictive modelling technique which investigates the relationship between a dependent (target) and independent variable (s) (predictor). This technique is used for forecasting, time series modelling and finding the causal effect relationship between the variables

Regression technique varies from Linear Regression to SVR and Random Forests Regression.

Look at this dataset. It's related to Co2 emissions from different cars. It includes Engine size, number of Cylinders, Fuel Consumption and Co2 emission from various automobile models. The question is, "Given this dataset, can we predict the Co2 emission of a car using other fields, such as EngineSize or Cylinders?" Let’s assume we have some historical data from different cars and assume that a car, such as in row 9, has not been manufactured yet, but we're interested in estimating its approximate Co2 emission, after production. Is it possible?
We can use regression methods to predict a continuous value, such as CO2 Emission, using some other variables. Indeed, Regression is the process of predicting a continuous value.
In regression, there are two types of variables: a dependent variable and one or more independent variables.
The dependent variable can be seen as the "state", "target" or "final goal" we study
and try to predict, and the independent variables, also known as explanatory variables, can be seen as the "causes" of those "states". The independent variables are shown conventionally by x, and the dependent variable is notated by y.

A regression model relates y, or the dependent variable, to a function of x, i.e., the independent variables. The key point in the regression is that our dependent value should be continuous, and cannot be a discreet value. However, the independent variable or variables can be measured on either a categorical or continuous measurement scale. So, what we want to do here is to use the historical data of some cars, using one or more of their features, and from that data, make a model.
We use regression to build such a regression/estimation model. Then the model is used to predict the expected Co2 emission for a new or unknown car. Basically, there are 2 types of regression models: simple regression and multiple regression.
Simple regression is when one independent variable is used to estimate a dependent variable. It can be either linear on non-linear. For example, predicting Co2emission using the variable of EngineSize. The linearity of regression is based on the nature of the relationship between independent and dependent variables.
When more than one independent variable is present, the process is called multiple linear regression. For example, predicting Co2emission using EngineSize and the number of Cylinders in any given car. Again, depending on the relation between dependent and independent variables, it can be either linear or non-linear regression.

Following Machine Learning Regression models:
  • Simple Linear Regression
  • Multiple Linear Regression
  • Polynomial Regression
  • Support Vector for Regression (SVR)
  • Decision Tree Regression
  • Random Forest Regression

Wednesday, September 18, 2019

Understanding Deep Learning with TensorFlow playground


The TensorFlow playground can be used to illustrate that deep learning uses multiple layers of abstraction.

First, notice blue represents +1, orange represents -1, and white represents 0.

Let’s start with the default classification example. There are 4 datasets.
The four datasets: circular, 4 quadrants, 2 clusters, and a swirl

The datasets all have 2 input features and 1 output label. The 2 input features, X1 and X2, are represented by the coordinates. X1 is the horizontal axis and X2 is the vertical axis. You can infer that from the feature inputs below.
Graph of input features: X1 and X2



The output label is the color of the dots, blue (+1) or orange (-1).
Features: X1, and X2 the horizontal and vertical axes. The label: blue(+1) or orange(-1) dots.

The first 3 datasets can be solved with the default setting with 2 hidden layers. However the 4th, the swirl dataset, can not. When you click the play button it is actually training a neural network that runs in your browser. The background color of the output changes from light shades (representing 0) to blue and orange patterns that illustrate what the network will predict for new input.

What is TensorFlow? Introduction, Architecture

What is TensorFlow?

Currently, the most famous deep learning library in the world is Google's TensorFlow. Google product uses machine learning in all of its products to improve the search engine, translation, image captioning or recommendations.

To give a concrete example, Google users can experience a faster and more refined the search with AI. If the user types a keyword in the search bar, Google provides a recommendation about what could be the next word.

Google wants to use machine learning to take advantage of their massive datasets to give users the best experience. Three different groups use machine learning:

  • Researchers
  • Data scientists
  • Programmers

They can all use the same toolset to collaborate with each other and improve their efficiency.

Google does not just have any data; they have the world's most massive computer, so Tensor Flow was built to scale. TensorFlow is a library developed by the Google Brain Team to accelerate machine learning and deep neural network research.

It was built to run on multiple CPUs or GPUs and even mobile operating systems, and it has several wrappers in several languages like Python, C++ or Java.



History of TensorFlow

A couple of years ago, deep learning started to outperform all other machine learning algorithms when giving a massive amount of data. Google saw it could use these deep neural networks to improve its services:
  • Gmail
  • Photo
  • Google search engine

They build a framework called Tensorflow to let researchers and developers work together on an AI model. Once developed and scaled, it allows lots of people to use it.

It was first made public in late 2015, while the first stable version appeared in 2017. It is open source under Apache Open Source license. You can use it, modify it and redistribute the modified version for a fee without paying anything to Google.

Tuesday, September 17, 2019

Activation functions and what are it uses in a Neural Network Model

Activation functions are really important for an Artificial Neural Network to learn and make sense of something really complicated and Non-linear complex functional mappings between the inputs and response variable. They introduce non-linear properties to our Network. Their main purpose is to convert an input signal of a node in an ANN to an output signal. That output signal now is used as an input in the next layer in the stack.
we need to apply an activation function f(x) so as to make the network more powerful and add the ability to it to learn something complex and complicated form data and represent non-linear complex arbitrary functional mappings between inputs and outputs. Hence using a non-linear Activation we are able to generate non-linear mappings from inputs to outputs.
Also, another important feature of an Activation function is that it should be differentiable. We need it to be this way so as to perform backpropagation optimization strategy while propagating backwards in the network to compute gradients of Error(loss) with respect to Weights and then accordingly optimize weights using Gradient descend or any other Optimization technique to reduce Error.

Most popular types of Activation functions:

Sigmoid or Logistic - Predict value in between 0 to 1. Returns f(x) = 1 / (1 + exp(-x))
TanH (Hyperbolic Tangent) - Predict value in between -1 to 1. Returns f(x) = tanh(x)

Introduction of the perception/neuron

Before we jump straight into neural networks we need to understand the individual components first such as a single neuron.
Artificial neural networks (ANN) actually have a basis in biology so we're going to see how we can attempt to mimic biological neurons of an artificial neuron (otherwise known as the perception) and then once we go through the process of how a simple perception works will go ahead and show you how you can represent that mathematically

Below let's see the biological neuron such as a brain cell

so the biological neuron works as in a simplified way through the following manner, basically you have dendrites that feed into the body of cell you can have many dendrites and what happens is electrical signal gets to pass through the dendrites to the body of the cell and then later on a single output or a single electrical signal is passed on down through an axon to, later on, connects to some other neuron and that's the basic idea we have a kind of the as many inputs of electoral signals to the dendrites goes to the body and in a single actual signal output through the axon so the artificial neuron also has inputs outputs so I go in attempt to mimic the biological neurons.


Machine Learning

What is machine learning?
Machine learning algorithms are algorithms that learn (often predictive) models from data. I.e., instead of formulating “rules” manually, a machine learning algorithm will learn the model for you.
in another word, Machine learning is a technique to achieve AI through algorithms trained with data.

There are three ways in which machines learn:

Supervised Learning:
Supervised learning is a method in which the machine learns using labelled data. It is like learning under the guidance of a teacher. The training dataset is like a teacher which is used to train the machine. Model is trained on a pre-defined dataset before it starts making decisions when given new data

Deep Learning

Deep learning is a subset of machine learning which in turn is a subset of artificial intelligence. Artificial intelligence is a technique that enables a machine to mimic human behaviour. Machine learning is a technique to achieve AI through algorithms trained with data and finally, deep learning is a type of machine learning inspired by the structure of the human brain in terms of deep learning this structure is called an artificial neural network let's understand deep learning better and how it's different from machine learning.


Monday, September 16, 2019

Machine Learning Algorithms

Deep Learning
Deep learning is a subset of machine learning which in turn is a subset of artificial intelligence. Artificial intelligence is a technique that enables a machine to mimic human behaviour. Machine learning is a technique to achieve AI through algorithms trained with data and finally, deep learning is a type of machine learning inspired by the structure of the human brain in terms of deep learning this structure is called an artificial neural network let's understand deep learning better and how it's different from machine learning. We create a machine that could differentiate between tomatoes and cherries if done using machine learning we'd have to tell the Machine the features based on which the two can be differentiated these features could be the size and the type of stem on them with deep learning, on the other hand, the features are picked out by the neural network without human intervention, of course, that kind of independence comes at the cost of having a much higher volume of data to train our machine.