N Days_With_Machine_Learning (Part2 )

In This Blog , I will write About How to Learn and Master Machine Learning , It will takes from me N-1 Days , and it will be divided into 6 Parts (Data Preprocessing , Classification , Regression , Clustering , Artificial Neural networks , Reinforcement learning )

Image for post
Image for post

Day 2 : Classification

Image for post
Image for post

What is Classification ?

Classification is one of the most widely used techniques in machine learning, with a broad array of applications, including sentiment analysis, ad targeting, Spam detection, risk assessment, medical diagnosis and image classification.

Image for post
Image for post
Spam Classification
Image for post
Image for post
Ads targeting

The core goal of classification is to predict a category or class y from some inputs x.

Let’s start Building our Model

Our Problem is To Predict if the Customer will purchase our Product or Not ?

We Have the dataset of the customers with all the necessary data (Age , Estimated_Salary , Gender ,User_Id, and Purchased or not )

Our Mission is Train Machine learning Model with Logistic_Regression to predict the action of the customers (To Buy or not To Buy .. )

What is Logistic Regression ?

Image for post
Image for post

. A Logistic regression maps a continuous x to a “binary” y

. We can use Logistic regression to make “category” or “true/false” decisions from data.

Example of application :

Logistic Regression: Given a set of medical inputs, does a patient have cancer? (Yes/No)

Image for post
Image for post

Don’t’ Worry if you don’t like Maths , you are not supposed to know the Mathematics equations

Image for post
Image for post

All What we have to do it to know how to implement the Logistic_Regression Classifier with scikit-learn

Let’ start Coding

Image for post
Image for post

Import the necessary Libraries

# Imporitng the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Load the Dataset (Social_Network_Ads.csv)

Image for post
Image for post
dataset = pd.read_csv('Social_Network_Ads.csv');
X = dataset.iloc[:,[2,3]].values
Y = dataset.iloc[:,4].values

We need to Provide 2 Features for our Model : Age && Estimated_Salary

Image for post
Image for post

# Splitting the Data into Training Set and Testing Set
from sklearn.model_selection import train_test_split
X_Train ,X_Test , Y_Train,Y_Test= train_test_split(X,Y, test_size=0.25,random_state=0)

Now let’s Scale our Data

# Scaling the data *************from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_Train)
X_Test =scaler.transform(X_Test)

Now let’s Prepare our Classifier :sklearn.linear_model.LogisticRegression

and Train it with Our Data (X_Train Y_Train )

from sklearn import linear_model
#Prepare our Classifier*********
classifier = linear_model.LogisticRegression(C=1e5)
#Train our classifier
classifier.fit(X_Train,Y_Train);

Now let’s Make Our Prediction with Classifier.predict

y_predicted = classifier.predict(X_Test)
Image for post
Image for post

I tried in This Blog to Explain for You How To make a Machine Learning Model , Train it with Data , and make your Prediction

I have used Logistic_Regression as a Classifier , there is other Classifier we can use them for the classification Task like (SVM, Naive-Bayes , Decision_Tree , K-Nearest-Neighbor and Random Forest )

Making mistakes is a lot better than not doing anything

Image for post
Image for post

You Can fin the Full code source Here :

Follow me in Twitter for code Updates

Thanks For Your Feed Back

Written by

<script>alert('try your best')</script>

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store