Python Lab10b
Python Lab10b
NO: 10 B)
71762305009 Scikit-Learn
26.03.2025
AIM:
To learn and implement python programs using Scikit -Learn and applying
Logistic Regression for Classification.
PROGRAM:
A school wants to predict whether students will pass or fail their final exams
based on the number of hours they study per week. They have collected data on
students' study hours and their final result (Pass = 1, Fail = 0). Your task is to
build a simple machine learning model using Scikit-Learn to make predictions.
CODE:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
data = {
"Study Hours": [1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5],
"Result": [0, 0, 0, 1, 1, 1, 1]
}
df = pd.DataFrame(data)
model = LogisticRegression()
model.fit(X, y)
y_pred = model.predict(X)
accuracy = accuracy_score(y, y_pred)
print(f"Model Accuracy: {accuracy * 100:.2f}%")
cm = confusion_matrix(y, y_pred)
print("Confusion Matrix:\n", cm)
X_range = np.linspace(1, 11, 100).reshape(-1, 1) # Generating values for
visualization
y_prob = model.predict_proba(X_range)[:, 1] # Get probabilities
plt.scatter(X, y, color='blue', label="Actual Data")
plt.plot(X_range, y_prob, color='red', label="Logistic Regression Curve")
plt.xlabel("Study Hours")
plt.ylabel("Probability of Passing")
plt.title("Logistic Regression - Study Hours vs. Pass Probability")
plt.legend()
plt.show()
OUTPUT:
RESULT:
The python program using scikit-learn has been successfully implemented and
verified.