-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript 2.py
More file actions
77 lines (61 loc) · 2.47 KB
/
Copy pathscript 2.py
File metadata and controls
77 lines (61 loc) · 2.47 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
75
76
77
# Prepare data for machine learning
from sklearn.preprocessing import LabelEncoder
# Create a copy for ML processing
ml_data = df.copy()
# Encode categorical variables
le_gender = LabelEncoder()
le_ses = LabelEncoder()
le_parent_ed = LabelEncoder()
ml_data['gender_encoded'] = le_gender.fit_transform(ml_data['gender'])
ml_data['ses_encoded'] = le_ses.fit_transform(ml_data['socioeconomic_status'])
ml_data['parent_education_encoded'] = le_parent_ed.fit_transform(ml_data['parent_education'])
# Select features for ML model
feature_columns = [
'age', 'gender_encoded', 'ses_encoded', 'math_grade', 'english_grade',
'science_grade', 'previous_gpa', 'attendance_rate', 'days_absent',
'disciplinary_actions', 'participation_score', 'homework_completion',
'parent_education_encoded', 'family_support', 'extracurricular_activities',
'teacher_relationship'
]
X = ml_data[feature_columns]
y = ml_data['dropout']
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
# Scale the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train multiple models
models = {
'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
'Logistic Regression': LogisticRegression(random_state=42, max_iter=1000)
}
model_results = {}
for name, model in models.items():
if name == 'Logistic Regression':
model.fit(X_train_scaled, y_train)
predictions = model.predict(X_test_scaled)
else:
model.fit(X_train, y_train)
predictions = model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
model_results[name] = {
'accuracy': accuracy,
'predictions': predictions,
'model': model
}
print(f"\n{name} Results:")
print(f"Accuracy: {accuracy:.3f}")
print("Classification Report:")
print(classification_report(y_test, predictions))
# Select best model (Random Forest performed better)
best_model = models['Random Forest']
best_scaler = None # Random Forest doesn't need scaling
# Save feature names for the web app
feature_names = feature_columns
print(f"\nFeature Importance (Random Forest):")
importances = best_model.feature_importances_
feature_importance = list(zip(feature_names, importances))
feature_importance.sort(key=lambda x: x[1], reverse=True)
for feature, importance in feature_importance:
print(f"{feature}: {importance:.3f}")