-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
156 lines (126 loc) · 5.96 KB
/
analysis.py
File metadata and controls
156 lines (126 loc) · 5.96 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Student Performance Analysis
# Simple data analysis project for beginners
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Load the dataset
print("Loading student data...")
df = pd.read_csv('student_performance.csv')
print("\n" + "="*50)
print("STEP 1: DATA CLEANING")
print("="*50)
# Check the data
print(f"\nTotal students: {len(df)}")
print(f"\nColumns in dataset: {list(df.columns)}")
# Check for missing values
print("\nMissing values in each column:")
print(df.isnull().sum())
# Clean the data - remove rows with missing final_score
print("\nRemoving rows with missing final scores...")
df_clean = df.dropna(subset=['final_score'])
# Fill missing values in other columns with median
print("Filling missing values with median...")
for col in df_clean.columns:
if df_clean[col].isnull().any():
df_clean[col].fillna(df_clean[col].median(), inplace=True)
# Remove any negative or invalid values
print("Removing invalid data (negative values)...")
df_clean = df_clean[(df_clean['internet_usage'] >= 0) &
(df_clean['sleep_hours'] >= 0) &
(df_clean['final_score'] >= 0) &
(df_clean['final_score'] <= 100)]
print(f"\nCleaned dataset: {len(df_clean)} students")
# Save cleaned data
df_clean.to_csv('student_performance_cleaned.csv', index=False)
print("Cleaned data saved!")
print("\n" + "="*50)
print("STEP 2: DESCRIPTIVE ANALYSIS")
print("="*50)
# Calculate statistics
avg_score = df_clean['final_score'].mean()
max_score = df_clean['final_score'].max()
min_score = df_clean['final_score'].min()
avg_study_hours = df_clean['hours_studied'].mean()
# Pass/Fail count (assuming 60 is passing grade)
df_clean['pass_fail'] = df_clean['final_score'].apply(lambda x: 'Pass' if x >= 60 else 'Fail')
pass_count = (df_clean['pass_fail'] == 'Pass').sum()
fail_count = (df_clean['pass_fail'] == 'Fail').sum()
print(f"\nKey Statistics:")
print(f" Average Exam Score: {avg_score:.2f}")
print(f" Highest Score: {max_score:.2f}")
print(f" Lowest Score: {min_score:.2f}")
print(f" Average Study Hours: {avg_study_hours:.2f} hours/day")
print(f"\n Pass Count: {pass_count} students")
print(f" Fail Count: {fail_count} students")
print(f" Pass Rate: {(pass_count/len(df_clean)*100):.1f}%")
print("\n" + "="*50)
print("STEP 3: DATA VISUALIZATION")
print("="*50)
# Create figure with 4 subplots
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
fig.suptitle('Student Performance Analysis', fontsize=16, fontweight='bold')
# 1. Study Hours vs Exam Score (Scatter Plot)
axes[0, 0].scatter(df_clean['hours_studied'], df_clean['final_score'],
alpha=0.5, color='blue', edgecolors='black')
axes[0, 0].set_xlabel('Study Hours per Day')
axes[0, 0].set_ylabel('Final Score')
axes[0, 0].set_title('Study Hours vs Exam Score')
axes[0, 0].grid(True, alpha=0.3)
# 2. Pass vs Fail (Bar Chart)
pass_fail_counts = df_clean['pass_fail'].value_counts()
colors = ['green', 'red']
axes[0, 1].bar(pass_fail_counts.index, pass_fail_counts.values, color=colors, alpha=0.7)
axes[0, 1].set_xlabel('Result')
axes[0, 1].set_ylabel('Number of Students')
axes[0, 1].set_title('Pass vs Fail Distribution')
for i, v in enumerate(pass_fail_counts.values):
axes[0, 1].text(i, v + 2, str(v), ha='center', fontweight='bold')
# 3. Attendance vs Score (Scatter Plot)
axes[1, 0].scatter(df_clean['attendance'], df_clean['final_score'],
alpha=0.5, color='orange', edgecolors='black')
axes[1, 0].set_xlabel('Attendance (%)')
axes[1, 0].set_ylabel('Final Score')
axes[1, 0].set_title('Attendance vs Score')
axes[1, 0].grid(True, alpha=0.3)
# 4. Score Distribution (Histogram)
axes[1, 1].hist(df_clean['final_score'], bins=20, color='purple', alpha=0.7, edgecolor='black')
axes[1, 1].set_xlabel('Final Score')
axes[1, 1].set_ylabel('Number of Students')
axes[1, 1].set_title('Score Distribution')
axes[1, 1].axvline(avg_score, color='red', linestyle='--', linewidth=2, label=f'Average: {avg_score:.1f}')
axes[1, 1].legend()
plt.tight_layout()
plt.savefig('student_analysis_charts.png', dpi=300, bbox_inches='tight')
print("\nCharts saved as 'student_analysis_charts.png'")
print("\n" + "="*50)
print("STEP 4: INSIGHTS & FINDINGS")
print("="*50)
# Calculate insights
high_study = df_clean[df_clean['hours_studied'] > 3]['final_score'].mean()
low_study = df_clean[df_clean['hours_studied'] <= 3]['final_score'].mean()
high_attendance = df_clean[df_clean['attendance'] > 75]['final_score'].mean()
low_attendance = df_clean[df_clean['attendance'] <= 75]['final_score'].mean()
high_attendance_pass_rate = (df_clean[df_clean['attendance'] > 75]['pass_fail'] == 'Pass').sum() / len(df_clean[df_clean['attendance'] > 75]) * 100
low_attendance_pass_rate = (df_clean[df_clean['attendance'] <= 75]['pass_fail'] == 'Pass').sum() / len(df_clean[df_clean['attendance'] <= 75]) * 100
print("\nKey Insights:")
print(f"\n1. Study Time Impact:")
print(f" - Students studying >3 hours/day: Average score = {high_study:.2f}")
print(f" - Students studying ≤3 hours/day: Average score = {low_study:.2f}")
print(f" - Difference: {high_study - low_study:.2f} points")
print(f"\n2. Attendance Impact:")
print(f" - Students with >75% attendance: Average score = {high_attendance:.2f}")
print(f" - Students with ≤75% attendance: Average score = {low_attendance:.2f}")
print(f" - Difference: {high_attendance - low_attendance:.2f} points")
print(f"\n3. Pass Rate by Attendance:")
print(f" - >75% attendance: {high_attendance_pass_rate:.1f}% pass rate")
print(f" - ≤75% attendance: {low_attendance_pass_rate:.1f}% pass rate")
print(f"\n4. Sleep Impact:")
sleep_score_corr = df_clean['sleep_hours'].corr(df_clean['final_score'])
print(f" - Correlation between sleep and score: {sleep_score_corr:.3f}")
print("\n" + "="*50)
print("ANALYSIS COMPLETE!")
print("="*50)
print("\nGenerated files:")
print(" 1. student_performance_cleaned.csv - Cleaned dataset")
print(" 2. student_analysis_charts.png - All visualizations")
print("\nNext: Check the charts and review the insights above!")