-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
127 lines (106 loc) · 4.27 KB
/
Copy pathscript.py
File metadata and controls
127 lines (106 loc) · 4.27 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Create synthetic training data for student dropout prediction
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.preprocessing import StandardScaler
import random
# Set random seed for reproducibility
np.random.seed(42)
random.seed(42)
# Generate synthetic student data
n_students = 1000
# Generate student features
data = {
'student_id': range(1, n_students + 1),
'age': np.random.randint(14, 19, n_students),
'gender': np.random.choice(['M', 'F'], n_students),
'socioeconomic_status': np.random.choice(['Low', 'Medium', 'High'], n_students, p=[0.3, 0.5, 0.2]),
# Academic performance (grades 0-100)
'math_grade': np.random.normal(75, 15, n_students),
'english_grade': np.random.normal(78, 12, n_students),
'science_grade': np.random.normal(76, 14, n_students),
'previous_gpa': np.random.normal(3.0, 0.8, n_students),
# Attendance (percentage)
'attendance_rate': np.random.normal(85, 20, n_students),
'days_absent': np.random.poisson(15, n_students),
# Behavioral factors
'disciplinary_actions': np.random.poisson(2, n_students),
'participation_score': np.random.normal(7, 2, n_students),
'homework_completion': np.random.normal(80, 25, n_students),
# Family factors
'parent_education': np.random.choice(['Elementary', 'High_School', 'College', 'Graduate'], n_students, p=[0.15, 0.4, 0.35, 0.1]),
'family_support': np.random.randint(1, 11, n_students), # Scale 1-10
# School engagement
'extracurricular_activities': np.random.randint(0, 4, n_students),
'teacher_relationship': np.random.normal(7, 1.5, n_students),
}
# Create DataFrame
df = pd.DataFrame(data)
# Clip values to reasonable ranges
df['math_grade'] = np.clip(df['math_grade'], 0, 100)
df['english_grade'] = np.clip(df['english_grade'], 0, 100)
df['science_grade'] = np.clip(df['science_grade'], 0, 100)
df['previous_gpa'] = np.clip(df['previous_gpa'], 0, 4.0)
df['attendance_rate'] = np.clip(df['attendance_rate'], 0, 100)
df['participation_score'] = np.clip(df['participation_score'], 0, 10)
df['homework_completion'] = np.clip(df['homework_completion'], 0, 100)
df['teacher_relationship'] = np.clip(df['teacher_relationship'], 1, 10)
# Create dropout risk based on realistic factors
def calculate_dropout_risk(row):
risk_score = 0
# Academic performance factors
avg_grade = (row['math_grade'] + row['english_grade'] + row['science_grade']) / 3
if avg_grade < 60:
risk_score += 3
elif avg_grade < 70:
risk_score += 2
elif avg_grade < 80:
risk_score += 1
# Attendance factors
if row['attendance_rate'] < 70:
risk_score += 3
elif row['attendance_rate'] < 80:
risk_score += 2
elif row['attendance_rate'] < 90:
risk_score += 1
# Behavioral factors
if row['disciplinary_actions'] > 5:
risk_score += 2
elif row['disciplinary_actions'] > 2:
risk_score += 1
# Family and social factors
if row['socioeconomic_status'] == 'Low':
risk_score += 1
if row['family_support'] < 4:
risk_score += 2
elif row['family_support'] < 6:
risk_score += 1
# Engagement factors
if row['participation_score'] < 5:
risk_score += 1
if row['homework_completion'] < 60:
risk_score += 2
elif row['homework_completion'] < 80:
risk_score += 1
# Convert risk score to dropout probability
if risk_score >= 8:
return 'High Risk'
elif risk_score >= 5:
return 'Medium Risk'
else:
return 'Low Risk'
# Apply the dropout risk calculation
df['dropout_risk'] = df.apply(calculate_dropout_risk, axis=1)
# Create binary dropout column for machine learning
df['dropout'] = (df['dropout_risk'] == 'High Risk').astype(int)
print("Student Dropout Prediction Dataset Created")
print(f"Dataset shape: {df.shape}")
print("\nDropout Risk Distribution:")
print(df['dropout_risk'].value_counts())
print(f"\nActual dropout rate: {df['dropout'].mean()*100:.1f}%")
# Display first few rows
print("\nFirst 5 students:")
print(df.head())