Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed
# 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions

Question

How do other differentiating characteristics factor into OB?

Answered: 1 week ago