Question: How can I modify this code so that it takes in two independent x variables for the logistic regression? That is, I would like to
How can I modify this code so that it takes in two independent x variables for the logistic regression? That is, I would like to call in another row of x's called "x2" and have them be included in the calculation.

# Importing libraries import numpy as np from math import exp # Divide the data to training set and test set X_train, X_test, y_train, y_test = train_test_split (df['xl' ], of['y' ], test_size=0. 20) # Method to make predictions def predict (X, b0, bl) : return np. array ([1 / (1 + exp (-1*b0 + -1*bl*x) ) for x in X]) # Method to train the model def logistic_regression (X, Y) : # Initializing variables b0 = 0 b1 = 0 L = 0. 001 epochs = 5000 for epoch in range (epochs) : y_pred = predict (X, b0, bl) D bo -2 * sum ( (Y - y_pred) * y_pred * (1 - y_pred) ) # Derivative of loss wrt b0 D bl = -2 * sum (X * (Y - y_pred) * y_pred * (1 - y_pred) ) # Derivative of loss wrt bl b0 = b0 - L * D b0 1 = b1 - L * D b1 return b0, b1 # Training the model b0, b1 = logistic_regression (X_train, y_train) # Making predictions y_pred predict (X_test, b0, b1) y_pred = [1 if p >= 0.5 else 0 for p in y_pred] # The accuracy accuracy = 0 for i in range (len (y_pred) ) : if y pred[i] = y_test. iloc[i]: accuracy += 1 print (f"Accuracy = {accuracy / len(y_pred) }") Accuracy = 0.85
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
