0% found this document useful (0 votes)
17 views4 pages

Logistic Regression for Student Pass Prediction

The document outlines a practical implementation of a Logistic Regression model using Python to predict student pass/fail status based on their average scores. It includes steps for data loading, feature engineering, model training, evaluation, and making predictions for a specific student. The code also demonstrates how to handle input and output for student enrollment numbers and their corresponding results.

Uploaded by

itsextra789
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Logistic Regression for Student Pass Prediction

The document outlines a practical implementation of a Logistic Regression model using Python to predict student pass/fail status based on their average scores. It includes steps for data loading, feature engineering, model training, evaluation, and making predictions for a specific student. The code also demonstrates how to handle input and output for student enrollment numbers and their corresponding results.

Uploaded by

itsextra789
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical:- 9 – Logistic Regression

Python code:-
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from [Link] import accuracy_score, classification_report
from [Link] import StandardScaler
import numpy as np
import [Link] as plt

# =================================================================
# LOGISTIC REGRESSION MODEL SETUP
# =================================================================

# Load the dataset


df = pd.read_csv("student_dataset.csv")

# 1. Feature Engineering: Create the 'AverageScore'


# Identify subject score columns (assuming they start from index 5 to the end)
subject_cols = [Link][5:].tolist()
passing_threshold = 60

df['AverageScore'] = df[subject_cols].mean(axis=1)

# 2. Create the Binary Target Variable 'Result' (1 for Pass, 0 for Fail)
# A student is 'Pass' if their Average Score is 60 or above.
df['Result'] = (df['AverageScore'] >= passing_threshold).astype(int)
# 3. Select Features (X) and Target (y)
X = df[['AverageScore']]
y = df['Result']

# 4. Preprocess Data: Scale the feature


# Scaling is crucial for Logistic Regression performance
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 5. Split the data


X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=0.3, random_state=42, stratify=y
)

# 6. Train the Logistic Regression Model


model = LogisticRegression(random_state=42)
[Link](X_train, y_train)

# 7. Evaluate the model


y_pred = [Link](X_test)
accuracy = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred)

print("="*60)
print("LOGISTIC REGRESSION MODEL TRAINING AND EVALUATION")
print("="*60)
print(f"Model Accuracy on Test Set: {accuracy:.4f}")
print("\nClassification Report:")
print(report)
print(f"Decision Boundary (Average Score): {scaler.inverse_transform([[model.intercept_[0] / -
model.coef_[0][0]]])[0][0]:.2f}")
print("="*60)

# =================================================================
# PREDICT STUDENT BY ENROLLMENT NUMBER
# =================================================================

# 1. INPUT THE STUDENT'S ENROLLMENT NUMBER HERE:


input_enrollment_no = "EN0020" # Example: Cynthia Chang

# 2. Look up the student's data using the EnrollmentNo


student_data = df[df['EnrollmentNo'].[Link]() == input_enrollment_no.upper()]

print("\n" + "*-"*30)

if student_data.empty:
print(f"Error: Enrollment Number '{input_enrollment_no}' not found in the dataset.")
else:
# 3. Extract and Scale the Average Score for prediction
student_score = student_data[['AverageScore']]
test_score_scaled = [Link](student_score)

# 4. Predict the probability and the class


proba_pass = model.predict_proba(test_score_scaled)[0][1]
prediction = [Link](test_score_scaled)[0]
# Map the numerical prediction to a clear status
result_map = {1: 'Pass', 0: 'Fail'}
predicted_status = result_map[prediction]

# 5. Print the prediction details


print(f"PREDICTION FOR ENROLLMENT NO: {input_enrollment_no.upper()}")
print("*-"*30)
print(f"Student Name: {student_data['StudentName'].iloc[0]}")
print(f"Calculated Average Score: {student_score.iloc[0]['AverageScore']:.2f}")
print(f"Predicted Probability of Pass: {proba_pass:.5f}")
print(f"Predicted Final Status: {predicted_status}")

print("*-"*30)

You might also like