Introduction

Manually building my own neural network as a learning excercise

Imports

In [1]:
# Using nothing besides numpy!
import numpy as np

Forward Propagation Functions

In [2]:
def initialize_parameters(input_layer_dims):
    
    # Dictionary for the parameters (weights + biases)
    parameters = {}
    
    # Layer dimension length
    length = len(input_layer_dims)
    
    # Loop through length
    for i in range (1, length):
        
        # Initialize the weights matrix
        parameters['W' + str(i)] = np.random.randn(input_layer_dims[i], input_layer_dims[i-1]) * 0.01
        
        # Initialize biases
        parameters['b' + str(i)] = np.zeros((input_layer_dims[i], 1))
        
    return parameters
In [3]:
initialize_parameters([3, 5])
Out[3]:
{'W1': array([[-0.00447174, -0.00319264,  0.01753352],
        [ 0.00821962, -0.00239162,  0.0130544 ],
        [-0.00748103,  0.00886696,  0.00384391],
        [ 0.00256924, -0.00186721,  0.00475994],
        [ 0.01326595, -0.00955127,  0.0095157 ]]),
 'b1': array([[0.],
        [0.],
        [0.],
        [0.],
        [0.]])}
In [4]:
def sigmoid(Z):
    A = 1 / (1 + np.exp(-Z))
    return A, Z
In [5]:
sigmoid(2.7)
Out[5]:
(0.9370266439430035, 2.7)
In [6]:
def relu(Z):
    A = abs(Z * (Z > 0))
    return A, Z
In [7]:
relu(-2.7)
Out[7]:
(0.0, -2.7)
In [8]:
# Activation function
# A is the matrix with input data
# W is the weights matrix
# b is the bias
def linear_activation(A, W, b):
    Z = np.dot(W, A) + b
    cache = (A, W, b)
    return Z, cache
In [9]:
linear_activation([1, 2], [3, 4], [5])
# 1*3 + 2*4 + 5 = 16
Out[9]:
(array([16]), ([1, 2], [3, 4], [5]))
In [10]:
# Forward movement
def forward(A_prev, W, b, activation):
    
    # Linear activation
    Z, linear_cache = linear_activation(A_prev, W, b)
    
    # Pass through differentiable function
    if activation == 'sigmoid':
        A, activation_cache = sigmoid(Z)
    elif activation == 'relu':
        A, activation_cache = relu(Z)
        
    cache = (linear_cache, activation_cache) 
        
    return A, cache
In [11]:
forward([-2], [3], [6], 'sigmoid')
Out[11]:
(array([0.5]), (([-2], [3], [6]), array([0])))
In [12]:
# Forward propagation
def forward_propagation(X, parameters):
    
    # List previous values (cache)
    caches = []
    
    # Input data
    A = X
    
    # Parameter length
    L = len(parameters) // 2
    
    # Loop through hidden layers
    for i in range (1, L):
        
        # Store previous A value
        A_prev = A
        
        # Run the forward
        A, cache = forward(A_prev, parameters["W" + str(i)], parameters["b" + str(i)], activation = "relu")

        # Save cache
        caches.append(cache)
        
    # Last layer output
    A_last, cache = forward(A, parameters["W" + str(L)], parameters["b" + str(L)], activation = "sigmoid")

    # Save cache
    caches.append(cache)
    
    return (A_last, caches)
In [13]:
# Binary Cross-Entropy Cost
def cost_function(A_last, Y):
    
    # Adjust Y shape to get its length (total amount of elements)
    m = Y.shape[1]
    
    # Calculate cost comparing true value and prediction
    cost = (-1 / m) * np.sum((Y * np.log(A_last)) + ((1 - Y) * np.log(1 - A_last)))
    
    # Adjust cost shape
    cost = np.squeeze(cost)
    
    return (cost)
In [14]:
cost_function(np.array([[0.9], [0.9]]), np.array([[1], [1]]))
Out[14]:
0.21072103131565256

Backward Propagation

In [15]:
# Backward derivative of the sigmoid function
def sigmoid_backward(da, Z):
    
    # Calculate derivative of Z
    dg = (1 / (1 + np.exp(-Z))) * (1 - (1 / (1 + np.exp(-Z))))
    
    # Find the change in the derivative of Z
    dz = da * dg
    return dz
In [16]:
sigmoid_backward(0.5, 0.5)
Out[16]:
0.11750185610079725
In [17]:
# Backward derivative of the relu function
def relu_backward(da, Z):
    
    dg = 1 * ( Z >= 0)
    dz = da * dg
    return dz
In [18]:
relu_backward(0.5, 0.5)
Out[18]:
0.5
In [19]:
# Linear activation for back propagation
def linear_backward_function(dz, cache):
    
    # Get values from cache (memory)
    A_prev, W, b = cache
    
    # m shape
    m = A_prev.shape[1]
    
    # Calculate the derivative of W (result of the operation with dz)
    dW = (1 / m) * np.dot(dz, A_prev.T)
    
    # Calculate the derivative of b (result of the operation with dz)
    db = (1 / m) * np.sum(dz, axis = 1, keepdims = True)

    # Calculate the derivative of the operation
    dA_prev = np.dot(W.T, dz)
    
    return dA_prev, dW, db
In [49]:
def linear_activation_backward(dA, cache, activation):
    
    # Extract the cache
    linear_cache, activation_cache = cache
    
    # Check if activation is relu
    if activation == "relu":
        dZ = relu_backward(dA, activation_cache)
        dA_prev, dW, db = linear_backward_function(dZ, linear_cache)
        
    # Check if activation is sigmoid
    if activation == "sigmoid":
        dZ = sigmoid_backward(dA, activation_cache)
        dA_prev, dW, db = linear_backward_function(dZ, linear_cache)
        
    return dA_prev, dW, db   
In [50]:
# Backward propagation algorithm (calculate gradients to update weights)
# AL = predicted value during forward
# Y = true value
def backward_propagation(AL, Y, caches):
    
    # Gradients dictionary
    grads = {}
    
    # Length of data (on cache)
    L = len(caches)
    
    # Get the length for the m value
    m = AL.shape[1]
    
    # Adjust Y shape
    Y = Y.reshape(AL.shape)
    
    # Calculate the derivative of the network's final prediction (done at the end of Forward Propagation)
    dAL = -((Y / AL) - ((1 - Y) / (1 - AL)))
    
    # Get the current cache value
    current_cache = caches[L-1]
    
    # Generate a list of gradients for the data, weights and biases
    # This is done because this is the final part of the NN, starting the way back
    # AKA this is the prediction (sigmoid) layer
    grads["dA" + str(L - 1)], grads["dW" + str(L)], grads["db" + str(L)] = linear_activation_backward(dAL, current_cache, activation = "sigmoid")
    
    # Loop to calculate the derivative durante the linear activations with relu
    for l in reversed(range(L-1)):
        
        # Current cache
        current_cache = caches[l]
        
        # Calculate derivatives
        dA_prev, dW, db = linear_activation_backward(grads["dA" + str(l + 1)], current_cache, activation = "relu")
        
        # Feed the gradients dictionary, using their specific indexes
        grads["dA" + str(l)] = dA_prev
        grads["dW" + str(l + 1)] = dW
        grads["db" + str(l + 1)] = db
        
    return grads
In [22]:
# Update weights using the gradients
def update_parameters(parameters, grads, learning_rate):
    
    # Length of the dictionary with weights and biases
    L = len(parameters)//2
    
    # Loop to update weights
    for l in range(L):
        
        # Update weights
        parameters["W" + str(l + 1)] = parameters["W" + str(l + 1)] - (learning_rate * grads["dW" + str(l + 1)])

        # Update biases
        parameters["b" + str(l + 1)] = parameters["b" + str(l + 1)] - (learning_rate * grads["db" + str(l + 1)])
    
    return parameters

Implementing Complete Network

In [23]:
# Complete Neural Network Model
def NN_model(X, Y, input_layer_dims, learning_rate = 0.005, num_iterations = 20):
    
    # List for the cost of each training epoch
    costs = []
    
    # Inicialize the parameters
    parameters = initialize_parameters(input_layer_dims)
    
    # Loop for the number of epocs
    for i in range(num_iterations):
        
        # Forward Propagation
        AL, caches = forward_propagation(X, parameters)
        
        # Calculate cost
        cost = cost_function(AL, Y)
        
        # Backward Propagation
        gradients = backward_propagation(AL, Y, caches)
        
        # Update parameters
        parameters = update_parameters(parameters, gradients, learning_rate)
        
        # Print the epoch cost at every 10 epochs
        if i % 10 == 0:
            print('Cost after ' + str(i) + ' epochs is ' + str(cost))
            costs.append(cost)
            
    return parameters, costs
In [24]:
def predict(X, parameters):
    AL, caches = forward_propagation(X, parameters)
    return AL

Using a Preloaded Dataset to Test My NN

In [25]:
# Imports
import sklearn
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
In [26]:
# Versões dos pacotes usados neste jupyter notebook
%reload_ext watermark
%watermark -v --iv
sklearn    0.23.1
matplotlib 3.2.2
numpy      1.18.5
pandas     1.0.5
CPython 3.7.7
IPython 7.16.1
In [27]:
# Loading the complete dataset object on a temporary
temp = load_breast_cancer()
type(temp)
Out[27]:
sklearn.utils.Bunch
In [28]:
# Visualize the object
temp
Out[28]:
{'data': array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01,
         1.189e-01],
        [2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01,
         8.902e-02],
        [1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01,
         8.758e-02],
        ...,
        [1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01,
         7.820e-02],
        [2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01,
         1.240e-01],
        [7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01,
         7.039e-02]]),
 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
        0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
        1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
        1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
        1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
        0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,
        1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
        0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
        1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
        1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0,
        0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
        0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
        1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
        1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
        1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
        1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
        1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
        1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1]),
 'frame': None,
 'target_names': array(['malignant', 'benign'], dtype='<U9'),
 'DESCR': '.. _breast_cancer_dataset:\n\nBreast cancer wisconsin (diagnostic) dataset\n--------------------------------------------\n\n**Data Set Characteristics:**\n\n    :Number of Instances: 569\n\n    :Number of Attributes: 30 numeric, predictive attributes and the class\n\n    :Attribute Information:\n        - radius (mean of distances from center to points on the perimeter)\n        - texture (standard deviation of gray-scale values)\n        - perimeter\n        - area\n        - smoothness (local variation in radius lengths)\n        - compactness (perimeter^2 / area - 1.0)\n        - concavity (severity of concave portions of the contour)\n        - concave points (number of concave portions of the contour)\n        - symmetry\n        - fractal dimension ("coastline approximation" - 1)\n\n        The mean, standard error, and "worst" or largest (mean of the three\n        worst/largest values) of these features were computed for each image,\n        resulting in 30 features.  For instance, field 0 is Mean Radius, field\n        10 is Radius SE, field 20 is Worst Radius.\n\n        - class:\n                - WDBC-Malignant\n                - WDBC-Benign\n\n    :Summary Statistics:\n\n    ===================================== ====== ======\n                                           Min    Max\n    ===================================== ====== ======\n    radius (mean):                        6.981  28.11\n    texture (mean):                       9.71   39.28\n    perimeter (mean):                     43.79  188.5\n    area (mean):                          143.5  2501.0\n    smoothness (mean):                    0.053  0.163\n    compactness (mean):                   0.019  0.345\n    concavity (mean):                     0.0    0.427\n    concave points (mean):                0.0    0.201\n    symmetry (mean):                      0.106  0.304\n    fractal dimension (mean):             0.05   0.097\n    radius (standard error):              0.112  2.873\n    texture (standard error):             0.36   4.885\n    perimeter (standard error):           0.757  21.98\n    area (standard error):                6.802  542.2\n    smoothness (standard error):          0.002  0.031\n    compactness (standard error):         0.002  0.135\n    concavity (standard error):           0.0    0.396\n    concave points (standard error):      0.0    0.053\n    symmetry (standard error):            0.008  0.079\n    fractal dimension (standard error):   0.001  0.03\n    radius (worst):                       7.93   36.04\n    texture (worst):                      12.02  49.54\n    perimeter (worst):                    50.41  251.2\n    area (worst):                         185.2  4254.0\n    smoothness (worst):                   0.071  0.223\n    compactness (worst):                  0.027  1.058\n    concavity (worst):                    0.0    1.252\n    concave points (worst):               0.0    0.291\n    symmetry (worst):                     0.156  0.664\n    fractal dimension (worst):            0.055  0.208\n    ===================================== ====== ======\n\n    :Missing Attribute Values: None\n\n    :Class Distribution: 212 - Malignant, 357 - Benign\n\n    :Creator:  Dr. William H. Wolberg, W. Nick Street, Olvi L. Mangasarian\n\n    :Donor: Nick Street\n\n    :Date: November, 1995\n\nThis is a copy of UCI ML Breast Cancer Wisconsin (Diagnostic) datasets.\nhttps://goo.gl/U2Uwz2\n\nFeatures are computed from a digitized image of a fine needle\naspirate (FNA) of a breast mass.  They describe\ncharacteristics of the cell nuclei present in the image.\n\nSeparating plane described above was obtained using\nMultisurface Method-Tree (MSM-T) [K. P. Bennett, "Decision Tree\nConstruction Via Linear Programming." Proceedings of the 4th\nMidwest Artificial Intelligence and Cognitive Science Society,\npp. 97-101, 1992], a classification method which uses linear\nprogramming to construct a decision tree.  Relevant features\nwere selected using an exhaustive search in the space of 1-4\nfeatures and 1-3 separating planes.\n\nThe actual linear program used to obtain the separating plane\nin the 3-dimensional space is that described in:\n[K. P. Bennett and O. L. Mangasarian: "Robust Linear\nProgramming Discrimination of Two Linearly Inseparable Sets",\nOptimization Methods and Software 1, 1992, 23-34].\n\nThis database is also available through the UW CS ftp server:\n\nftp ftp.cs.wisc.edu\ncd math-prog/cpo-dataset/machine-learn/WDBC/\n\n.. topic:: References\n\n   - W.N. Street, W.H. Wolberg and O.L. Mangasarian. Nuclear feature extraction \n     for breast tumor diagnosis. IS&T/SPIE 1993 International Symposium on \n     Electronic Imaging: Science and Technology, volume 1905, pages 861-870,\n     San Jose, CA, 1993.\n   - O.L. Mangasarian, W.N. Street and W.H. Wolberg. Breast cancer diagnosis and \n     prognosis via linear programming. Operations Research, 43(4), pages 570-577, \n     July-August 1995.\n   - W.H. Wolberg, W.N. Street, and O.L. Mangasarian. Machine learning techniques\n     to diagnose breast cancer from fine-needle aspirates. Cancer Letters 77 (1994) \n     163-171.',
 'feature_names': array(['mean radius', 'mean texture', 'mean perimeter', 'mean area',
        'mean smoothness', 'mean compactness', 'mean concavity',
        'mean concave points', 'mean symmetry', 'mean fractal dimension',
        'radius error', 'texture error', 'perimeter error', 'area error',
        'smoothness error', 'compactness error', 'concavity error',
        'concave points error', 'symmetry error',
        'fractal dimension error', 'worst radius', 'worst texture',
        'worst perimeter', 'worst area', 'worst smoothness',
        'worst compactness', 'worst concavity', 'worst concave points',
        'worst symmetry', 'worst fractal dimension'], dtype='<U23'),
 'filename': 'C:\\Users\\Matheus\\Anaconda3\\lib\\site-packages\\sklearn\\datasets\\data\\breast_cancer.csv'}
In [29]:
# Load the dataset
dataset = pd.DataFrame(columns = load_breast_cancer()["feature_names"], data = load_breast_cancer()["data"])
In [30]:
# Shape
dataset.shape
Out[30]:
(569, 30)
In [31]:
dataset.head()
Out[31]:
mean radius mean texture mean perimeter mean area mean smoothness mean compactness mean concavity mean concave points mean symmetry mean fractal dimension ... worst radius worst texture worst perimeter worst area worst smoothness worst compactness worst concavity worst concave points worst symmetry worst fractal dimension
0 17.99 10.38 122.80 1001.0 0.11840 0.27760 0.3001 0.14710 0.2419 0.07871 ... 25.38 17.33 184.60 2019.0 0.1622 0.6656 0.7119 0.2654 0.4601 0.11890
1 20.57 17.77 132.90 1326.0 0.08474 0.07864 0.0869 0.07017 0.1812 0.05667 ... 24.99 23.41 158.80 1956.0 0.1238 0.1866 0.2416 0.1860 0.2750 0.08902
2 19.69 21.25 130.00 1203.0 0.10960 0.15990 0.1974 0.12790 0.2069 0.05999 ... 23.57 25.53 152.50 1709.0 0.1444 0.4245 0.4504 0.2430 0.3613 0.08758
3 11.42 20.38 77.58 386.1 0.14250 0.28390 0.2414 0.10520 0.2597 0.09744 ... 14.91 26.50 98.87 567.7 0.2098 0.8663 0.6869 0.2575 0.6638 0.17300
4 20.29 14.34 135.10 1297.0 0.10030 0.13280 0.1980 0.10430 0.1809 0.05883 ... 22.54 16.67 152.20 1575.0 0.1374 0.2050 0.4000 0.1625 0.2364 0.07678

5 rows Ă— 30 columns

In [32]:
# Check of missing values
dataset.isnull().any()
Out[32]:
mean radius                False
mean texture               False
mean perimeter             False
mean area                  False
mean smoothness            False
mean compactness           False
mean concavity             False
mean concave points        False
mean symmetry              False
mean fractal dimension     False
radius error               False
texture error              False
perimeter error            False
area error                 False
smoothness error           False
compactness error          False
concavity error            False
concave points error       False
symmetry error             False
fractal dimension error    False
worst radius               False
worst texture              False
worst perimeter            False
worst area                 False
worst smoothness           False
worst compactness          False
worst concavity            False
worst concave points       False
worst symmetry             False
worst fractal dimension    False
dtype: bool
In [33]:
# Split the target variavle
target = load_breast_cancer()["target"]
type(target)
Out[33]:
numpy.ndarray
In [34]:
target
Out[34]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
       1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
       1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
       1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0,
       0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,
       1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
       0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
       1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
       1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0,
       0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0,
       1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1,
       1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
       1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
       1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
       1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1,
       1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1])
In [35]:
# Total samples per class - benign cancer
np.count_nonzero(target == 1)
Out[35]:
357
In [36]:
# Total samples per class - malign cancer
np.count_nonzero(target == 0)
Out[36]:
212
In [37]:
# Extract labels

# Labels dictionary
labels = {}

# Target variable class names
target_names = load_breast_cancer()["target_names"]

# Map
for i in range(len(target_names)):
    labels.update({i:target_names[i]})
In [38]:
labels
Out[38]:
{0: 'malignant', 1: 'benign'}
In [39]:
# Prepare the predicting variables in X
X = np.array(dataset)
X
Out[39]:
array([[1.799e+01, 1.038e+01, 1.228e+02, ..., 2.654e-01, 4.601e-01,
        1.189e-01],
       [2.057e+01, 1.777e+01, 1.329e+02, ..., 1.860e-01, 2.750e-01,
        8.902e-02],
       [1.969e+01, 2.125e+01, 1.300e+02, ..., 2.430e-01, 3.613e-01,
        8.758e-02],
       ...,
       [1.660e+01, 2.808e+01, 1.083e+02, ..., 1.418e-01, 2.218e-01,
        7.820e-02],
       [2.060e+01, 2.933e+01, 1.401e+02, ..., 2.650e-01, 4.087e-01,
        1.240e-01],
       [7.760e+00, 2.454e+01, 4.792e+01, ..., 0.000e+00, 2.871e-01,
        7.039e-02]])
In [40]:
# Train test split
X_train, X_test, y_train, y_test = train_test_split(X, target, test_size = 0.15, shuffle = True)
In [41]:
# Shape of training data
print(X_train.shape)
print(y_train.shape)
(483, 30)
(483,)
In [42]:
# Shape of test data
print(X_test.shape)
print(y_test.shape)
(86, 30)
(86,)
In [43]:
# Adjust the shape of the input data
X_train = X_train.T
X_test = X_test.T
In [44]:
print(X_train.shape)
print(X_test.shape)
(30, 483)
(30, 86)
In [45]:
# Adjust the shape of the output data
y_train = y_train.reshape(1, len(y_train))
y_test = y_test.reshape(1, len(y_test))
In [46]:
print(y_train.shape)
print(y_test.shape)
(1, 483)
(1, 86)
In [47]:
# Variable with the input layer dimension for the number of neurons
input_layer_dims = [X_train.shape[0], 50, 20, 5, 1]
input_layer_dims
Out[47]:
[30, 50, 20, 5, 1]
In [54]:
# Train model

print("\nStarting training.\n")

parameters, cost = NN_model(X = X_train,
                            Y = y_train,
                            input_layer_dims = input_layer_dims,
                            num_iterations = 10000,
                            learning_rate = 0.005)

print("\nTraining finished.\n")
Starting training.

Cost after 0 epochs is 0.6931304580521144
Cost after 10 epochs is 0.6922742544292866
Cost after 20 epochs is 0.691439026049842
Cost after 30 epochs is 0.6906242458885682
Cost after 40 epochs is 0.6898293979887672
Cost after 50 epochs is 0.6890539786092041
Cost after 60 epochs is 0.6882974930298721
Cost after 70 epochs is 0.687559458837205
Cost after 80 epochs is 0.6868394012599506
Cost after 90 epochs is 0.6861368593518884
Cost after 100 epochs is 0.6854513813098023
Cost after 110 epochs is 0.6847825321596055
Cost after 120 epochs is 0.6841298956889486
Cost after 130 epochs is 0.6834930852029913
Cost after 140 epochs is 0.6828716935310413
Cost after 150 epochs is 0.6822653261581259
Cost after 160 epochs is 0.6816736214933604
Cost after 170 epochs is 0.68109618424477
Cost after 180 epochs is 0.6805326218936708
Cost after 190 epochs is 0.6799825677885046
Cost after 200 epochs is 0.6794456605467839
Cost after 210 epochs is 0.6789215491330786
Cost after 220 epochs is 0.6784098861957498
Cost after 230 epochs is 0.6779103313454091
Cost after 240 epochs is 0.6774225528440888
Cost after 250 epochs is 0.6769462235830742
Cost after 260 epochs is 0.6764810230797204
Cost after 270 epochs is 0.6760266325182183
Cost after 280 epochs is 0.6755827378623259
Cost after 290 epochs is 0.675149027355467
Cost after 300 epochs is 0.6747251913807468
Cost after 310 epochs is 0.6743109260305237
Cost after 320 epochs is 0.673905924053753
Cost after 330 epochs is 0.6735098815098284
Cost after 340 epochs is 0.6731224930003813
Cost after 350 epochs is 0.6727434420382505
Cost after 360 epochs is 0.6723724258887595
Cost after 370 epochs is 0.672009125855853
Cost after 380 epochs is 0.6716532241929207
Cost after 390 epochs is 0.6713044083458048
Cost after 400 epochs is 0.6709623554393569
Cost after 410 epochs is 0.670626766412347
Cost after 420 epochs is 0.670297271096923
Cost after 430 epochs is 0.6699734826677318
Cost after 440 epochs is 0.6696549993673382
Cost after 450 epochs is 0.6693413975549877
Cost after 460 epochs is 0.6690322237263674
Cost after 470 epochs is 0.6687269936479735
Cost after 480 epochs is 0.6684251757785566
Cost after 490 epochs is 0.6681261801449884
Cost after 500 epochs is 0.6678293515892141
Cost after 510 epochs is 0.6675339557908799
Cost after 520 epochs is 0.6672391398707107
Cost after 530 epochs is 0.6669439248349146
Cost after 540 epochs is 0.6666471733649788
Cost after 550 epochs is 0.6663475570728531
Cost after 560 epochs is 0.6660434844727904
Cost after 570 epochs is 0.6657330580703043
Cost after 580 epochs is 0.6654139696361776
Cost after 590 epochs is 0.6650834047605408
Cost after 600 epochs is 0.6647378891370537
Cost after 610 epochs is 0.6643730555867937
Cost after 620 epochs is 0.6639833840779784
Cost after 630 epochs is 0.6635617847265662
Cost after 640 epochs is 0.6630990485848521
Cost after 650 epochs is 0.6625830589970575
Cost after 660 epochs is 0.6619976460499649
Cost after 670 epochs is 0.6613209264871208
Cost after 680 epochs is 0.6605228559799723
Cost after 690 epochs is 0.6595615840913449
Cost after 700 epochs is 0.6583781225158378
Cost after 710 epochs is 0.6568886944530397
Cost after 720 epochs is 0.6549741470117286
Cost after 730 epochs is 0.6524683972706923
Cost after 740 epochs is 0.6491565933339426
Cost after 750 epochs is 0.6448062627197799
Cost after 760 epochs is 0.6392858143594561
Cost after 770 epochs is 0.6328593690555212
Cost after 780 epochs is 0.6264649038749297
Cost after 790 epochs is 0.621314838234475
Cost after 800 epochs is 0.6178081320358313
Cost after 810 epochs is 0.6153823980570488
Cost after 820 epochs is 0.6133955208196815
Cost after 830 epochs is 0.6115329068254801
Cost after 840 epochs is 0.6096925495530437
Cost after 850 epochs is 0.6078472042535569
Cost after 860 epochs is 0.605989114096321
Cost after 870 epochs is 0.6041110623014786
Cost after 880 epochs is 0.6021721870879478
Cost after 890 epochs is 0.6002548774645349
Cost after 900 epochs is 0.5983198109501557
Cost after 910 epochs is 0.5963591749811554
Cost after 920 epochs is 0.5943690688861746
Cost after 930 epochs is 0.5923452518414096
Cost after 940 epochs is 0.5902827792902228
Cost after 950 epochs is 0.5881758046515079
Cost after 960 epochs is 0.586017403045497
Cost after 970 epochs is 0.5837992587161464
Cost after 980 epochs is 0.5815114230154753
Cost after 990 epochs is 0.579141852937202
Cost after 1000 epochs is 0.5766758792947242
Cost after 1010 epochs is 0.5740954965693758
Cost after 1020 epochs is 0.5713783924891735
Cost after 1030 epochs is 0.5684967808205921
Cost after 1040 epochs is 0.5654158230060281
Cost after 1050 epochs is 0.5620916046161998
Cost after 1060 epochs is 0.5584686076818759
Cost after 1070 epochs is 0.5544786311039002
Cost after 1080 epochs is 0.5500347966606582
Cost after 1090 epochs is 0.5450329762100087
Cost after 1100 epochs is 0.5393301431906261
Cost after 1110 epochs is 0.5327568780464491
Cost after 1120 epochs is 0.5251166616710337
Cost after 1130 epochs is 0.516213173838642
Cost after 1140 epochs is 0.5059903647681573
Cost after 1150 epochs is 0.4945044759725293
Cost after 1160 epochs is 0.48185297277928474
Cost after 1170 epochs is 0.46842622996351385
Cost after 1180 epochs is 0.4554450596127147
Cost after 1190 epochs is 0.4439336924894468
Cost after 1200 epochs is 0.4338783038105641
Cost after 1210 epochs is 0.4251864330229679
Cost after 1220 epochs is 0.41764435580033904
Cost after 1230 epochs is 0.4109835297841426
Cost after 1240 epochs is 0.4072763709043427
Cost after 1250 epochs is 0.44939588619055953
Cost after 1260 epochs is 0.4361530019634508
Cost after 1270 epochs is 0.430991922584882
Cost after 1280 epochs is 0.42442416666910093
Cost after 1290 epochs is 0.41825367294384763
Cost after 1300 epochs is 0.4092612390025749
Cost after 1310 epochs is 0.40757240161372943
Cost after 1320 epochs is 0.4016249214142402
Cost after 1330 epochs is 0.3998057415771508
Cost after 1340 epochs is 0.3976694252739425
Cost after 1350 epochs is 0.3933970393475048
Cost after 1360 epochs is 0.3896583118830006
Cost after 1370 epochs is 0.3875209871731822
Cost after 1380 epochs is 0.38637673571198444
Cost after 1390 epochs is 0.3823389631364052
Cost after 1400 epochs is 0.3818590852019738
Cost after 1410 epochs is 0.379508929564215
Cost after 1420 epochs is 0.3771502770924509
Cost after 1430 epochs is 0.3760126682813436
Cost after 1440 epochs is 0.37434668140011257
Cost after 1450 epochs is 0.37026834480965437
Cost after 1460 epochs is 0.3684838243095031
Cost after 1470 epochs is 0.36612404218400546
Cost after 1480 epochs is 0.36715365625381363
Cost after 1490 epochs is 0.3650759278318123
Cost after 1500 epochs is 0.3645618372179085
Cost after 1510 epochs is 0.3612354181652788
Cost after 1520 epochs is 0.3643122617333823
Cost after 1530 epochs is 0.35636673685781367
Cost after 1540 epochs is 0.3585433798554397
Cost after 1550 epochs is 0.35251588230423664
Cost after 1560 epochs is 0.3565513698937398
Cost after 1570 epochs is 0.3509214464107927
Cost after 1580 epochs is 0.3501900645845686
Cost after 1590 epochs is 0.3486297849338277
Cost after 1600 epochs is 0.34944223088611565
Cost after 1610 epochs is 0.345540321988322
Cost after 1620 epochs is 0.3465845703577683
Cost after 1630 epochs is 0.34345983672092584
Cost after 1640 epochs is 0.34345543182287336
Cost after 1650 epochs is 0.34239382507990784
Cost after 1660 epochs is 0.3406136293179004
Cost after 1670 epochs is 0.3370110257678776
Cost after 1680 epochs is 0.3311964795434893
Cost after 1690 epochs is 0.32128102646452494
Cost after 1700 epochs is 0.3206061143473655
Cost after 1710 epochs is 0.31983084043374205
Cost after 1720 epochs is 0.32049348729499666
Cost after 1730 epochs is 0.3183576730029515
Cost after 1740 epochs is 0.317910856511288
Cost after 1750 epochs is 0.3166116657183696
Cost after 1760 epochs is 0.3182659918694704
Cost after 1770 epochs is 0.320619424117694
Cost after 1780 epochs is 0.3415741569604561
Cost after 1790 epochs is 0.33125226271084895
Cost after 1800 epochs is 0.33584234356085557
Cost after 1810 epochs is 0.3311269709535801
Cost after 1820 epochs is 0.3307563619354851
Cost after 1830 epochs is 0.32796958041672003
Cost after 1840 epochs is 0.3221143927738398
Cost after 1850 epochs is 0.319197854775166
Cost after 1860 epochs is 0.3181477942231375
Cost after 1870 epochs is 0.317314860593556
Cost after 1880 epochs is 0.3176902438869636
Cost after 1890 epochs is 0.3161432457306157
Cost after 1900 epochs is 0.3145766246361672
Cost after 1910 epochs is 0.3144304745680433
Cost after 1920 epochs is 0.31274198790978436
Cost after 1930 epochs is 0.3095427348630494
Cost after 1940 epochs is 0.30110378155222606
Cost after 1950 epochs is 0.30011549763440265
Cost after 1960 epochs is 0.29878709361146855
Cost after 1970 epochs is 0.300435196534384
Cost after 1980 epochs is 0.30013935343253234
Cost after 1990 epochs is 0.30009343099820385
Cost after 2000 epochs is 0.30098255015586045
Cost after 2010 epochs is 0.3009518198125595
Cost after 2020 epochs is 0.29847526099599475
Cost after 2030 epochs is 0.2993621215442026
Cost after 2040 epochs is 0.29751224101669466
Cost after 2050 epochs is 0.296830043357596
Cost after 2060 epochs is 0.29909385152101714
Cost after 2070 epochs is 0.29637892296238194
Cost after 2080 epochs is 0.2978172683643826
Cost after 2090 epochs is 0.29794313935075023
Cost after 2100 epochs is 0.29396979806393114
Cost after 2110 epochs is 0.29436499676437095
Cost after 2120 epochs is 0.29612124194357775
Cost after 2130 epochs is 0.292518317189885
Cost after 2140 epochs is 0.2983768134352726
Cost after 2150 epochs is 0.29406169908429103
Cost after 2160 epochs is 0.29283117683842624
Cost after 2170 epochs is 0.291067834690254
Cost after 2180 epochs is 0.2913954500111914
Cost after 2190 epochs is 0.2919154513257292
Cost after 2200 epochs is 0.28996990107342846
Cost after 2210 epochs is 0.2897778096663196
Cost after 2220 epochs is 0.2913115012288931
Cost after 2230 epochs is 0.2887179387620864
Cost after 2240 epochs is 0.29131671198719544
Cost after 2250 epochs is 0.2876087384719468
Cost after 2260 epochs is 0.2893378025031119
Cost after 2270 epochs is 0.28952758842806847
Cost after 2280 epochs is 0.2889340429520992
Cost after 2290 epochs is 0.28640125073370815
Cost after 2300 epochs is 0.28960722226960167
Cost after 2310 epochs is 0.2902203613483251
Cost after 2320 epochs is 0.28968893504846355
Cost after 2330 epochs is 0.2879516074294169
Cost after 2340 epochs is 0.2861677523463038
Cost after 2350 epochs is 0.2832196479246027
Cost after 2360 epochs is 0.28489799919493236
Cost after 2370 epochs is 0.28361575331303746
Cost after 2380 epochs is 0.28391984323679215
Cost after 2390 epochs is 0.28248207085632987
Cost after 2400 epochs is 0.28241930546326877
Cost after 2410 epochs is 0.28144785798441635
Cost after 2420 epochs is 0.2826505828140209
Cost after 2430 epochs is 0.2832488113646631
Cost after 2440 epochs is 0.27953002669718535
Cost after 2450 epochs is 0.27586980535823064
Cost after 2460 epochs is 0.2795835649436827
Cost after 2470 epochs is 0.28095593374078726
Cost after 2480 epochs is 0.27935837278920095
Cost after 2490 epochs is 0.28044868239101295
Cost after 2500 epochs is 0.27811298559375974
Cost after 2510 epochs is 0.27849425583498655
Cost after 2520 epochs is 0.27745642909924173
Cost after 2530 epochs is 0.2763896735325652
Cost after 2540 epochs is 0.2727075064190929
Cost after 2550 epochs is 0.27293654369774484
Cost after 2560 epochs is 0.2743128249727928
Cost after 2570 epochs is 0.2719852180044934
Cost after 2580 epochs is 0.27272241144147347
Cost after 2590 epochs is 0.27235621699323215
Cost after 2600 epochs is 0.27119089685613373
Cost after 2610 epochs is 0.27180632319976733
Cost after 2620 epochs is 0.2702452758589713
Cost after 2630 epochs is 0.2707337117133918
Cost after 2640 epochs is 0.27054056776563346
Cost after 2650 epochs is 0.2697898091615516
Cost after 2660 epochs is 0.26976753368339756
Cost after 2670 epochs is 0.2678418121717704
Cost after 2680 epochs is 0.2723735636254019
Cost after 2690 epochs is 0.2716940698092063
Cost after 2700 epochs is 0.27034278564417796
Cost after 2710 epochs is 0.2694194743345172
Cost after 2720 epochs is 0.2705413856356412
Cost after 2730 epochs is 0.2690176154713404
Cost after 2740 epochs is 0.26874559451964586
Cost after 2750 epochs is 0.2668801411257349
Cost after 2760 epochs is 0.2667870980447782
Cost after 2770 epochs is 0.26604902116920115
Cost after 2780 epochs is 0.2623136862597268
Cost after 2790 epochs is 0.26676638979749456
Cost after 2800 epochs is 0.26403702745573643
Cost after 2810 epochs is 0.26298954959324405
Cost after 2820 epochs is 0.26422183622363166
Cost after 2830 epochs is 0.2638103986980101
Cost after 2840 epochs is 0.2625947852670944
Cost after 2850 epochs is 0.2622815053346211
Cost after 2860 epochs is 0.26226216537189223
Cost after 2870 epochs is 0.25966425499103835
Cost after 2880 epochs is 0.26216907671310297
Cost after 2890 epochs is 0.2616879146569248
Cost after 2900 epochs is 0.26365288091274736
Cost after 2910 epochs is 0.2577205614476418
Cost after 2920 epochs is 0.25839627219619227
Cost after 2930 epochs is 0.259028607203608
Cost after 2940 epochs is 0.2587986973265977
Cost after 2950 epochs is 0.2563472152759951
Cost after 2960 epochs is 0.25775881775077086
Cost after 2970 epochs is 0.25771469394227703
Cost after 2980 epochs is 0.25551010342613356
Cost after 2990 epochs is 0.2555665229200765
Cost after 3000 epochs is 0.25507493339932125
Cost after 3010 epochs is 0.2540624310981445
Cost after 3020 epochs is 0.2564354806875036
Cost after 3030 epochs is 0.25594014490852296
Cost after 3040 epochs is 0.2561776674050344
Cost after 3050 epochs is 0.2564395826497092
Cost after 3060 epochs is 0.25426644844128016
Cost after 3070 epochs is 0.2515053375732044
Cost after 3080 epochs is 0.2539517198809962
Cost after 3090 epochs is 0.2551386325696129
Cost after 3100 epochs is 0.25247953055641353
Cost after 3110 epochs is 0.2482079058632544
Cost after 3120 epochs is 0.24830359110218989
Cost after 3130 epochs is 0.24810942704120753
Cost after 3140 epochs is 0.2475310626147396
Cost after 3150 epochs is 0.2469931240347489
Cost after 3160 epochs is 0.2476231619092826
Cost after 3170 epochs is 0.24830129052180616
Cost after 3180 epochs is 0.2536080488968158
Cost after 3190 epochs is 0.2545927234445736
Cost after 3200 epochs is 0.25363666913653843
Cost after 3210 epochs is 0.252892094991828
Cost after 3220 epochs is 0.2506602919880666
Cost after 3230 epochs is 0.2524674652886548
Cost after 3240 epochs is 0.2515836366155972
Cost after 3250 epochs is 0.25212094485239406
Cost after 3260 epochs is 0.2526080993886427
Cost after 3270 epochs is 0.2506144839034108
Cost after 3280 epochs is 0.25007564875919075
Cost after 3290 epochs is 0.25168239789903935
Cost after 3300 epochs is 0.2506189380792863
Cost after 3310 epochs is 0.24999455824800168
Cost after 3320 epochs is 0.25037000055865005
Cost after 3330 epochs is 0.24998781182238247
Cost after 3340 epochs is 0.24988647224672988
Cost after 3350 epochs is 0.24910604449249393
Cost after 3360 epochs is 0.2476519730315126
Cost after 3370 epochs is 0.24194519204161555
Cost after 3380 epochs is 0.24231745579311154
Cost after 3390 epochs is 0.242068271930711
Cost after 3400 epochs is 0.24254147042100027
Cost after 3410 epochs is 0.24227524262054378
Cost after 3420 epochs is 0.24251586708122747
Cost after 3430 epochs is 0.2421581660148219
Cost after 3440 epochs is 0.24131302088456236
Cost after 3450 epochs is 0.24134113930907422
Cost after 3460 epochs is 0.24103176927163733
Cost after 3470 epochs is 0.2407724383186343
Cost after 3480 epochs is 0.24068166499949792
Cost after 3490 epochs is 0.24083804464941705
Cost after 3500 epochs is 0.2404345573489283
Cost after 3510 epochs is 0.24700939344803022
Cost after 3520 epochs is 0.24819432375756584
Cost after 3530 epochs is 0.2462665682395585
Cost after 3540 epochs is 0.2452987992880495
Cost after 3550 epochs is 0.24612018315241713
Cost after 3560 epochs is 0.24419256404512443
Cost after 3570 epochs is 0.241866133337166
Cost after 3580 epochs is 0.23750337953952927
Cost after 3590 epochs is 0.23408058069828838
Cost after 3600 epochs is 0.24498248907410247
Cost after 3610 epochs is 0.23994681850901825
Cost after 3620 epochs is 0.238626482579778
Cost after 3630 epochs is 0.23990664246769658
Cost after 3640 epochs is 0.2410473906315275
Cost after 3650 epochs is 0.2386197038718208
Cost after 3660 epochs is 0.23910553992791456
Cost after 3670 epochs is 0.23946544576172685
Cost after 3680 epochs is 0.23434518133928908
Cost after 3690 epochs is 0.23393822100305367
Cost after 3700 epochs is 0.23518295713205267
Cost after 3710 epochs is 0.23470445276448187
Cost after 3720 epochs is 0.23924070575397305
Cost after 3730 epochs is 0.24284714115238604
Cost after 3740 epochs is 0.24077719031106604
Cost after 3750 epochs is 0.24159373816088042
Cost after 3760 epochs is 0.24112771140825778
Cost after 3770 epochs is 0.23842979326060523
Cost after 3780 epochs is 0.2355156925831009
Cost after 3790 epochs is 0.23535666362511892
Cost after 3800 epochs is 0.23683290081602054
Cost after 3810 epochs is 0.23694827445793917
Cost after 3820 epochs is 0.23583462293856075
Cost after 3830 epochs is 0.23001836170959844
Cost after 3840 epochs is 0.23547378023305215
Cost after 3850 epochs is 0.23853157036769165
Cost after 3860 epochs is 0.23765644302102049
Cost after 3870 epochs is 0.23456627018965895
Cost after 3880 epochs is 0.23736120396679178
Cost after 3890 epochs is 0.23128808381658636
Cost after 3900 epochs is 0.22849159712549236
Cost after 3910 epochs is 0.23698873411031549
Cost after 3920 epochs is 0.23740219901583987
Cost after 3930 epochs is 0.23222640239416997
Cost after 3940 epochs is 0.23215538952910245
Cost after 3950 epochs is 0.23283709148274662
Cost after 3960 epochs is 0.2327636186060637
Cost after 3970 epochs is 0.23315250840508508
Cost after 3980 epochs is 0.22924667680746755
Cost after 3990 epochs is 0.22562820634633315
Cost after 4000 epochs is 0.22865562294712546
Cost after 4010 epochs is 0.23563096852160395
Cost after 4020 epochs is 0.23243768921796934
Cost after 4030 epochs is 0.22957986538171732
Cost after 4040 epochs is 0.22957229610249882
Cost after 4050 epochs is 0.22848573520835316
Cost after 4060 epochs is 0.2247858446157539
Cost after 4070 epochs is 0.23336410821914377
Cost after 4080 epochs is 0.2317396527270898
Cost after 4090 epochs is 0.22774607891293794
Cost after 4100 epochs is 0.22559517440525564
Cost after 4110 epochs is 0.23187898665139012
Cost after 4120 epochs is 0.23579791096569006
Cost after 4130 epochs is 0.2295948869571262
Cost after 4140 epochs is 0.22584709224178276
Cost after 4150 epochs is 0.22909127754922368
Cost after 4160 epochs is 0.2333114796334365
Cost after 4170 epochs is 0.22592427338390658
Cost after 4180 epochs is 0.22749264558080615
Cost after 4190 epochs is 0.23385854840327114
Cost after 4200 epochs is 0.2253129852448298
Cost after 4210 epochs is 0.22257344168823812
Cost after 4220 epochs is 0.2316884422723068
Cost after 4230 epochs is 0.23004265031691457
Cost after 4240 epochs is 0.22445695939042998
Cost after 4250 epochs is 0.22658895064949097
Cost after 4260 epochs is 0.22986253578816393
Cost after 4270 epochs is 0.23044064156381472
Cost after 4280 epochs is 0.22947704220130508
Cost after 4290 epochs is 0.22965826970121012
Cost after 4300 epochs is 0.23026590738976455
Cost after 4310 epochs is 0.22912179384131146
Cost after 4320 epochs is 0.2284799448009291
Cost after 4330 epochs is 0.22413454222156837
Cost after 4340 epochs is 0.22174692894536763
Cost after 4350 epochs is 0.22579090008126437
Cost after 4360 epochs is 0.22760494053770197
Cost after 4370 epochs is 0.2267942216021744
Cost after 4380 epochs is 0.22692003878610648
Cost after 4390 epochs is 0.2258955131146606
Cost after 4400 epochs is 0.22301045264042396
Cost after 4410 epochs is 0.221616507728844
Cost after 4420 epochs is 0.22217093939457452
Cost after 4430 epochs is 0.2225816002157424
Cost after 4440 epochs is 0.22416320423556527
Cost after 4450 epochs is 0.22190328481786192
Cost after 4460 epochs is 0.2218933337693835
Cost after 4470 epochs is 0.22297599255862835
Cost after 4480 epochs is 0.22349555440500776
Cost after 4490 epochs is 0.22576812584602238
Cost after 4500 epochs is 0.2251838266137034
Cost after 4510 epochs is 0.2249720233660468
Cost after 4520 epochs is 0.22552926119648797
Cost after 4530 epochs is 0.22058146275290713
Cost after 4540 epochs is 0.22149550574082646
Cost after 4550 epochs is 0.22141283996758418
Cost after 4560 epochs is 0.21946485651617903
Cost after 4570 epochs is 0.22137988751570287
Cost after 4580 epochs is 0.22016126086838927
Cost after 4590 epochs is 0.22126968381074208
Cost after 4600 epochs is 0.2209210329690645
Cost after 4610 epochs is 0.22208525283639338
Cost after 4620 epochs is 0.2209254046861853
Cost after 4630 epochs is 0.22156139167762273
Cost after 4640 epochs is 0.21987841842460767
Cost after 4650 epochs is 0.22069401936234817
Cost after 4660 epochs is 0.21929234094365735
Cost after 4670 epochs is 0.21936006785662396
Cost after 4680 epochs is 0.21875075615637588
Cost after 4690 epochs is 0.21962187257517046
Cost after 4700 epochs is 0.2208172089171569
Cost after 4710 epochs is 0.2217960751507673
Cost after 4720 epochs is 0.21683452247223076
Cost after 4730 epochs is 0.2174046326160635
Cost after 4740 epochs is 0.21908805915229704
Cost after 4750 epochs is 0.21837690601049506
Cost after 4760 epochs is 0.21841073121224236
Cost after 4770 epochs is 0.21785085981292163
Cost after 4780 epochs is 0.21819706632980845
Cost after 4790 epochs is 0.21824736239119175
Cost after 4800 epochs is 0.21811095593208762
Cost after 4810 epochs is 0.21800606638127012
Cost after 4820 epochs is 0.217375608379621
Cost after 4830 epochs is 0.21763636726613556
Cost after 4840 epochs is 0.21566271032921855
Cost after 4850 epochs is 0.21735111779613997
Cost after 4860 epochs is 0.2185387797439712
Cost after 4870 epochs is 0.21758425163435396
Cost after 4880 epochs is 0.21414383525940475
Cost after 4890 epochs is 0.21090524344979153
Cost after 4900 epochs is 0.21185664071208268
Cost after 4910 epochs is 0.21345265891614787
Cost after 4920 epochs is 0.2216868519638286
Cost after 4930 epochs is 0.21690852961913826
Cost after 4940 epochs is 0.21717777748917408
Cost after 4950 epochs is 0.21681892678230993
Cost after 4960 epochs is 0.2153104174121715
Cost after 4970 epochs is 0.21186277421315278
Cost after 4980 epochs is 0.2107815464155775
Cost after 4990 epochs is 0.21434714520602646
Cost after 5000 epochs is 0.21836154402504426
Cost after 5010 epochs is 0.2150451816846959
Cost after 5020 epochs is 0.21732755266115883
Cost after 5030 epochs is 0.21599961707943652
Cost after 5040 epochs is 0.2146077009198641
Cost after 5050 epochs is 0.21436915355937153
Cost after 5060 epochs is 0.2126715715204942
Cost after 5070 epochs is 0.208364744666893
Cost after 5080 epochs is 0.20795128747553623
Cost after 5090 epochs is 0.20885597494444014
Cost after 5100 epochs is 0.2131077241720419
Cost after 5110 epochs is 0.2153619112865813
Cost after 5120 epochs is 0.21577394201594505
Cost after 5130 epochs is 0.21442209266873552
Cost after 5140 epochs is 0.21428608274835856
Cost after 5150 epochs is 0.21133460166247622
Cost after 5160 epochs is 0.2070495471437771
Cost after 5170 epochs is 0.20692687179949687
Cost after 5180 epochs is 0.20717372448618862
Cost after 5190 epochs is 0.20598921774991133
Cost after 5200 epochs is 0.20711994057716762
Cost after 5210 epochs is 0.21089696865398824
Cost after 5220 epochs is 0.22043891711555103
Cost after 5230 epochs is 0.21345790642366932
Cost after 5240 epochs is 0.2123752264695368
Cost after 5250 epochs is 0.21215802730067065
Cost after 5260 epochs is 0.21059973030204238
Cost after 5270 epochs is 0.21132714877199046
Cost after 5280 epochs is 0.20976626468619888
Cost after 5290 epochs is 0.20742520418276675
Cost after 5300 epochs is 0.20695439396500642
Cost after 5310 epochs is 0.20581944108469946
Cost after 5320 epochs is 0.20614760664055273
Cost after 5330 epochs is 0.20941310102677804
Cost after 5340 epochs is 0.2111152630384859
Cost after 5350 epochs is 0.2098189693550647
Cost after 5360 epochs is 0.20777476880358597
Cost after 5370 epochs is 0.20642210084832474
Cost after 5380 epochs is 0.2073830676218139
Cost after 5390 epochs is 0.21019545025404096
Cost after 5400 epochs is 0.21584996513639204
Cost after 5410 epochs is 0.2124295959364984
Cost after 5420 epochs is 0.21512351556615555
Cost after 5430 epochs is 0.21095781033327027
Cost after 5440 epochs is 0.20974178750621397
Cost after 5450 epochs is 0.20934620867881779
Cost after 5460 epochs is 0.20862508010284062
Cost after 5470 epochs is 0.21080189311630393
Cost after 5480 epochs is 0.20868923985579274
Cost after 5490 epochs is 0.2093096147867125
Cost after 5500 epochs is 0.21137614006205283
Cost after 5510 epochs is 0.209027838227749
Cost after 5520 epochs is 0.20933977067311393
Cost after 5530 epochs is 0.20812669258175825
Cost after 5540 epochs is 0.20833844862824527
Cost after 5550 epochs is 0.21037623653096263
Cost after 5560 epochs is 0.20714864429092886
Cost after 5570 epochs is 0.2046342455193824
Cost after 5580 epochs is 0.2030596201135063
Cost after 5590 epochs is 0.2039411168229471
Cost after 5600 epochs is 0.20649316041157026
Cost after 5610 epochs is 0.20878201573074398
Cost after 5620 epochs is 0.2078772935044017
Cost after 5630 epochs is 0.20732539081672954
Cost after 5640 epochs is 0.2085567327664599
Cost after 5650 epochs is 0.20706433414079953
Cost after 5660 epochs is 0.20841004312516626
Cost after 5670 epochs is 0.2059257036779992
Cost after 5680 epochs is 0.20709289972781
Cost after 5690 epochs is 0.20837833037328854
Cost after 5700 epochs is 0.20595576653045644
Cost after 5710 epochs is 0.20442887914952526
Cost after 5720 epochs is 0.20186294828342824
Cost after 5730 epochs is 0.2023100506591138
Cost after 5740 epochs is 0.201436922523617
Cost after 5750 epochs is 0.20624510182380973
Cost after 5760 epochs is 0.20956940589284934
Cost after 5770 epochs is 0.21071381833101663
Cost after 5780 epochs is 0.20583116419224667
Cost after 5790 epochs is 0.2046258309508361
Cost after 5800 epochs is 0.20383684031495927
Cost after 5810 epochs is 0.20680558597620996
Cost after 5820 epochs is 0.2092816216600948
Cost after 5830 epochs is 0.20601276808424865
Cost after 5840 epochs is 0.20368028155410814
Cost after 5850 epochs is 0.20514259763230516
Cost after 5860 epochs is 0.20447729278487684
Cost after 5870 epochs is 0.20277661895977464
Cost after 5880 epochs is 0.20433671282345497
Cost after 5890 epochs is 0.20474325357029882
Cost after 5900 epochs is 0.2024319035490838
Cost after 5910 epochs is 0.20281280463368237
Cost after 5920 epochs is 0.20323621806559716
Cost after 5930 epochs is 0.20290271974404625
Cost after 5940 epochs is 0.2035747158058361
Cost after 5950 epochs is 0.20304054577816735
Cost after 5960 epochs is 0.2040730730505779
Cost after 5970 epochs is 0.20426076589586423
Cost after 5980 epochs is 0.2030701076825719
Cost after 5990 epochs is 0.2029069963109929
Cost after 6000 epochs is 0.20253770850052638
Cost after 6010 epochs is 0.20435543398605224
Cost after 6020 epochs is 0.2019761859204743
Cost after 6030 epochs is 0.20382103868983417
Cost after 6040 epochs is 0.20288705707756888
Cost after 6050 epochs is 0.2044096694020846
Cost after 6060 epochs is 0.20483213169908457
Cost after 6070 epochs is 0.20571166167168764
Cost after 6080 epochs is 0.2071449745860062
Cost after 6090 epochs is 0.20708093455992957
Cost after 6100 epochs is 0.20772096012024946
Cost after 6110 epochs is 0.20452208313423156
Cost after 6120 epochs is 0.20059873825178645
Cost after 6130 epochs is 0.20128237612353608
Cost after 6140 epochs is 0.2022323520347071
Cost after 6150 epochs is 0.2015589381527689
Cost after 6160 epochs is 0.2004854556446479
Cost after 6170 epochs is 0.20283257172390676
Cost after 6180 epochs is 0.2046159706343859
Cost after 6190 epochs is 0.20531415305308395
Cost after 6200 epochs is 0.20288781357350705
Cost after 6210 epochs is 0.2020851947057073
Cost after 6220 epochs is 0.20057942853345273
Cost after 6230 epochs is 0.2007286851118223
Cost after 6240 epochs is 0.2018911856008457
Cost after 6250 epochs is 0.20336292130489508
Cost after 6260 epochs is 0.20306104334542857
Cost after 6270 epochs is 0.20061348726228964
Cost after 6280 epochs is 0.19922673231681728
Cost after 6290 epochs is 0.19742444598975234
Cost after 6300 epochs is 0.1988977645511954
Cost after 6310 epochs is 0.19846494035011586
Cost after 6320 epochs is 0.1971002680398923
Cost after 6330 epochs is 0.19884468063196184
Cost after 6340 epochs is 0.20043851563757434
Cost after 6350 epochs is 0.20422803202872336
Cost after 6360 epochs is 0.20211218869854994
Cost after 6370 epochs is 0.2023190322002486
Cost after 6380 epochs is 0.20318466913620284
Cost after 6390 epochs is 0.20203108122305294
Cost after 6400 epochs is 0.20308535334104366
Cost after 6410 epochs is 0.20146243902187694
Cost after 6420 epochs is 0.19924276041898745
Cost after 6430 epochs is 0.1966081548521083
Cost after 6440 epochs is 0.1970363390559985
Cost after 6450 epochs is 0.19850999744189454
Cost after 6460 epochs is 0.19748198905539344
Cost after 6470 epochs is 0.1978045097394216
Cost after 6480 epochs is 0.19771742623179928
Cost after 6490 epochs is 0.19832927818835294
Cost after 6500 epochs is 0.1981635432953485
Cost after 6510 epochs is 0.19723438697587486
Cost after 6520 epochs is 0.19799226447358773
Cost after 6530 epochs is 0.19775645618771384
Cost after 6540 epochs is 0.19753686659076009
Cost after 6550 epochs is 0.1973296750562762
Cost after 6560 epochs is 0.19597536868724255
Cost after 6570 epochs is 0.19735344506949595
Cost after 6580 epochs is 0.19757161968000883
Cost after 6590 epochs is 0.19707605284314586
Cost after 6600 epochs is 0.19824981669881184
Cost after 6610 epochs is 0.19727408452280892
Cost after 6620 epochs is 0.19718444389283568
Cost after 6630 epochs is 0.19702120103924456
Cost after 6640 epochs is 0.19755848048472854
Cost after 6650 epochs is 0.19744151470401095
Cost after 6660 epochs is 0.1971960551254828
Cost after 6670 epochs is 0.19622161874932414
Cost after 6680 epochs is 0.19682798542177482
Cost after 6690 epochs is 0.19689354825687544
Cost after 6700 epochs is 0.20411166560570704
Cost after 6710 epochs is 0.20596042556559288
Cost after 6720 epochs is 0.20054881912499478
Cost after 6730 epochs is 0.20071785720867952
Cost after 6740 epochs is 0.20083084346852276
Cost after 6750 epochs is 0.20058092687197715
Cost after 6760 epochs is 0.19998987338364585
Cost after 6770 epochs is 0.19447143537885955
Cost after 6780 epochs is 0.1962103355589715
Cost after 6790 epochs is 0.19494382623365075
Cost after 6800 epochs is 0.1962038212828343
Cost after 6810 epochs is 0.19493004739367595
Cost after 6820 epochs is 0.19555509627227466
Cost after 6830 epochs is 0.19483030190589026
Cost after 6840 epochs is 0.19573311408040442
Cost after 6850 epochs is 0.19625776827274644
Cost after 6860 epochs is 0.19615254297672993
Cost after 6870 epochs is 0.19578278356700585
Cost after 6880 epochs is 0.1952682980450143
Cost after 6890 epochs is 0.19569443642682866
Cost after 6900 epochs is 0.19488294720919522
Cost after 6910 epochs is 0.19465526108455683
Cost after 6920 epochs is 0.19527899799515883
Cost after 6930 epochs is 0.19509495798962725
Cost after 6940 epochs is 0.19464408275917464
Cost after 6950 epochs is 0.1950715796720015
Cost after 6960 epochs is 0.19542133862893335
Cost after 6970 epochs is 0.19681132178104652
Cost after 6980 epochs is 0.19456403422844992
Cost after 6990 epochs is 0.19459504261988608
Cost after 7000 epochs is 0.19554901279410658
Cost after 7010 epochs is 0.1983771006668004
Cost after 7020 epochs is 0.19965083572598613
Cost after 7030 epochs is 0.20238545812281217
Cost after 7040 epochs is 0.198431992218991
Cost after 7050 epochs is 0.1943236746464492
Cost after 7060 epochs is 0.1932853325477542
Cost after 7070 epochs is 0.1939421690773602
Cost after 7080 epochs is 0.19416666335737162
Cost after 7090 epochs is 0.19522209429880938
Cost after 7100 epochs is 0.1945376784560038
Cost after 7110 epochs is 0.1930676380051193
Cost after 7120 epochs is 0.19298558359797635
Cost after 7130 epochs is 0.19519882043086612
Cost after 7140 epochs is 0.19475218486172394
Cost after 7150 epochs is 0.1943624050271758
Cost after 7160 epochs is 0.19571473819854296
Cost after 7170 epochs is 0.19480820048394112
Cost after 7180 epochs is 0.1933831057993592
Cost after 7190 epochs is 0.1928154067436793
Cost after 7200 epochs is 0.19386002866847976
Cost after 7210 epochs is 0.19422106397719938
Cost after 7220 epochs is 0.19467186853180757
Cost after 7230 epochs is 0.1939309009341512
Cost after 7240 epochs is 0.19129502973302273
Cost after 7250 epochs is 0.19209537681165792
Cost after 7260 epochs is 0.19264456975399546
Cost after 7270 epochs is 0.19229602595101697
Cost after 7280 epochs is 0.1916135849934707
Cost after 7290 epochs is 0.1927835044706852
Cost after 7300 epochs is 0.19446622241134812
Cost after 7310 epochs is 0.19403024815205788
Cost after 7320 epochs is 0.1919189665823316
Cost after 7330 epochs is 0.19159641648216053
Cost after 7340 epochs is 0.19156867088462753
Cost after 7350 epochs is 0.19071728852974965
Cost after 7360 epochs is 0.1922415533566183
Cost after 7370 epochs is 0.19111358255383196
Cost after 7380 epochs is 0.19179675470151533
Cost after 7390 epochs is 0.19153964873201104
Cost after 7400 epochs is 0.19297906890646047
Cost after 7410 epochs is 0.19364835499407765
Cost after 7420 epochs is 0.19706104915973344
Cost after 7430 epochs is 0.1991898637000247
Cost after 7440 epochs is 0.1934327629120244
Cost after 7450 epochs is 0.19103489412183353
Cost after 7460 epochs is 0.1903972343967605
Cost after 7470 epochs is 0.19332324351259947
Cost after 7480 epochs is 0.1925303983234958
Cost after 7490 epochs is 0.19134333942347143
Cost after 7500 epochs is 0.1927469028314276
Cost after 7510 epochs is 0.19138376120804568
Cost after 7520 epochs is 0.1908236348464888
Cost after 7530 epochs is 0.19243600638641958
Cost after 7540 epochs is 0.1939432821228928
Cost after 7550 epochs is 0.1923456103521401
Cost after 7560 epochs is 0.19311826213344063
Cost after 7570 epochs is 0.1922137251881455
Cost after 7580 epochs is 0.19121938422004545
Cost after 7590 epochs is 0.1907675729398208
Cost after 7600 epochs is 0.19023257911548871
Cost after 7610 epochs is 0.19236363501056702
Cost after 7620 epochs is 0.1905791347494583
Cost after 7630 epochs is 0.19094251975939067
Cost after 7640 epochs is 0.19337445959246644
Cost after 7650 epochs is 0.1909261565762265
Cost after 7660 epochs is 0.19070766571584621
Cost after 7670 epochs is 0.1919460338747451
Cost after 7680 epochs is 0.19079818988854808
Cost after 7690 epochs is 0.1905501275220329
Cost after 7700 epochs is 0.19211298642770758
Cost after 7710 epochs is 0.19061385177159942
Cost after 7720 epochs is 0.19016316331584196
Cost after 7730 epochs is 0.1909318040343908
Cost after 7740 epochs is 0.19106288081681005
Cost after 7750 epochs is 0.19252066715355173
Cost after 7760 epochs is 0.19193556466446116
Cost after 7770 epochs is 0.19007401204050253
Cost after 7780 epochs is 0.19004070900479667
Cost after 7790 epochs is 0.19121363584614903
Cost after 7800 epochs is 0.19351935145166638
Cost after 7810 epochs is 0.1975901854480867
Cost after 7820 epochs is 0.1942750194659716
Cost after 7830 epochs is 0.18928661295966256
Cost after 7840 epochs is 0.18974902059316873
Cost after 7850 epochs is 0.18969253732315927
Cost after 7860 epochs is 0.18971604655462987
Cost after 7870 epochs is 0.19019605605737505
Cost after 7880 epochs is 0.19043038202248155
Cost after 7890 epochs is 0.1908275353494652
Cost after 7900 epochs is 0.18841208490832437
Cost after 7910 epochs is 0.19098149438398573
Cost after 7920 epochs is 0.1879511589254907
Cost after 7930 epochs is 0.1915230821314704
Cost after 7940 epochs is 0.1883548683782248
Cost after 7950 epochs is 0.19373063245303346
Cost after 7960 epochs is 0.19521670216354076
Cost after 7970 epochs is 0.18962558730445034
Cost after 7980 epochs is 0.1891851220497701
Cost after 7990 epochs is 0.18921116073954342
Cost after 8000 epochs is 0.18891821959708982
Cost after 8010 epochs is 0.19058612371634018
Cost after 8020 epochs is 0.18861719978043723
Cost after 8030 epochs is 0.18827433389024767
Cost after 8040 epochs is 0.18958627762562005
Cost after 8050 epochs is 0.1890592206480474
Cost after 8060 epochs is 0.189394425887649
Cost after 8070 epochs is 0.1905439880642314
Cost after 8080 epochs is 0.1869572036674099
Cost after 8090 epochs is 0.189440985269527
Cost after 8100 epochs is 0.19118465395006082
Cost after 8110 epochs is 0.19002778184856467
Cost after 8120 epochs is 0.19031946634288208
Cost after 8130 epochs is 0.19274764836925215
Cost after 8140 epochs is 0.1979198087713838
Cost after 8150 epochs is 0.1924026529008262
Cost after 8160 epochs is 0.1886902393241113
Cost after 8170 epochs is 0.18951186038960627
Cost after 8180 epochs is 0.18948617999956316
Cost after 8190 epochs is 0.18775962672488314
Cost after 8200 epochs is 0.18887891129986922
Cost after 8210 epochs is 0.18834304263880214
Cost after 8220 epochs is 0.18848568632110776
Cost after 8230 epochs is 0.18840631894055873
Cost after 8240 epochs is 0.18654130828757748
Cost after 8250 epochs is 0.18972807048982224
Cost after 8260 epochs is 0.18921870179149053
Cost after 8270 epochs is 0.18808155089015235
Cost after 8280 epochs is 0.18843673700026578
Cost after 8290 epochs is 0.18805424690867362
Cost after 8300 epochs is 0.18894773399513876
Cost after 8310 epochs is 0.18608523322276474
Cost after 8320 epochs is 0.1887917338393432
Cost after 8330 epochs is 0.18913966328561624
Cost after 8340 epochs is 0.18973377486877457
Cost after 8350 epochs is 0.1875204002838948
Cost after 8360 epochs is 0.18723903289634947
Cost after 8370 epochs is 0.19054037437518623
Cost after 8380 epochs is 0.1934873775180864
Cost after 8390 epochs is 0.19411843479355761
Cost after 8400 epochs is 0.18783451032469986
Cost after 8410 epochs is 0.18762101491113314
Cost after 8420 epochs is 0.18563342743745065
Cost after 8430 epochs is 0.18621568884070974
Cost after 8440 epochs is 0.18835477129890713
Cost after 8450 epochs is 0.18793577266174907
Cost after 8460 epochs is 0.18584526608917745
Cost after 8470 epochs is 0.18465929745547383
Cost after 8480 epochs is 0.1891320430736906
Cost after 8490 epochs is 0.19007323196016704
Cost after 8500 epochs is 0.18802416787248716
Cost after 8510 epochs is 0.18785141360526983
Cost after 8520 epochs is 0.18720461099598038
Cost after 8530 epochs is 0.18757254169221885
Cost after 8540 epochs is 0.18825473085775085
Cost after 8550 epochs is 0.18827595308668166
Cost after 8560 epochs is 0.19045445395746793
Cost after 8570 epochs is 0.19074138458906145
Cost after 8580 epochs is 0.1880134136193453
Cost after 8590 epochs is 0.18507720384980686
Cost after 8600 epochs is 0.1873367566757607
Cost after 8610 epochs is 0.18682764102025365
Cost after 8620 epochs is 0.184106946058108
Cost after 8630 epochs is 0.18454367929999324
Cost after 8640 epochs is 0.184333815721715
Cost after 8650 epochs is 0.18579865318169267
Cost after 8660 epochs is 0.18730151598145495
Cost after 8670 epochs is 0.18549480384565145
Cost after 8680 epochs is 0.1891389997042537
Cost after 8690 epochs is 0.1920278498139088
Cost after 8700 epochs is 0.19421747628111188
Cost after 8710 epochs is 0.1913936553734741
Cost after 8720 epochs is 0.1845384205616715
Cost after 8730 epochs is 0.18551086734029895
Cost after 8740 epochs is 0.18555254316614156
Cost after 8750 epochs is 0.18568514044102694
Cost after 8760 epochs is 0.18626151174530525
Cost after 8770 epochs is 0.18632775625589917
Cost after 8780 epochs is 0.18393025659074272
Cost after 8790 epochs is 0.18357773671185967
Cost after 8800 epochs is 0.18385797739215579
Cost after 8810 epochs is 0.18535523906273793
Cost after 8820 epochs is 0.18599170747714946
Cost after 8830 epochs is 0.1861278842615755
Cost after 8840 epochs is 0.18699071368175516
Cost after 8850 epochs is 0.18518291895935465
Cost after 8860 epochs is 0.18405740974345244
Cost after 8870 epochs is 0.1859512374468288
Cost after 8880 epochs is 0.1852984909995377
Cost after 8890 epochs is 0.18713763878623194
Cost after 8900 epochs is 0.1886722894622872
Cost after 8910 epochs is 0.19453255740251219
Cost after 8920 epochs is 0.18767690223510972
Cost after 8930 epochs is 0.18347278186002883
Cost after 8940 epochs is 0.18500474900541783
Cost after 8950 epochs is 0.18498330104503163
Cost after 8960 epochs is 0.18601276098770528
Cost after 8970 epochs is 0.18400824995131557
Cost after 8980 epochs is 0.18391132013654912
Cost after 8990 epochs is 0.18162645370964486
Cost after 9000 epochs is 0.18583799780728258
Cost after 9010 epochs is 0.18455471258397307
Cost after 9020 epochs is 0.1843464504134578
Cost after 9030 epochs is 0.18504290319743158
Cost after 9040 epochs is 0.1846631204853309
Cost after 9050 epochs is 0.1861919511972069
Cost after 9060 epochs is 0.18493444754676963
Cost after 9070 epochs is 0.18493359598055928
Cost after 9080 epochs is 0.1871345290857096
Cost after 9090 epochs is 0.18908302267079619
Cost after 9100 epochs is 0.19474160859301118
Cost after 9110 epochs is 0.1881321111029067
Cost after 9120 epochs is 0.1842863406117511
Cost after 9130 epochs is 0.1809518019494957
Cost after 9140 epochs is 0.18450698378806113
Cost after 9150 epochs is 0.18475412265153157
Cost after 9160 epochs is 0.18419397858182116
Cost after 9170 epochs is 0.18447802064714452
Cost after 9180 epochs is 0.18423483463181242
Cost after 9190 epochs is 0.184864487694961
Cost after 9200 epochs is 0.1835524085728957
Cost after 9210 epochs is 0.18264847055284159
Cost after 9220 epochs is 0.1806586102631152
Cost after 9230 epochs is 0.1852573015582489
Cost after 9240 epochs is 0.1838427372891795
Cost after 9250 epochs is 0.18768654391343345
Cost after 9260 epochs is 0.19290318596536613
Cost after 9270 epochs is 0.18967053068288373
Cost after 9280 epochs is 0.18837969628302742
Cost after 9290 epochs is 0.18309265197890098
Cost after 9300 epochs is 0.18214572168945511
Cost after 9310 epochs is 0.1814789499479022
Cost after 9320 epochs is 0.182843654043378
Cost after 9330 epochs is 0.18309096045311202
Cost after 9340 epochs is 0.18408828193982466
Cost after 9350 epochs is 0.18310431504394578
Cost after 9360 epochs is 0.18197905137673176
Cost after 9370 epochs is 0.18166555999252057
Cost after 9380 epochs is 0.18366474430792415
Cost after 9390 epochs is 0.1830738647799767
Cost after 9400 epochs is 0.1861170197806509
Cost after 9410 epochs is 0.18929822589774323
Cost after 9420 epochs is 0.1909789673250502
Cost after 9430 epochs is 0.18821336556122753
Cost after 9440 epochs is 0.18297912215018367
Cost after 9450 epochs is 0.18161333176409142
Cost after 9460 epochs is 0.18318754717464028
Cost after 9470 epochs is 0.18328059985297734
Cost after 9480 epochs is 0.18281539791216375
Cost after 9490 epochs is 0.18239805931784048
Cost after 9500 epochs is 0.1821838286006783
Cost after 9510 epochs is 0.1826873187370064
Cost after 9520 epochs is 0.1821992827982419
Cost after 9530 epochs is 0.1829098851239267
Cost after 9540 epochs is 0.1831531178006162
Cost after 9550 epochs is 0.18338588198080644
Cost after 9560 epochs is 0.1824393071286802
Cost after 9570 epochs is 0.1819288763061527
Cost after 9580 epochs is 0.18322511515372153
Cost after 9590 epochs is 0.1843303662308409
Cost after 9600 epochs is 0.18228509478288277
Cost after 9610 epochs is 0.18286689340201695
Cost after 9620 epochs is 0.18190155999632374
Cost after 9630 epochs is 0.18236422958861048
Cost after 9640 epochs is 0.1828905559545639
Cost after 9650 epochs is 0.1831824520060896
Cost after 9660 epochs is 0.18392484104595938
Cost after 9670 epochs is 0.18455370738888205
Cost after 9680 epochs is 0.1878346073915413
Cost after 9690 epochs is 0.1909949688110066
Cost after 9700 epochs is 0.18597040046178268
Cost after 9710 epochs is 0.18006206263758595
Cost after 9720 epochs is 0.18263405041925168
Cost after 9730 epochs is 0.18269786465900104
Cost after 9740 epochs is 0.1818973707832466
Cost after 9750 epochs is 0.18118370791656346
Cost after 9760 epochs is 0.1792609370602179
Cost after 9770 epochs is 0.1801763918833329
Cost after 9780 epochs is 0.1861539210231999
Cost after 9790 epochs is 0.18406360906860558
Cost after 9800 epochs is 0.18227357255611387
Cost after 9810 epochs is 0.18088069913635935
Cost after 9820 epochs is 0.1829060899693975
Cost after 9830 epochs is 0.18195492247501785
Cost after 9840 epochs is 0.18283471932905157
Cost after 9850 epochs is 0.18360429771784648
Cost after 9860 epochs is 0.1839753956277975
Cost after 9870 epochs is 0.1853970528395417
Cost after 9880 epochs is 0.188657219486418
Cost after 9890 epochs is 0.19049975813604397
Cost after 9900 epochs is 0.18920318668244607
Cost after 9910 epochs is 0.1896963851678113
Cost after 9920 epochs is 0.18778534144240155
Cost after 9930 epochs is 0.18059218439812919
Cost after 9940 epochs is 0.17951036762220837
Cost after 9950 epochs is 0.18120615789133404
Cost after 9960 epochs is 0.18305842365479566
Cost after 9970 epochs is 0.1808772388390086
Cost after 9980 epochs is 0.17930698369708337
Cost after 9990 epochs is 0.18396538240442814

Training finished.

In [55]:
# Plot training error
plt.plot(cost)
Out[55]:
[<matplotlib.lines.Line2D at 0x22b9d3fddc8>]
In [56]:
# Predictions with train data
y_pred_train = predict(X_train, parameters)
In [57]:
# Visualize predictions
y_pred_train
Out[57]:
array([[8.46447897e-01, 9.45659399e-01, 7.59251897e-01, 9.45689543e-01,
        8.88486098e-01, 9.39620244e-01, 1.10562780e-02, 1.32105272e-06,
        6.24757316e-01, 9.45616326e-01, 5.92787967e-02, 9.45670998e-01,
        2.67467203e-06, 9.45685972e-01, 2.00006099e-01, 4.41845269e-03,
        9.15748813e-01, 8.86964240e-01, 9.45603905e-01, 8.79641562e-01,
        9.45676967e-01, 4.00376683e-01, 9.45640982e-01, 9.45655593e-01,
        9.37751082e-01, 9.40012996e-01, 9.45655817e-01, 9.30541919e-01,
        6.79177457e-02, 9.45622409e-01, 9.70085108e-03, 3.69532534e-06,
        3.18360336e-06, 5.20677362e-01, 9.45682046e-01, 1.24604860e-02,
        9.45627350e-01, 2.17736050e-05, 9.45676627e-01, 9.45642279e-01,
        9.45612266e-01, 9.41456977e-01, 9.45680886e-01, 6.00839773e-02,
        1.96253632e-02, 4.01343059e-01, 1.54330715e-05, 1.81829105e-31,
        9.37121729e-01, 9.45663544e-01, 9.33883486e-01, 9.45649777e-01,
        2.00548720e-02, 9.45655529e-01, 7.15484695e-03, 9.45681254e-01,
        9.30445336e-01, 2.53499742e-15, 9.45599478e-01, 9.45693809e-01,
        9.45658434e-01, 9.45649409e-01, 9.45626697e-01, 1.89061432e-05,
        1.98448976e-04, 9.45659460e-01, 9.43930492e-01, 1.47935042e-01,
        9.31903266e-01, 9.39146762e-01, 2.94225160e-04, 9.45677807e-01,
        9.45684623e-01, 9.45698522e-01, 7.28841580e-03, 2.50988926e-08,
        9.99548507e-09, 9.45674159e-01, 1.63547615e-01, 9.45674101e-01,
        6.55720948e-01, 5.53739246e-02, 9.45690113e-01, 8.11174594e-05,
        5.91866364e-01, 6.24384419e-04, 1.32415376e-06, 9.45678989e-01,
        8.88152783e-01, 6.72521518e-06, 9.45626759e-01, 9.45691838e-01,
        1.20490249e-04, 9.45658665e-01, 3.90246852e-10, 9.45629891e-01,
        1.55759376e-01, 9.45678378e-01, 8.51676353e-01, 1.08882566e-02,
        9.45625340e-01, 4.25655851e-19, 9.45672084e-01, 1.43345515e-05,
        9.45667532e-01, 9.45690920e-01, 9.24321828e-01, 7.25016750e-01,
        9.36637410e-01, 1.06503345e-03, 9.28566215e-01, 9.45633756e-01,
        4.87972678e-02, 9.45634050e-01, 9.45686748e-01, 1.27455198e-13,
        9.23434139e-01, 8.49232682e-01, 9.38907443e-01, 9.23695870e-01,
        8.67570554e-01, 9.45616921e-01, 9.45692475e-01, 9.18749356e-01,
        4.86888469e-03, 9.45637993e-01, 9.45677959e-01, 9.45696369e-01,
        1.79739529e-04, 9.45602205e-01, 9.45677704e-01, 1.32635128e-05,
        9.45590477e-01, 9.45616613e-01, 7.89134966e-04, 9.45643452e-01,
        9.10591670e-01, 5.74803620e-03, 8.21774592e-01, 9.45647935e-01,
        9.32787857e-01, 9.39502515e-01, 7.60637981e-01, 9.45671619e-01,
        9.16468392e-01, 9.45620755e-01, 7.99158794e-01, 9.45660340e-01,
        9.45646997e-01, 5.64549709e-01, 8.14369390e-01, 9.35192654e-01,
        9.45670835e-01, 2.49500168e-18, 9.45663074e-01, 8.06621278e-01,
        9.37013734e-01, 2.11528936e-09, 6.03848290e-04, 4.18911151e-02,
        2.28508317e-02, 2.42944553e-03, 9.45699999e-01, 3.28922587e-18,
        1.48837771e-05, 1.51089933e-02, 9.45694301e-01, 9.07446708e-01,
        9.45658014e-01, 9.45626819e-01, 9.45646318e-01, 8.42669132e-01,
        3.32921536e-01, 8.89492079e-01, 7.32561795e-01, 2.18876791e-04,
        4.35293819e-04, 9.45663951e-01, 1.05587965e-04, 7.80419271e-01,
        1.95779662e-04, 9.45686165e-01, 7.48566119e-01, 9.29761954e-01,
        8.63021582e-01, 3.88286962e-05, 9.44254903e-01, 1.82615968e-01,
        6.67477487e-01, 9.45686523e-01, 9.45693189e-01, 9.28264072e-01,
        8.98936018e-01, 9.41880322e-01, 7.87101605e-04, 6.12143625e-01,
        1.62217422e-04, 1.09561854e-04, 9.45680775e-01, 9.32037477e-01,
        1.03793632e-01, 9.03931320e-01, 5.73258710e-04, 1.96299261e-07,
        1.31721687e-16, 9.45633948e-01, 8.77434122e-01, 4.79751005e-02,
        8.83990093e-01, 9.45626811e-01, 1.62120388e-01, 9.88590109e-05,
        9.45665983e-01, 9.24612809e-01, 9.45680858e-01, 9.27519144e-01,
        9.45655006e-01, 7.89215323e-01, 9.15207071e-01, 9.40649353e-01,
        9.45609426e-01, 5.85024431e-07, 4.75160395e-01, 7.50870278e-01,
        1.52194749e-05, 7.15276206e-01, 9.45656437e-01, 9.45660129e-01,
        9.45663901e-01, 9.25839963e-01, 3.23296466e-01, 8.68496974e-01,
        4.45415115e-01, 2.94798247e-05, 9.45678164e-01, 1.70081315e-07,
        9.18021165e-01, 5.45376928e-01, 9.45628662e-01, 8.72899540e-01,
        9.45637403e-01, 1.29033194e-01, 1.60581840e-04, 1.36707414e-09,
        9.27196000e-01, 6.72906955e-01, 9.45651764e-01, 9.45658467e-01,
        8.52647493e-01, 9.30235525e-01, 1.66296326e-01, 9.30851114e-01,
        9.45633606e-01, 7.92668870e-01, 9.16932612e-01, 8.32259386e-11,
        6.87317090e-01, 9.45615268e-01, 2.46309852e-05, 9.45651464e-01,
        9.32264917e-01, 6.69857664e-06, 9.45632915e-01, 8.71471094e-01,
        9.30486565e-01, 4.43209142e-17, 6.69569861e-10, 6.98022434e-01,
        9.45666459e-01, 9.28398039e-01, 9.45672706e-01, 9.45674887e-01,
        5.90207820e-02, 9.45673148e-01, 9.45686738e-01, 5.78888885e-07,
        8.42034573e-01, 9.38859548e-01, 9.45649024e-01, 9.45689256e-01,
        9.45695974e-01, 9.45613572e-01, 9.45656947e-01, 9.41332290e-01,
        8.42941780e-12, 1.05742675e-01, 9.14125454e-01, 9.45685037e-01,
        9.40095409e-01, 1.97342447e-04, 9.21883198e-01, 9.45649617e-01,
        9.35257083e-01, 9.44426194e-01, 2.16659883e-03, 8.96604488e-01,
        9.45643066e-01, 9.45637050e-01, 6.37889086e-02, 1.61200769e-13,
        9.18096790e-01, 2.79621312e-02, 7.90884445e-01, 9.32137191e-01,
        9.45629887e-01, 1.29377632e-07, 9.45600839e-01, 8.72024239e-05,
        9.45603613e-01, 9.45633849e-01, 6.23839711e-01, 4.66209286e-02,
        9.45660807e-01, 9.45653830e-01, 9.26894358e-01, 3.16397375e-01,
        9.28430910e-01, 9.45587771e-01, 8.88129177e-01, 9.32840965e-01,
        5.24829464e-01, 2.30935355e-15, 1.02748037e-06, 4.57351975e-01,
        9.33650334e-01, 9.45635276e-01, 7.63514461e-03, 8.05956232e-01,
        9.45707314e-01, 1.09200815e-01, 1.41277525e-05, 7.32179371e-04,
        9.41946163e-01, 9.03983941e-01, 8.98446497e-01, 9.19842153e-01,
        9.04203369e-01, 3.23073711e-09, 9.45691983e-01, 9.43876608e-01,
        6.63976285e-03, 9.45681374e-01, 9.45673118e-01, 9.45647281e-01,
        2.63228663e-01, 9.45677925e-01, 9.33530467e-01, 9.45596768e-01,
        6.52376314e-04, 9.08440763e-01, 9.45661916e-01, 8.90903905e-01,
        9.45683111e-01, 9.45648751e-01, 9.45683056e-01, 9.43939474e-01,
        9.45627676e-01, 5.04050271e-01, 3.84376326e-24, 5.40321009e-01,
        8.14430230e-04, 5.99859250e-08, 7.94949025e-04, 9.31189364e-01,
        9.45655008e-01, 8.52494320e-01, 9.00616110e-01, 3.43216704e-17,
        9.31304996e-01, 8.86837787e-01, 9.45672195e-01, 9.45595222e-01,
        7.68780995e-01, 9.34787081e-01, 9.08453546e-01, 9.45670391e-01,
        6.64897436e-09, 3.15490525e-02, 8.29551473e-01, 1.99822067e-02,
        7.70961159e-05, 9.45610751e-01, 4.85402004e-05, 9.45689770e-01,
        9.45626345e-01, 9.45671722e-01, 7.53275046e-11, 9.45675606e-01,
        4.73465293e-04, 9.45658388e-01, 8.33058204e-01, 9.45595544e-01,
        5.16896190e-10, 7.27487307e-04, 2.02785110e-06, 9.45660411e-01,
        9.25077834e-01, 9.45637840e-01, 8.98471305e-01, 1.61800384e-09,
        9.45661904e-01, 2.73545149e-02, 9.22488200e-01, 9.45679911e-01,
        9.45661785e-01, 9.45594869e-01, 8.93330134e-01, 9.25329475e-01,
        9.45088421e-01, 8.91837886e-01, 4.21449252e-09, 9.09128341e-01,
        9.05634568e-01, 7.60113706e-02, 9.42357509e-01, 1.25904539e-05,
        1.79833252e-02, 9.27799762e-01, 9.45604458e-01, 9.45610206e-01,
        9.45608920e-01, 9.45686022e-01, 9.43179779e-01, 4.27261045e-02,
        9.42003629e-01, 6.10790946e-16, 9.16579577e-01, 9.45668718e-01,
        9.45627536e-01, 9.45680149e-01, 9.45642160e-01, 8.52778286e-01,
        3.22146199e-03, 2.69268386e-13, 9.32642108e-01, 9.45598878e-01,
        9.17060787e-01, 9.12839341e-01, 9.45661490e-01, 9.45668435e-01,
        9.45575052e-01, 8.58908692e-01, 9.45643401e-01, 9.45667810e-01,
        9.45672919e-01, 9.45621659e-01, 9.45681553e-01, 9.28032306e-01,
        3.20108472e-01, 1.91734107e-05, 1.06817490e-01, 9.32357873e-01,
        9.38318475e-01, 1.27763976e-05, 7.73207415e-03, 9.42075774e-01,
        6.46691081e-01, 8.28889330e-01, 4.59561565e-04, 6.21127654e-03,
        8.99750970e-01, 9.45653516e-01, 9.06807970e-01, 8.80461928e-06,
        9.45665139e-01, 9.45676253e-01, 9.45670009e-01, 9.45661097e-01,
        9.39176381e-01, 9.45699588e-01, 1.47304887e-02, 7.26514183e-01,
        9.17418457e-01, 1.10966108e-05, 8.88441364e-01, 7.95263778e-01,
        9.45658585e-01, 8.72079770e-14, 2.90169326e-09, 6.80725428e-06,
        8.21648093e-06, 8.26159247e-01, 3.69600134e-01]])
In [58]:
# Adjust shape of prediction and true value
y_pred_train = y_pred_train.reshape(-1)
y_train = y_train.reshape(-1)
In [59]:
y_pred_train > 0.5
Out[59]:
array([ True,  True,  True,  True,  True,  True, False, False,  True,
        True, False,  True, False,  True, False, False,  True,  True,
        True,  True,  True, False,  True,  True,  True,  True,  True,
        True, False,  True, False, False, False,  True,  True, False,
        True, False,  True,  True,  True,  True,  True, False, False,
       False, False, False,  True,  True,  True,  True, False,  True,
       False,  True,  True, False,  True,  True,  True,  True,  True,
       False, False,  True,  True, False,  True,  True, False,  True,
        True,  True, False, False, False,  True, False,  True,  True,
       False,  True, False,  True, False, False,  True,  True, False,
        True,  True, False,  True, False,  True, False,  True,  True,
       False,  True, False,  True, False,  True,  True,  True,  True,
        True, False,  True,  True, False,  True,  True, False,  True,
        True,  True,  True,  True,  True,  True,  True, False,  True,
        True,  True, False,  True,  True, False,  True,  True, False,
        True,  True, False,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
       False,  True,  True,  True, False, False, False, False, False,
        True, False, False, False,  True,  True,  True,  True,  True,
        True, False,  True,  True, False, False,  True, False,  True,
       False,  True,  True,  True,  True, False,  True, False,  True,
        True,  True,  True,  True,  True, False,  True, False, False,
        True,  True, False,  True, False, False, False,  True,  True,
       False,  True,  True, False, False,  True,  True,  True,  True,
        True,  True,  True,  True,  True, False, False,  True, False,
        True,  True,  True,  True,  True, False,  True, False, False,
        True, False,  True,  True,  True,  True,  True, False, False,
       False,  True,  True,  True,  True,  True,  True, False,  True,
        True,  True,  True, False,  True,  True, False,  True,  True,
       False,  True,  True,  True, False, False,  True,  True,  True,
        True,  True, False,  True,  True, False,  True,  True,  True,
        True,  True,  True,  True,  True, False, False,  True,  True,
        True, False,  True,  True,  True,  True, False,  True,  True,
        True, False, False,  True, False,  True,  True,  True, False,
        True, False,  True,  True,  True, False,  True,  True,  True,
       False,  True,  True,  True,  True,  True, False, False, False,
        True,  True, False,  True,  True, False, False, False,  True,
        True,  True,  True,  True, False,  True,  True, False,  True,
        True,  True, False,  True,  True,  True, False,  True,  True,
        True,  True,  True,  True,  True,  True,  True, False,  True,
       False, False, False,  True,  True,  True,  True, False,  True,
        True,  True,  True,  True,  True,  True,  True, False, False,
        True, False, False,  True, False,  True,  True,  True, False,
        True, False,  True,  True,  True, False, False, False,  True,
        True,  True,  True, False,  True, False,  True,  True,  True,
        True,  True,  True,  True,  True, False,  True,  True, False,
        True, False, False,  True,  True,  True,  True,  True,  True,
       False,  True, False,  True,  True,  True,  True,  True,  True,
       False, False,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True, False, False,
       False,  True,  True, False, False,  True,  True,  True, False,
       False,  True,  True,  True, False,  True,  True,  True,  True,
        True,  True, False,  True,  True, False,  True,  True,  True,
       False, False, False, False,  True, False])
In [60]:
# Convert predictions to the binary class value
# (0 or 1, using as threshold 0.5)
y_pred_train = 1 * (y_pred_train > 0.5)
In [61]:
y_pred_train
Out[61]:
array([1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0,
       1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0,
       0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1,
       1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1,
       1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0,
       1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0,
       1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0,
       0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,
       1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0,
       0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
       1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
       1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0,
       1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0,
       0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
       1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1,
       1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1,
       1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1,
       1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0])
In [63]:
acc_train = sum(1 * (y_pred_train == y_train)) / len(y_pred_train) * 100
print("Accuracy with training data: " + str(acc_train))
AcurĂ¡cia with training data: 92.96066252587993
In [64]:
print(classification_report(y_train, y_pred_train, target_names = ['Malign', 'Benign']))
              precision    recall  f1-score   support

      Malign       0.97      0.84      0.90       178
      Benign       0.91      0.98      0.95       305

    accuracy                           0.93       483
   macro avg       0.94      0.91      0.92       483
weighted avg       0.93      0.93      0.93       483

In [66]:
# Predictions with test data
y_pred_test = predict(X_test, parameters)
y_pred_test
Out[66]:
array([[9.45679621e-01, 9.32862709e-01, 7.32943968e-03, 2.02822816e-08,
        4.40478262e-03, 1.03478793e-04, 6.09463820e-05, 9.45665645e-01,
        9.45662210e-01, 8.51463867e-01, 6.44837080e-01, 9.33128971e-01,
        9.45621619e-01, 9.45686627e-01, 9.45673741e-01, 1.56495043e-02,
        8.40978787e-01, 6.67483274e-01, 1.30734894e-05, 8.92175157e-01,
        9.45693190e-01, 3.79240095e-02, 6.11833102e-11, 9.45662604e-01,
        9.45657500e-01, 9.45629778e-01, 8.73860896e-01, 9.45645499e-01,
        4.64093678e-14, 4.91663965e-01, 2.96534913e-01, 5.98396196e-01,
        9.21411614e-01, 9.45677973e-01, 9.45635353e-01, 8.58003751e-08,
        4.27415069e-02, 9.27013592e-01, 9.45634292e-01, 9.45634436e-01,
        9.45679158e-01, 1.11537824e-07, 9.45658846e-01, 9.45628788e-01,
        9.09441287e-01, 9.44696648e-01, 5.77476864e-02, 9.31413889e-01,
        1.30412251e-01, 9.45652566e-01, 9.45655297e-01, 3.79887506e-05,
        8.74328901e-09, 9.45639397e-01, 2.74220496e-03, 1.10243005e-01,
        9.44729023e-01, 8.45207731e-01, 3.94172878e-01, 1.50883970e-01,
        9.45630000e-01, 9.45683261e-01, 8.57697755e-02, 1.13693217e-01,
        2.68639571e-07, 8.12546251e-01, 9.42640778e-01, 1.29042309e-01,
        8.82552857e-01, 8.52465830e-01, 7.49945743e-31, 9.28042291e-01,
        9.45691039e-01, 9.45673006e-01, 9.45600482e-01, 9.45660588e-01,
        4.75161328e-03, 1.91171735e-06, 9.45595313e-01, 9.45691501e-01,
        9.15008925e-01, 9.45643369e-01, 1.19366452e-01, 9.45690000e-01,
        1.44001731e-01, 8.86456800e-01]])
In [67]:
# Adjust shapes
y_pred_test = y_pred_test.reshape(-1)
y_test = y_test.reshape(-1)
In [70]:
# Convert to binary
y_pred_test = 1 * (y_pred_test > 0.5)
y_pred_test
Out[70]:
array([1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1,
       1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1,
       1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1])
In [71]:
acc_test = sum(1 * (y_pred_test == y_test)) / len(y_pred_test) * 100
print("Accuracy with test data: " + str(acc_test))
AcurĂ¡cia with test data: 97.67441860465115
In [72]:
print(classification_report(y_test, y_pred_test, target_names = ['Malign', 'Benign']))
              precision    recall  f1-score   support

      Malign       1.00      0.94      0.97        34
      Benign       0.96      1.00      0.98        52

    accuracy                           0.98        86
   macro avg       0.98      0.97      0.98        86
weighted avg       0.98      0.98      0.98        86

End