text_plain_output_1.png
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('../input/data.csv')

data.drop(['id', 'Unnamed: 32'], axis=1, inplace=True)
x_data = data.drop(['diagnosis'], axis=1)

data.diagnosis = [1 if each == 'M' else 0 for each in data.diagnosis]

x = ((x_data - np.min(x_data)) / (np.max(x_data) - np.min(x_data))).values
y = data.diagnosis.values

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
x_train = x_train.T
x_test = x_test.T
y_train = y_train.T
y_test = y_test.T

def initialize_parameters(dimension):
    theta0 = 0.0
    thetaLeft = np.full((dimension, 1), 0.01)
    return (theta0, thetaLeft)

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def forward_backward_propagation(theta0, thetaLeft, x_train, y_train):
    z = theta0 + np.dot(thetaLeft.T, x_train)
    y_head = sigmoid(z)
    cost_function = -y_train * np.log(y_head) - (1 - y_head) * np.log(1 - y_head)
    cost = np.sum(cost_function) / x_train.shape[1]
    derivative_theta0 = np.sum(y_head - y_train) / x_train.shape[1]
    derivative_thetaLeft = np.dot(x_train, (y_head - y_train).T) / x_train.shape[1]
    gradients = {'derivative_theta0': derivative_theta0, 'derivative_thetaLeft': derivative_thetaLeft}
    return (cost, gradients)

def update(theta0, thetaLeft, x_train, y_train, learning_rate, number_of_iteration):
    cost_list = []
    cost_list_to_plot = []
    index_to_plot = []
    for i in range(number_of_iteration):
        cost, gradients = forward_backward_propagation(theta0, thetaLeft, x_train, y_train)
        cost_list.append(cost)
        theta0 = theta0 - learning_rate * gradients['derivative_theta0']
        thetaLeft = thetaLeft - learning_rate * gradients['derivative_thetaLeft']
        if i % 50 == 0:
            cost_list_to_plot.append(cost)
            index_to_plot.append(i)
    parameters = {'theta0': theta0, 'thetaLeft': thetaLeft}
    plt.xticks(index_to_plot, rotation='vertical')
    return (parameters, gradients, cost_list)

def predict(theta0, thetaLeft, x_test):
    z = sigmoid(theta0 + np.dot(thetaLeft.T, x_test))
    Y_prediction = np.zeros((1, x_test.shape[1]))
    for i in range(z.shape[1]):
        if z[0, i] <= 0.5:
            Y_prediction[0, i] = 0
        else:
            Y_prediction[0, i] = 1
    return Y_prediction

def logistic_regression(x_train, y_train, x_test, y_test, learning_rate, number_of_iteration):
    dimension = x_train.shape[0]
    theta0, thetaLeft = initialize_parameters(dimension)
    parameters, gradients, cost_list = update(theta0, thetaLeft, x_train, y_train, learning_rate, number_of_iteration)
    y_prediction_test = predict(parameters['theta0'], parameters['thetaLeft'], x_test)
    y_prediction_train = predict(parameters['theta0'], parameters['thetaLeft'], x_train)
    cost_list_global_train.append(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100)
    cost_list_global_test.append(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100)

logistic_regression(x_train, y_train, x_test, y_test, learning_rate=0.01, number_of_iteration=500)