Classification using Logistic Regression

The difference between Linear and Logistic Regression, Image from https://dev.to/adityaberi8/logistic-regression-eg1

Binary Classification

This was the hypothesis function for Linear Regression where theta was a vector of parameters
This is the hypothesis function where the bottom function is called the sigmoid function or the logistic function

Cost/Loss Function

This is the i-th training example, this can be only one value or this can be a vector of values if you have multiple features
This is the answer or the output of i-th training example
This is the cost function for Logistic Regression, Image from https://stats.stackexchange.com/questions/278771/how-is-the-cost-function-from-logistic-regression-derivated
This is the gradient descent algorithm where alpha is the learning rate and we update all the parameters theta simultaneously in every iteration of Gradient Descent

Python Implementation

import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
data_frame = pd.read_csv('/Social_Network_Ads.csv')
data_frame.Gender = pd.Categorical(data_frame.Gender).codes
X = data_frame[['Gender', 'Age', 'EstimatedSalary']]
y = data_frame['Purchased']
X = X.to_numpy()
y = y.to_numpy()
scaler = MinMaxScaler()
scaler.fit(X)
X = scaler.transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
def pred(x_value):
x_value = scaler.transform([x_value])
print(model.predict(x_value))
print(model.score(X_train, y_train))

Multiclass Classification (One vs All Algorithm)

Conclusion

Thanks for Reading!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Fuzzy Logic and Artificial Intelligence

Extreme Learning Machines

Extracting Data from PDF Invoices

Object Localization without Deep Learning

DataGAN: Leveraging Synthetic Data for Self-Driving Vehicles

Machine learning and its scope — for core engineers, with a cup of coffee

Automating Breast Cancer Detection with Deep Learning

PyCaret 2.3.6 is Here! Learn What’s New?

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
Ahaan Pandya

Ahaan Pandya

More from Medium

Supervised and Unsupervised Machine Learning Algorithms

How to Transform Nominal Data for ML with OneHotEncoder from Scikit-Learn

Cross-validation and hyper-parameter tuning

Introduction to Random Forest algorithm through song popularity prediction