Predicting Student Performance with Machine Learning
Predicting Student Performance with Machine Learning
This tutorial walks you through building a classification model to predict whether students pass or fail based on study hours, attendance, and assignment scores. We’ll use Python and scikit-learn.
1. Training Dataset
Here’s the sample dataset we’ll use:
import pandas as pd
data = {
'StudyHours': [1, 2, 3, 4, 5, 6, 7, 8],
'Attendance': [60, 65, 70, 75, 80, 85, 90, 95],
'Assignments': [50, 55, 60, 65, 70, 75, 80, 85],
'Passed': [0, 0, 0, 1, 1, 1, 1, 1]
}
df = pd.DataFrame(data)
print(df)
2. Feature Selection and Model Training
We’ll use Decision Tree Classifier to train the model:
from sklearn.tree import DecisionTreeClassifier
X = df[['StudyHours', 'Attendance', 'Assignments']]
y = df['Passed']
model = DecisionTreeClassifier()
model.fit(X, y)
3. Making Predictions
Let’s predict if a student with 5 study hours, 80% attendance, and 70 assignment score will pass:
prediction = model.predict([[5, 80, 70]])
print("Prediction:", "Pass" if prediction[0] == 1 else "Fail")
4. Model Evaluation
We’ll use accuracy score to evaluate the model:
from sklearn.metrics import accuracy_score
y_pred = model.predict(X)
print("Accuracy:", accuracy_score(y, y_pred))
5. Visualizing the Decision Tree
Visualize how the model makes decisions:
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt
plt.figure(figsize=(10,6))
plot_tree(model, feature_names=['StudyHours', 'Attendance', 'Assignments'], class_names=['Fail', 'Pass'], filled=True)
plt.show()
6. Conclusion
We’ve built a classification model using a realistic dataset to predict student outcomes. This approach can be extended to larger datasets and more features like test scores, participation, and more.


Comments
Post a Comment