Logistic Regression with a Neural Network mindset python example

Example 1: Logistic Regression with a Neural Network mindset python example

def load_dataset():
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes
    
    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
    
    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

Example 2: Logistic Regression with a Neural Network mindset python example

import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage

%matplotlib inline

Example 3: Logistic Regression with a Neural Network mindset python example

sigmoid([0, 2]) = [ 0.5         0.88079708]

Example 4: Logistic Regression with a Neural Network mindset python example

print ("sigmoid([0, 2]) = " + str(sigmoid(np.array([0,2]))))

Example 5: Logistic Regression with a Neural Network mindset python example

dim = 2
w, b = initialize_with_zeros(dim)
print ("w = " + str(w))
print ("b = " + str(b))

Example 6: Logistic Regression with a Neural Network mindset python example

- m_train (number of training examples)
- m_test (number of test examples)
- num_px (= height = width of a training image)

Example 7: Logistic Regression with a Neural Network mindset python example

- Initialize the parameters of the model
- Learn the parameters for the model by minimizing the cost  
- Use the learned parameters to make predictions (on the test set)
- Analyse the results and conclude

Example 8: Logistic Regression with a Neural Network mindset python example

train_set_x = train_set_x_flatten/255.
test_set_x = test_set_x_flatten/255.

Example 9: Logistic Regression with a Neural Network mindset python example

y = [1], it's a 'cat' picture.

Example 10: Logistic Regression with a Neural Network mindset python example

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]

Tags:

Misc Example