Introduction
In this tutorial, we'll build an interactive machine learning web app to predict whether a student will be accepted into a master's program. We'll use:
- Streamlit for creating the web interface.
- scikit-learn for training the machine learning model.
- joblib for saving and loading the model.
What You’ll Learn
- Loading and preprocessing data.
- Training and saving a machine learning model.
- Building a Streamlit app for real-time predictions.
- Deploying the app online.
Setting Up the Environment
Install Dependencies
pip install streamlit scikit-learn pandas joblib numpy
Training the Machine Learning Model
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib
# Sample dataset
data = {
"GRE_Score": [300, 320, 310, 315, 305, 330, 290, 325, 335, 310],
"TOEFL_Score": [100, 110, 105, 108, 101, 115, 95, 112, 118, 104],
"GPA": [3.0, 3.5, 3.2, 3.4, 3.1, 3.9, 2.8, 3.8, 4.0, 3.3],
"Work_Experience": [0, 1, 2, 2, 1, 3, 0, 4, 5, 1],
"Research": [1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
"Accepted": [0, 1, 0, 1, 0, 1, 0, 1, 1, 0]
}
# Create DataFrame
df = pd.DataFrame(data)
# Split data into features and target
X = df[["GRE_Score", "TOEFL_Score", "GPA", "Work_Experience", "Research"]]
y = df["Accepted"]
# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train a Random Forest model
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
# Evaluate the model
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Model Accuracy:", accuracy)
# Save the model
joblib.dump(model, "trainedModel.pkl")
Run the script
python myModel.py
Creating the Streamlit App
Create the app.py file:
import streamlit as st
import numpy as np
import joblib
# Load the trained model
model = joblib.load("trainedModel.pkl")
# Streamlit app title
st.title("🎓 Student Acceptance Prediction App")
# App description
st.markdown("""
This app predicts whether a student is likely to be accepted into a master's program based on their academic and professional credentials.
Provide the details below to get a prediction.
""")
# Input fields
gre_score = st.slider("GRE Score", min_value=260, max_value=340, value=300, step=1)
toefl_score = st.slider("TOEFL Score", min_value=0, max_value=120, value=100, step=1)
gpa = st.slider("Undergraduate GPA", min_value=0.0, max_value=4.0, value=3.0, step=0.1)
work_exp = st.slider("Work Experience (Years)", min_value=0, max_value=10, value=2, step=1)
research = st.radio("Research Experience", options=["Yes", "No"])
# Convert research input to binary
research_binary = 1 if research == "Yes" else 0
# Prepare the input data
input_data = np.array([[gre_score, toefl_score, gpa, work_exp, research_binary]])
# Predict button
if st.button("Predict"):
result = model.predict(input_data)
probability = model.predict_proba(input_data)
if result[0] == 1:
st.success("🎉 The student is likely to be accepted!")
else:
st.error("❌ The student is unlikely to be accepted.")
st.write(f"**Probability of Acceptance:** {probability[0][1]:.2f}")
Finally run the Streamlit App
streamlit run app.py
Outcome
By the end of this tutorial, you’ll have:
- A working ML model for student acceptance prediction.
- An interactive web app built with Streamlit.
Top comments (0)