-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.py
More file actions
74 lines (60 loc) · 2.16 KB
/
Algorithm.py
File metadata and controls
74 lines (60 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# suggestion_system_small_data.py
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from lightgbm import LGBMClassifier
from sklearn.metrics import accuracy_score
import joblib
# --------------------
# 1. Load dataset
# --------------------
df = pd.read_csv("RemedyDataClean.csv") # Make sure file has q1...q20 + Enrol
X = df.drop("Enrol", axis=1)
y = df["Enrol"]
# --------------------
# 2. Train/Test split (90/10 because dataset is small)
# --------------------
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.1, random_state=42, stratify=y
)
# --------------------
# 3. Define models
# --------------------
models = {
"Logistic Regression": LogisticRegression(max_iter=500),
"Random Forest": RandomForestClassifier(n_estimators=150, max_depth=6, random_state=42),
"LightGBM": LGBMClassifier(n_estimators=150, learning_rate=0.05, random_state=42)
}
# --------------------
# 4. Train & Evaluate
# --------------------
results = {}
for name, model in models.items():
# Cross-validation on training data (5 folds)
cv_scores = cross_val_score(model, X_train, y_train, cv=5, scoring="accuracy")
avg_cv = np.mean(cv_scores)
# Train on training set
model.fit(X_train, y_train)
# Evaluate on test set
y_pred = model.predict(X_test)
test_acc = accuracy_score(y_test, y_pred)
results[name] = {
"CV Accuracy": avg_cv,
"Test Accuracy": test_acc,
"Model": model
}
print(f"{name} → CV: {avg_cv:.4f}, Test: {test_acc:.4f}")
# --------------------
# 5. Pick best model
# --------------------
best_model_name = max(results, key=lambda x: results[x]["Test Accuracy"])
best_model = results[best_model_name]["Model"]
print("\nBest Model:", best_model_name)
print("Test Accuracy:", results[best_model_name]["Test Accuracy"])
# --------------------
# 6. Save best model
# --------------------
joblib.dump(best_model, "RemedyModel.pkl")
print("RemedyModel.pkl")