final-sp / mod.py
ayush0100's picture
Add mod.py and cache files
6ff5630
Raw History Blame Contribute Delete
1.32 kB
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle
import os
# --- Step 1: Create sample data ---
data = {
"Math": [55, 40, 70, 80, 30, 90, 60, 50, 45, 85],
"English": [60, 35, 75, 70, 25, 95, 55, 65, 50, 80],
"Science": [50, 45, 65, 85, 35, 90, 60, 55, 40, 75]
}
df = pd.DataFrame(data)
# Define pass/fail: pass if average >= 50
df['Pass'] = (df[['Math', 'English', 'Science']].mean(axis=1) >= 50).astype(int)
# --- Step 2: Split data ---
X = df[['Math', 'English', 'Science']]
y = df['Pass']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# --- Step 3: Train model ---
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# --- Step 4: Test model ---
y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
# --- Step 5: Save model to your folder ---
folder_path = r"C:\Users\singh\OneDrive\Desktop\my-ml-spcae"
os.makedirs(folder_path, exist_ok=True) # create folder if it doesn't exist
file_path = os.path.join(folder_path, "student_model.pkl")
with open(file_path, "wb") as f:
pickle.dump(model, f)
print(f"Model saved successfully at: {file_path}")