Friday, December 1, 2017

Predict Prices By Using Linear Regression Algorithm In Machine Learning


Simple linear regression is a statistical method that allows us to summarize and study relationships between two continuous variables where the output of variable is directly proportional to input. The variable which we will be predicting is called criterion variable which is along the Y axis and the variable which we are basing our predictions is known as predictor which is along the X axis. When there is single predictor variable that method is called simple linear regression method and when we have more than 1 predictor that model is called multiple linear regression method.

Statistical relation has lot of examples like height and weight, speed and petrol consumption, router bandwidth and router cpu relation, pizza size and price relation etc.

In the previous post, we have used the KNN neighbor algorithm to predict the values. In this post, we will be using the simple linear regression method to predict the pizza prices.

Before moving to the machine learning code, we need to first understand what equation of line is. The equation of line represents the relationship between the X and Y axis. Formula of finding equation of line is y = mx + c

C is Y intercept where the line meets the Y axis.

Slope of the line is nothing but the difference between the (Y2 – Y1)/(X2-X1)

With the above equation, if we get the values of m and c which remains constant and for every value of x we can get the value of y. Now let’s take example of pizza size and its price. First plot the variables with the existing dataset and after that we will use the simple linear regression to predict the pizza prices by giving pizza size.

Below is the graph of pizza size vs pizza price. This example is the right fit for simple linear regression method because as the size increase the price is also increasing. So we can say that there is direct relationship between pizza size and price.

Now we have to find the values of m and c, so that we can find predict any new price of any size of pizza. To get this done, we will be using the Scikit Learn library and import the linear regression model and find the slope and y intercept value.

Use the below python code in Jupyter Notebook and predict the price value of pizza sizes from 100 to 110.

import matplotlib.pyplot as plt
from sklearn import linear_model
pizza_size = [[4.0],[4.5],[5.0],[5.6],[5.8],[6.0],[7.2],[7.4],[7.8],[9]]
pizza_price = [42,45,50,55,58,62,65,70,72,75]
print("Pizza Size and Pizza Price")
for row in zip(pizza_size,pizza_price):
print(row[0],'->',row[1])

#Instantiating Linear Regression Model
reg = linear_model.LinearRegression()
reg.fit(pizza_size,pizza_price)

#storing the slope
m = reg.coef_
#storing y intercept
b = reg.intercept_

print("Slope Of the Line Is:", m)
print("Y intercept is:", b)

# This is used to plot the existing relationship on graph
plt.scatter(pizza_size,pizza_price,color='red')
#Equation of straight Line is y = m*x + b
#Now we know the m and b values, so we can predict the straight line points
predicted_values = [m * x + b for x in pizza_size]

#Plot the straight line
plt.plot(pizza_size,predicted_values,'r--')
plt.xlabel("pizza_size")
plt.ylabel("pizza_price")
plt.show()

#Predict the Pizza Size Prices from 100 to 110
for i in range(100,110):
print("The price of pizza will be:",reg.predict(i))


Click Here To Read Rest Of The Post...

Thursday, November 30, 2017

Predicting Values By Using k-nearest Neighbors Machine Learning Algorithm


The k-nearest neighbors algorithm (k-NN) is a non-parametric method used for classification and regression in machine learning. This is the simplest and easiest algorithm which can be used to predict the outcome values. K-NN is also known as lazy learning, which stores the training data and waits until given a test tuple.

K-Nearest Neighbor Algorithm Has Below Mentioned Steps:
1. Pick a value of K
2. Search for K observations in the training data that are nearest to the measurements of the unknown data
3. Use the most popular response value from the K nearest neighbors as presicted response value for the unknown data or iris data in our example.
In the previous post we have loaded our first dataset which is IRIS. Now use the same dataset to train our model by using KNN neighbor algorithm and predict the values or outcomes. Click here to check which algorithm is used when.

Follow the code as explained in the previous post to load the IRIS dataset by using Scikit Learn.

# Below code will load the 4 attributes of IRIS in x_train_data object. The same can be verifies by using shape command
x_train_data = iris.data
y_train_data_outcome = iris.target

#Import KNN classifier from sklearn
from sklearn.neighbors import KNeighborsClassifier

#Define the neighbors value, I have used 1 you can try with 2, 3 4 or 5 and check the results.
knn = KNeighborsClassifier(n_neighbors=1)
print(knn)

#Training the KNN Classifier by loading test samples which is nothing but the 4 attributes of iris.data and 1 outcome of iris target

knn.fit(x_train_data, y_train_data_outcome)

#Define predict_species with 4 attributes
predict_species = ([3,5,4,2])

# Using Knn predict method to predict the species class.
print(knn.predict([predict_species]))

You can change the n_neighbors value and see what happens.

Click Here To Read Rest Of The Post...

Wednesday, November 29, 2017

Loading First Data Set In Machine Learning


In my previous post, I have already explained the difference between the Deep Learning and Machine Learning. In this post, I going to talk more about how to load the data set in Scikit learn which is machine learning library for new comers.

This post is more focus on loading IRIS dataset from Scikit Learn library by using Jupyter notebook. In 1936 Edgar Anderson collected 50 samples of 3 different species of IRIS for each sample he measured the sepal length, sepal width, petal length and petal width and record the measurements along with its species.

Use the below code to load the CSV file in your Jupyter notebook directly from internet.
from IPython.display import HTML
Use this link to load it in your Jupyter Notebook http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data

Output: There are 150 rows but I have only pasted sample from each row.
5.1,3.5,1.4,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
5.9,3.0,5.1,1.8,Iris-virginica

The above output is comma separated output starting with sepal length, sepal width, petal length and petal width. The dataset also covers the species setosa, versicolor and virginica. With this dataset we can predict the species of the flower. This problem is also known as supervised learning because we are trying to learn the relationship between the data namely the IRIS measurements and the outcome is the species of IRIS.

I am using Scikit learn library for writing machine learning code. You can also install it from here. The best way to learn the machine learning or deep learning is to use the anaconda package on your laptop. This has lot of inbuilt libraries which can help you to sharpen your skills with GUI. GUI is nothing but it’s Jupyter iPYTHON version.

The IRIS dataset is the most famous dataset in machine learning and it is already built in Scikit-Learn. So we don’t need to load it from anywhere else as it is already built in function in Scikit-Learn. Let’s load the dataset.

#import Load_iris function from SKlearn datasets
from sklearn.datasets import load_iris

#Save iris dataset and it’s attributes in object called iris
Iris = load_iris()
type(iris)

#Iris is a object which has special container called bunch which is scikit learn special object type for storing datasets and it’s attributes.

#to check what’s in the dataset we can run the below code
print (iris.data)

#to check all about the dataset we can run the below code
Print(iris.DESCR)

In Machine Learning, each row is known as observation and column is known as feature.

#The below command will let you know about the features of the IRIS dataset
print (iris.feature_names)
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

#The below command will let you know about the type of IRIS category
print (iris.target_names)
['setosa' 'versicolor' 'virginica']

Each value what we are predicting is the response or target or outcome. In machine learning features and responses are separate objects and it can be numeric too. All the features and responses must be in numpy format else it will not going to work. We can check the type of the dataset by using “type(iris.data)” command.

In this dataset, we have X axis which is nothing but the attributes and we have outcome which is nothing but the Y axis. So lets’ store the attributes in X and outcome in Y.

#Store the IRIS features in X
X = iris.data

#Store the IRIS outcome in Y
Y = iris.target

Stay tuned for the next post which will help to understand how to use this dataset to write your first machine learning algorithm and check it's accuracy score.

Click Here To Read Rest Of The Post...