Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how we can solve this Binary Classification using Multi Variant Logistic Regression.

+2 votes
asked Feb 4, 2020 by Herry794 (140 points)
i need the solution without using  the built in model of Linear Regression in python lib. kindly calculate the problem only by using the cost function formula and step by step program for each calculation

Evaluate the model with confusion Matrix.

1 Answer

+1 vote
answered Aug 6, 2024 by Vallabhi Vithlani (200 points)
import numpy as np

# Initialize model parameters
theta0 = 0.5
theta1 = 0.2

# Define the learning rate and number of iterations
alpha = 0.01
num_iterations = 1000

# Define the dataset
hours_studied = np.array([2, 4, 6, 8, 10])
scores = np.array([90, 85, 95, 88, 92])

# Repeat steps 3-6 for the specified number of iterations
for i in range(num_iterations):
    predicted_scores = theta0 + theta1 * hours_studied
    error = predicted_scores - scores
    gradient_theta0 = (1 / len(hours_studied)) * np.sum(error)
    gradient_theta1 = (1 / len(hours_studied)) * np.sum(error * hours_studied)
    theta0 -= alpha * gradient_theta0
    theta1 -= alpha * gradient_theta1

# Evaluate the model using a confusion matrix
predicted_scores = theta0 + theta1 * hours_studied
predicted_scores = np.round(predicted_scores)

# Create a binary classification problem
binary_scores = np.where(scores > 90, 1, 0)
binary_predicted_scores = np.where(predicted_scores > 90, 1, 0)

confusion_matrix = np.array([[0, 0], [0, 0]])

for i in range(len(hours_studied)):
    if binary_predicted_scores[i] == binary_scores[i]:
        if binary_scores[i] == 1:
            confusion_matrix[0, 0] += 1
        else:
            confusion_matrix[1, 1] += 1
    else:
        if binary_scores[i] == 1:
            confusion_matrix[0, 1] += 1
        else:
            confusion_matrix[1, 0] += 1

print("Final Model Parameters:")
print("theta0:", theta0)
print("theta1:", theta1)

print("\nConfusion Matrix:")
print(confusion_matrix)

print("\nAccuracy:", (confusion_matrix[0, 0] + confusion_matrix[1, 1]) / len(hours_studied))
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...