Using a Logistic regression along with Neural Networks for Cat vs Non-Cat Image Classification

Logistic Regression

How do computers interpret Images

6x6 RGB abstract image representation (

General Architecture

Logistic Regression, using a Neural Network (
  1. Define the model structure (such as number of input features)
  2. Initialize the model’s parameters
  3. Loop:
  • Calculate current loss (forward propagation)
  • Calculate current gradient (backward propagation)
  • Update parameters (gradient descent)

Step 1: Creating a new Notebook

Step 2: Loading the dataset

Step 3: Analyzing the dataset

Number of training examples: m_train = 209
Number of testing examples: m_test = 50
Height/Width of each image: num_px = 64
Each image is of size: (64, 64, 3)
train_set_x shape: (209, 64, 64, 3)
train_set_y shape: (1, 209)
test_set_x shape: (50, 64, 64, 3)
test_set_y shape: (1, 50)

Step 3: Reshaping the dataset

train_set_x_flatten shape: (12288, 209)
train_set_y shape: (1, 209)
test_set_x_flatten shape: (12288, 50)
test_set_y shape: (1, 50)
sanity check after reshaping: [17 31 56 22 33]

Step 4: Sigmoid function

print (“sigmoid([0, 2]) = “ + str(sigmoid(np.array([0,2]))))
Output : sigmoid([0, 2])[ 0.5 0.88079708]

Step 4: Initiating parameters

Output: w= [[ 0.] [ 0.]] b=0

Step 5: Forward and Backward Propogation

Output : dw=[[ 0.99845601] [ 2.39507239]] ,db=0.00145557813678 ,cost=5.801545319394553

Step 6: Optimization

w = [[0.19033591]
b = 1.9253598300845747
dw = [[0.67752042]
db = 0.21919450454067652

Step 7 : Prediction

  1. Calculate Ŷ =A=σ(wTX+b)Y^=A=σ(wTX+b)
  2. Convert the entries of a into 0 (if activation <= 0.5) or 1 (if activation > 0.5), stores the predictions in a vector Y_prediction. If you wish, you can use an if/else statement in a for loop (though there is also a way to vectorize this).
predictions = [[1. 1. 0.]]

Step 8 : Merge all functions into a mode

train accuracy: 99.99876382512535 %
test accuracy: 72.01052229722973 %

Step 12: Testing your model with your images

Image from google search
Image from google search
Image from google search
Image from google search
Image from google search
Image from google search


If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

Engineer. Data Analyst. Machine Learning enthusiast

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

Recommended from Medium

Data is the New Oil, even in the Oil and Gas Industry

Feature Selection with sklearn and Pandas

90s-Kids Movies Recommender System

How to Write Python Command-Line Interfaces like a Pro

A GUI for USA Pollution Trends

Simple Linear Regression for Machine Learning made easy with Ordinary Least Square [OLS] Method

A Look at Sephora Foundation Reviews: Part I

The Wire and Vanity Metrics | Ten Rules for Actionable Data

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
Aditi Mukerjee

Aditi Mukerjee

Engineer. Data Analyst. Machine Learning enthusiast

More from Medium

The 3 Reasons To Build A Lead Funnel

Supply ChainWhat the Fungible!! InfinitVerse takes the events forward in Metaverse in Dec 2021!


Uzair Merchant and Finhaven Private Markets Consider Offering a New Kind of NFT with Uzair’s Music…

How Challenger Media Brands Can Leverage Web3 Strategies to Disrupt the Status Quo