<jupyter_start><jupyter_text>Logistic Regression  import libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn.model_selection import train_test_split<jupyter_output><empty_output><jupyter_text>Reading Dataset<jupyter_code>dataset = pd.read_csv('heart.csv')
dataset = dataset.sample(frac=1).reset_index(drop = True) ##shuffing the dataframe
dataset
x = dataset[dataset.columns[:-1]].to_numpy() #independent variables
y = dataset[dataset.columns[-1]].to_numpy() #dependent variables
x.shape
y = y[:,np.newaxis]
y.shape<jupyter_output><empty_output><jupyter_text>Splitting Dataset<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2) #splitting the data in 80:20
print("Shape of \nX_train: {} \nX_test: {} \nY_train: {} \nY_test: {}".format(x_train.shape,x_test.shape,y_train.shape,y_test.shape))<jupyter_output>Shape of 
X_train: (242, 13) 
X_test: (61, 13) 
Y_train: (242, 1) 
Y_test: (61, 1)<jupyter_text>Sigmoid Function<jupyter_code>def sigmoid(z): #sigmoid function required for logistic regression
    return 1/(1+np.exp(-z))<jupyter_output><empty_output><jupyter_text>Weight and Bias Initialization<jupyter_code>def init_weight_bias(dim): #initialize the weights and biases according to the required dimensions
    wt = np.zeros((dim,1))
    b = 0
    return wt,b<jupyter_output><empty_output><jupyter_text>Forward Propagation<jupyter_code>def forward(x,y,w,b): #calculate the gradients and the cost on forward propagation
    m = x.shape[0]
    a = sigmoid(np.dot(x,w)+b)
    cost = -(1 / m) * np.sum(y * np.log(a) + (1 - y) * np.log(1 - a)) #computing the cost function or the error function
    dw = (1 / m) * np.dot(x.T, (a - y))   #this is derivative of the cost function with respect to w
    db = (1 / m) * np.sum(a - y)          #this is the derivative of the cost function with respect to b
    grads = {'dw': dw, 'db': db}          #these values are stored in a dictionary so as to access them later
    return grads, cost<jupyter_output><empty_output><jupyter_text>Opitmizer<jupyter_code>def optimize(x,y,w,b,iteration,lr,show_cost = True): #optimize the weights and biases
    costs = []
    for i in range(iteration):
        grads,cost = forward(x,y,w,b)
        dw = grads['dw']
        db = grads['db']
        w -= lr * dw
        b -= lr * db
        np.squeeze(cost)
        if i%500 == 0:
            costs.append(cost)
            if show_cost:
                print("cost after {} iterations :{}".format(i,cost))
    plt.plot(np.squeeze(costs),color='orange')
    plt.ylabel('cost')
    plt.xlabel('iterations (per tens)')
    plt.title("Learning rate = " + str(lr))
    plt.show()
    params = {'w': w, 'b': db}                    #storing value in the dictionary for future access
    grads = {'dw': dw, 'db': db}                  #storing value in the dictionary for future access
    return params, grads, costs<jupyter_output><empty_output><jupyter_text>Prediction FUnction<jupyter_code>def predict(w, b, x):
    m = x.shape[0]
    y_prediction = np.zeros((m, 1))
    A = sigmoid(np.dot(x, w) + b)
    for i in range(A.shape[0]):
        if (A[i, 0] <= 0.5):
            y_prediction[i, 0] = 0
        else:
            y_prediction[i, 0] = 1
            
    return y_prediction<jupyter_output><empty_output><jupyter_text>Summing up as model<jupyter_code>def logistic_regression(X_train, X_test, y_train, y_test, num_iterations, learning_rate, print_cost = True):
    w, b = init_weight_bias(X_train.shape[1])
    parameters, grads, costs = optimize(X_train, y_train,w,b, num_iterations, learning_rate)
    w = parameters["w"]
    b = parameters["b"]
    y_prediction_test = predict(w, b, X_test)
    y_prediction_train = predict(w, b, X_train)
    
    print('train accuracy: {}'.format(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100))
    print('test accuracy: {}'.format("test accuracy: {} %".format(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100)))
    
    d = {"costs": costs,
         "y_prediction_test": y_prediction_test, 
         "y_prediction_train" : y_prediction_train, 
         "w" : w, 
         "b" : b,
         "learning_rate" : learning_rate,
         "num_iterations": num_iterations}
    
    return d
log_Reg = logistic_regression(x_train,x_test,y_train,y_test,num_iterations=100000,learning_rate=0.00015,print_cost=True)<jupyter_output>cost after 0 iterations :0.6931471805599453
cost after 500 iterations :0.9679736052485857
cost after 1000 iterations :0.9568059378919556
cost after 1500 iterations :0.9499027686133058
cost after 2000 iterations :0.9432626317335868
cost after 2500 iterations :0.9367641802972672
cost after 3000 iterations :0.9303584574983875
cost after 3500 iterations :0.9240201500923151
cost after 4000 iterations :0.917736057447954
cost after 4500 iterations :0.9114994171538602
cost after 5000 iterations :0.9053069293476338
cost after 5500 iterations :0.8991571611557575
cost after 6000 iterations :0.8930496822234703
cost after 6500 iterations :0.8869845935340716
cost after 7000 iterations :0.8809622694550685
cost after 7500 iterations :0.8749832159737406
cost after 8000 iterations :0.8690479925233784
cost after 8500 iterations :0.8631571687802468
cost after 9000 iterations :0.8573113008079657
cost after 9500 iterations :0.8515109179910388
cost after 10000 iterations :0.8457565160484669
cost after 10500 [...]<jupyter_text>Overlap of Test data Labels and Prediction Labels<jupyter_code>plt.scatter(range(len(y_test)),y_test,label='true label',color='crimson')
plt.scatter(range(len(log_Reg["y_prediction_test"])),log_Reg["y_prediction_test"],label='predictions',color='gray')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12027850304612707, 'fraction_numerical': 0.0898172323759791, 'mean_word_length': 4.310536044362292, 'pattern_counts': {'":': 3, '<': 40, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '155575', 'license': 'MIT', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2057, 'n_words': 478, 'path': 'Assignment_5/Assignment_5.2.ipynb', 'repo_name': 'Soumya296/Data_Science_Assignments', 'size': '5745'}