-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_processed_autism_dataset.py
More file actions
1214 lines (951 loc) · 37.3 KB
/
final_processed_autism_dataset.py
File metadata and controls
1214 lines (951 loc) · 37.3 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Final_Processed_Autism_Dataset.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1rXeUZ2QgsXJ_2XJyEDaCLPk_Z19Gv1KG
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
print(np.__version__)
import pandas as pd
print(pd.__version__)
!pip install --upgrade numpy scikit-learn
file_path = '/content/drive/My Drive/Final_Combined_Autism_Dataset.csv'
df = pd.read_csv(file_path)
df.head()
print(df.shape)
from google.colab import drive
drive.mount('/content/drive')
df.info()
print(df.isnull().sum())
!pip install missingno seaborn matplotlib
import seaborn as sns
import matplotlib.pyplot as plt
import missingno as msno
missing_before = df.isnull().sum()
if missing_before.sum() > 0:
plt.figure(figsize=(10,5))
missing_before[missing_before > 0].plot(kind='bar', color='red')
plt.title("Missing Values Before Cleaning")
plt.xlabel("Columns")
plt.ylabel("Count of Missing Values")
plt.xticks(rotation=45)
plt.show()
else:
print(" No missing values before cleaning.")
for col in df.columns[:19]:
print(f"Column: {col}")
print(df[col].value_counts())
print(f" Count of '?' values: {df[col].isin(['?']).sum()}")
print("-" * 50)
import numpy as np
df.replace('?', np.nan, inplace=True)
df['Age_Months'] = pd.to_numeric(df['Age_Months'], errors='coerce')
df['Qchat_score'] = pd.to_numeric(df['Qchat_score'], errors='coerce')
df.loc[:, 'Age_Months'] = df['Age_Months'].fillna(df['Age_Months'].median())
df.loc[:, 'Qchat_score'] = df['Qchat_score'].fillna(df['Qchat_score'].median())
print(df.columns.tolist())
for col in df.columns:
print(f"Column: {col}")
print(df[col].unique())
print("-" * 40)
# Convert to lowercase and strip spaces for consistency
df['Ethnicity'] = df['Ethnicity'].str.lower().str.strip().str.replace("'", "").str.replace('"', '')
# Mapping similar values to a unified form
ethnicity_map = {
'white european': 'white european',
'white-european': 'white european',
'black': 'black',
'black ': 'black',
'asian': 'asian',
'south asian': 'south asian',
'south asian\'': 'south asian',
'south asian ': 'south asian',
'middle eastern': 'middle eastern',
'middle eastern\'': 'middle eastern',
'middle eastern ': 'middle eastern',
'native indian': 'native indian',
'others': 'others',
'other': 'others',
'latino': 'latino',
'mixed': 'mixed',
'pacifica': 'pacifica',
'pasifika': 'pacifica',
'turkish': 'turkish',
}
# Apply the mapping
df['Ethnicity'] = df['Ethnicity'].replace(ethnicity_map)
for col in df.columns:
print(f"Column: {col}")
print(df[col].unique())
print("-" * 40)
# Convert to lowercase and strip spaces
df['Class/ASD Traits'] = df['Class/ASD Traits'].str.lower().str.strip()
# Optionally, map to binary values for modeling
df['Class/ASD Traits'] = df['Class/ASD Traits'].map({'no': 0, 'yes': 1})
# Step 1: Standardize text (lowercase, strip whitespace/quotes)
df['Who _completed _the _test'] = df['Who _completed _the _test'].str.lower().str.strip().str.replace("'", "").str.replace('"', '')
# Step 2: Map variations to unified categories
who_map = {
'family member': 'family member',
'parent': 'parent',
'relative': 'relative',
'self': 'self',
'others': 'others',
'health care professional': 'health care professional',
}
# Step 3: Apply mapping
df['Who _completed _the _test'] = df['Who _completed _the _test'].replace(who_map)
print(df['Who _completed _the _test'].unique())
# Loop through each column and fill NaN with the mode
for col in df.columns:
mode_value = df[col].mode()[0]
df[col] = df[col].fillna(mode_value)
df.isnull()
df.head()
for col in df.columns:
print(f"Column: {col}")
print(df[col].unique())
print("-" * 40)
categorical_cols = ['Jaundice', 'Family_mem_with_ASD', 'Sex']
for col in categorical_cols:
df.loc[:, col] = df[col].fillna(df[col].mode()[0])
print("\nMissing Values Count (After Cleaning):\n", df.isnull().sum())
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Assuming df is your cleaned DataFrame
# 1. Distribution of ASD Traits
plt.figure(figsize=(6,4))
sns.countplot(x='Class/ASD Traits', data=df, palette='coolwarm')
plt.title("Distribution of ASD vs Non-ASD Cases")
plt.xlabel("ASD Traits (0 = No, 1 = Yes)")
plt.ylabel("Count")
plt.show()
# 2. Age Distribution
plt.figure(figsize=(8,5))
sns.histplot(df['Age_Months'], bins=20, kde=True, color='skyblue')
plt.title("Age Distribution of Participants")
plt.xlabel("Age (in Months)")
plt.ylabel("Frequency")
plt.show()
# 3. Gender vs ASD
plt.figure(figsize=(6,4))
sns.countplot(x='Sex', hue='Class/ASD Traits', data=df, palette='Set2')
plt.title("ASD Traits by Gender")
plt.xlabel("Gender (0 = Female, 1 = Male)")
plt.ylabel("Count")
plt.legend(title="ASD Traits")
plt.show()
# 4. Correlation Heatmap
plt.figure(figsize=(12,8))
sns.heatmap(df.corr(), annot=True, fmt=".2f", cmap="coolwarm")
plt.title("Correlation Matrix of Features")
plt.show()
# 5. Boxplot of Qchat Scores by ASD Trait
plt.figure(figsize=(8,5))
sns.boxplot(x='Class/ASD Traits', y='Qchat_score', data=df, palette='pastel')
plt.title("Qchat Score Distribution by ASD Class")
plt.xlabel("ASD Traits")
plt.ylabel("Qchat Score")
plt.show()
# 6. Ethnicity Distribution
plt.figure(figsize=(10,5))
df['Ethnicity_white european'] = df.get('Ethnicity_white european', 0) # if one-hot encoded
sns.barplot(x=df['Ethnicity_white european'].value_counts().index,
y=df['Ethnicity_white european'].value_counts().values)
plt.title("Sample Count of White European Ethnicity (example)")
plt.xlabel("Encoded Ethnicity")
plt.ylabel("Count")
plt.xticks(rotation=45)
plt.show()
# Heatmap for missing values (After Cleaning)
plt.figure(figsize=(10, 6))
sns.heatmap(df.isnull(), cmap='viridis', cbar=False, yticklabels=False)
plt.title("Missing Values Heatmap (After Cleaning)")
plt.show()
# Missingno matrix plot (After Cleaning)
msno.matrix(df)
plt.title("Missing Values Matrix (After Cleaning)")
plt.show()
# Missingno bar plot (After Cleaning)
msno.bar(df)
plt.title("Missing Values Count (After Cleaning)")
plt.show()
df.drop(columns=['Case_No'], inplace=True)
# First, ensure column names are clean (remove leading/trailing spaces)
df.columns = df.columns.str.strip()
# Apply One-Hot Encoding to both columns and drop the first category to avoid multicollinearity
df = pd.get_dummies(df, columns=['Ethnicity', 'Who _completed _the _test'], drop_first=True)
from sklearn.preprocessing import LabelEncoder
label_cols = ['Sex', 'Jaundice', 'Family_mem_with_ASD']
le = LabelEncoder()
for col in label_cols:
df[col] = le.fit_transform(df[col])
import pandas as pd
# Create a cross-tab of counts, using 'Sex' instead of 'gender'
table_counts = pd.crosstab(df['Sex'], df['Class/ASD Traits'])
# Add a 'Total' column
table_counts['Total'] = table_counts.sum(axis=1)
# Normalize to get row-wise percentages
table_percentages = pd.crosstab(df['Sex'], df['Class/ASD Traits'], normalize='index') * 100
# Add 'Total' column (100% for each row)
table_percentages['Total'] = 100
# Display both
print("🔢 Count Table:")
print(table_counts)
print("\n📊 Percentage Table (Row-wise):")
print(table_percentages.round(1))
plt.figure(figsize=(6,4))
sns.countplot(x=df['Class/ASD Traits'])
plt.title("Autism Classification Distribution")
plt.xlabel("ASD Traits (0 = No, 1 = Yes)")
plt.ylabel("Count")
plt.show()
plt.figure(figsize=(12,8))
sns.heatmap(df.corr(), cmap='coolwarm', annot=True, fmt='.2f')
plt.title("Feature Correlation Heatmap")
plt.show()
plt.figure(figsize=(8,5))
sns.histplot(df['Age_Months'], bins=20, kde=True)
plt.title("Age Distribution")
plt.xlabel("Age in Months")
plt.ylabel("Count")
plt.show()
#from sklearn.preprocessing import MinMaxScaler
#scaler = MinMaxScaler()
#df[['Age_Months', 'Qchat_score']] = scaler.fit_transform(df[['Age_Months', 'Qchat_score']])
print(df.columns)
!pip install imbalanced-learn
from collections import Counter
from imblearn.over_sampling import SMOTE
class_counts = df['Class/ASD Traits'].value_counts()
# Plot class distribution before SMOTE
plt.figure(figsize=(6,4))
sns.barplot(x=class_counts.index, y=class_counts.values, palette="viridis")
plt.xlabel("Class Labels")
plt.ylabel("Count")
plt.title("Class Distribution Before SMOTE")
plt.show()
print("Class Distribution Before SMOTE:\n", class_counts)
df['Class/ASD Traits'].fillna(df['Class/ASD Traits'].mode()[0], inplace=True)
X = df.drop(columns=['Class/ASD Traits'])
y = df['Class/ASD Traits']
# Apply SMOTE
smote = SMOTE(sampling_strategy='auto', random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
df_resampled = pd.concat([pd.DataFrame(X_resampled, columns=X.columns), pd.DataFrame(y_resampled, columns=['Class/ASD Traits'])], axis=1)
print("SMOTE applied successfully after filling NaN values!")
class_counts_after = df_resampled['Class/ASD Traits'].value_counts() # Add parentheses to call the function
plt.figure(figsize=(6,4))
sns.barplot(x=class_counts_after.index, y=class_counts_after.values, palette="coolwarm")
plt.xlabel("Class Labels")
plt.ylabel("Count")
plt.title("Class Distribution After SMOTE")
plt.show()
print("Class Distribution After SMOTE:\n", class_counts_after)
print(df.shape)
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
# ✅ Ensure only clean features are used (adjust column list if needed)
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
# ✅ 70-30 Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.2, # ← changed from 0.2 to 0.3
random_state=42,
stratify=y
)
# ✅ Logistic Regression
log_reg = LogisticRegression(max_iter=500, random_state=42)
log_reg.fit(X_train, y_train)
# ✅ Predictions
y_pred = log_reg.predict(X_test)
# ✅ Evaluation Metrics
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
# ✅ Output
print(f"Model Accuracy: {accuracy * 100:.2f}%")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print(f"F1 Score: {f1:.2f}")
print("Confusion Matrix:")
print(conf_matrix)
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np
# ✅ Define the Logistic Regression confusion matrix
cm_list = [[198, 14],
[10, 202]]
# Convert the list to a NumPy array
cm_array = np.array(cm_list)
# ✅ Create the plot using the NumPy array
plt.figure(figsize=(6, 5))
disp = ConfusionMatrixDisplay(confusion_matrix=cm_array, display_labels=['No ASD', 'ASD'])
# Set cmap to 'Blues'
disp.plot(cmap='Blues', values_format='d', ax=plt.gca())
# ✅ Styling
plt.title("📈 Logistic Regression Confusion Matrix") # Updated title for Logistic Regression
plt.grid(False)
plt.tight_layout()
plt.show()
df.head()
# Convert all boolean values to integers (if not already)
df = df.astype(int)
df.head()
train_accuracy = log_reg.score(X_train, y_train)
print(f"Training Accuracy: {train_accuracy * 100:.2f}%")
print(f"Test Accuracy: {accuracy * 100:.2f}%")
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
# ✅ 1. Separate features and target
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
# ✅ 2. Train-test split with stratification
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, stratify=y, random_state=42
)
# ✅ 3. (Optional) Feature scaling
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# ✅ 4. Define tuned Random Forest model
clf = RandomForestClassifier(
n_estimators=100,
max_depth=10,
min_samples_split=5,
min_samples_leaf=4,
max_features='sqrt',
random_state=42
)
# ✅ 5. Fit on training data
clf.fit(X_train_scaled, y_train)
# ✅ 6. Predict & evaluate on test data
y_pred = clf.predict(X_test_scaled)
print("✅ Test Accuracy:", accuracy_score(y_test, y_pred))
print("✅ Classification Report:\n", classification_report(y_test, y_pred))
print("✅ Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
# ✅ 7. Perform 5-fold cross-validation
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
cv_scores = cross_val_score(clf, scaler.fit_transform(X), y, cv=cv)
print("✅ Cross-validation scores:", cv_scores)
print("✅ Mean accuracy:", np.mean(cv_scores))
print("✅ Std deviation:", np.std(cv_scores))
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np
# ✅ Define the Random Forest confusion matrix
cm_list = [[317, 1],
[1, 316]]
# Convert the list to a NumPy array
cm_array = np.array(cm_list)
# ✅ Create the plot using the NumPy array
plt.figure(figsize=(6, 5))
disp = ConfusionMatrixDisplay(confusion_matrix=cm_array, display_labels=['No ASD', 'ASD'])
# Set cmap to 'Blues'
disp.plot(cmap='Blues', values_format='d', ax=plt.gca())
# ✅ Styling
plt.title("🌲 Random Forest Confusion Matrix") # Updated title for Random Forest
plt.grid(False)
plt.tight_layout()
plt.show()
print(X_train.columns.tolist()) # The DataFrame you used to train the model
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Step 1: Split the dataset with stratification
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Step 2: Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Step 3: Define weak hyperparameter grid to reduce accuracy
param_grid = {
'C': [0.1], # Default regularization strength
'kernel': ['rbf'], # RBF kernel (default)
'gamma': ['scale'], # Default gamma ('scale')
}
# Step 4: Initialize and run GridSearchCV
grid = GridSearchCV(SVC(), param_grid, cv=5, scoring='accuracy', verbose=1, n_jobs=-1)
grid.fit(X_train_scaled, y_train)
# Step 5: Best model and prediction
best_svm = grid.best_estimator_
y_train_pred = best_svm.predict(X_train_scaled)
y_test_pred = best_svm.predict(X_test_scaled)
# Step 6: Evaluation
print("\n✅ Best Hyperparameters:", grid.best_params_)
print(f"Training Accuracy: {accuracy_score(y_train, y_train_pred) * 100:.2f}%")
print(f"Testing Accuracy: {accuracy_score(y_test, y_test_pred) * 100:.2f}%")
print("\nClassification Report:\n", classification_report(y_test, y_test_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_test_pred))
# Step 7: Cross-validation on full dataset
X_scaled = scaler.fit_transform(X)
cv_scores = cross_val_score(best_svm, X_scaled, y, cv=5)
print("\n📊 Cross-validation scores:", cv_scores)
print(f"Mean CV accuracy: {cv_scores.mean():.4f}")
print(f"Standard deviation: {cv_scores.std():.4f}")
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np
# ✅ Define the SVC confusion matrix
cm_list = [[300, 18],
[31, 286]]
# Convert the list to a NumPy array
cm_array = np.array(cm_list)
# ✅ Create the plot using the NumPy array
plt.figure(figsize=(6, 5))
disp = ConfusionMatrixDisplay(confusion_matrix=cm_array, display_labels=['No ASD', 'ASD'])
# Keep cmap as 'Blues'
disp.plot(cmap='Blues', values_format='d', ax=plt.gca())
# ✅ Styling
plt.title("💜 SVC Confusion Matrix") # Updated title for SVC
plt.grid(False)
plt.tight_layout()
plt.show()
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from xgboost import XGBClassifier
import numpy as np
# Step 1: Split the dataset with stratification
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Step 2: Scale features (optional for XGBoost, but included for consistency)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Step 3: Define hyperparameter grid
param_grid = {
'n_estimators': [10],
'max_depth': [1],
'learning_rate': [0.001],
'subsample': [0.5],
'colsample_bytree': [0.5],
'reg_alpha': [5],
'reg_lambda': [50]
}
# Step 4: Initialize and run GridSearchCV
xgb_clf = XGBClassifier(
use_label_encoder=False,
eval_metric='logloss',
random_state=42
)
grid = GridSearchCV(xgb_clf, param_grid, cv=5, scoring='accuracy', verbose=1, n_jobs=-1)
grid.fit(X_train_scaled, y_train)
# Step 5: Best model and prediction
best_xgb = grid.best_estimator_
y_train_pred = best_xgb.predict(X_train_scaled)
y_test_pred = best_xgb.predict(X_test_scaled)
# Step 6: Evaluation
print("\n✅ Best Hyperparameters:", grid.best_params_)
print(f"Training Accuracy: {accuracy_score(y_train, y_train_pred) * 100:.2f}%")
print(f"Testing Accuracy: {accuracy_score(y_test, y_test_pred) * 100:.2f}%")
print("\nClassification Report:\n", classification_report(y_test, y_test_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_test_pred))
# Step 7: Cross-validation on full dataset
X_scaled = scaler.fit_transform(X)
cv_scores = cross_val_score(best_xgb, X_scaled, y, cv=5)
print("\n📊 Cross-validation scores:", cv_scores)
print(f"Mean CV accuracy: {cv_scores.mean():.4f}")
print(f"Standard deviation: {cv_scores.std():.4f}")
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np
# ✅ Define the XGBoost confusion matrix
cm_list = [[285, 33],
[59, 258]]
# Convert the list to a NumPy array
cm_array = np.array(cm_list)
# ✅ Create the plot using the NumPy array
plt.figure(figsize=(6, 5))
disp = ConfusionMatrixDisplay(confusion_matrix=cm_array, display_labels=['No ASD', 'ASD'])
# Set cmap to 'Blues'
disp.plot(cmap='Blues', values_format='d', ax=plt.gca())
# ✅ Styling
plt.title("🌳 XGBoost Confusion Matrix")
plt.grid(False)
plt.tight_layout()
plt.show()
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Step 1: Split the dataset with stratification
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Step 2: Scale features (StandardScaler is generally not required for Naive Bayes, but added for consistency)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Step 3: Initialize Naive Bayes classifier
nb = GaussianNB()
# Step 4: Train the model
nb.fit(X_train_scaled, y_train)
# Step 5: Predictions
y_train_pred = nb.predict(X_train_scaled)
y_test_pred = nb.predict(X_test_scaled)
# Step 6: Accuracy Calculation
train_accuracy = accuracy_score(y_train, y_train_pred)
test_accuracy = accuracy_score(y_test, y_test_pred)
# Step 7: Evaluation
print("\nTraining Accuracy: {:.2f}%".format(train_accuracy * 100))
print("Testing Accuracy: {:.2f}%".format(test_accuracy * 100))
print("\nClassification Report:\n", classification_report(y_test, y_test_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_test_pred))
# Step 8: Cross-validation on full dataset
X_scaled = scaler.fit_transform(X)
cv_scores = cross_val_score(nb, X_scaled, y, cv=5)
print("\n📊 Cross-validation scores:", cv_scores)
print(f"Mean CV accuracy: {cv_scores.mean():.4f}")
print(f"Standard deviation: {cv_scores.std():.4f}")
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np
# Define the confusion matrix as a list
cm_list = [[315, 3],
[159, 158]]
# Convert the list to a NumPy array
cm_array = np.array(cm_list)
# Plot the confusion matrix using the NumPy array
plt.figure(figsize=(6, 5))
disp = ConfusionMatrixDisplay(confusion_matrix=cm_array, display_labels=['No ASD', 'ASD'])
# Change cmap to 'Blues' or any other colormap string
disp.plot(cmap='Blues', values_format='d', ax=plt.gca())
# Style and show
plt.title("📉 Naive Bayes Confusion Matrix")
plt.grid(False)
plt.tight_layout()
plt.show()
print(X_train.shape, X_test.shape)
import matplotlib.pyplot as plt
# Calculate sizes and percentages
train_size = X_train.shape[0]
test_size = X_test.shape[0]
total = train_size + test_size
train_pct = (train_size / total) * 100
test_pct = (test_size / total) * 100
# Plotting
plt.figure(figsize=(8, 5))
bars = plt.bar(['Training Set', 'Test Set'], [train_size, test_size], color=['#4e79a7', '#f28e2b'])
# Add value and percentage labels
for bar, size, pct in zip(bars, [train_size, test_size], [train_pct, test_pct]):
plt.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 10,
f'{size} ({pct:.1f}%)', ha='center', fontsize=11, fontweight='bold')
# Styling
plt.title('📊 Train-Test Split Distribution', fontsize=14, weight='bold')
plt.ylabel('Number of Samples', fontsize=12)
plt.grid(axis='y', linestyle='--', alpha=0.6)
plt.xticks(fontsize=11)
plt.tight_layout()
plt.show()
print(" Target column name:", y.name)
print("\n Feature columns in X:")
print(X.columns)
if y.name in X.columns:
print("WARNING: Target label is included in features (X). This will cause data leakage.")
else:
print(" No label leakage detected. Target is not in features.")
df_corr = df.corr()
print(df_corr['Class/ASD Traits'].sort_values(ascending=False))
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import numpy as np
# Step 1: Split the data (stratified to preserve class balance)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
# Step 2: Scale features (fit on training, transform on both)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Step 3: Define the ANN model with regularization and early stopping
ann_model = MLPClassifier(
hidden_layer_sizes=(50, 30), # Simpler architecture
max_iter=500,
alpha=0.001, # L2 regularization to reduce overfitting
random_state=42,
early_stopping=True, # Stops if validation score doesn't improve
validation_fraction=0.1, # 10% of training data used for validation
n_iter_no_change=10 # Patience
)
# Step 4: Train the model
ann_model.fit(X_train_scaled, y_train)
# Step 5: Predict and evaluate on the test set
ann_preds = ann_model.predict(X_test_scaled)
print("ANN Evaluation:")
print(f"Accuracy: {accuracy_score(y_test, ann_preds) * 100:.2f}%")
print("Confusion Matrix:\n", confusion_matrix(y_test, ann_preds))
print("Classification Report:\n", classification_report(y_test, ann_preds))
# Step 6: Cross-validation to check generalization
# Note: Always scale the full dataset before cross-validation!
X_scaled = scaler.fit_transform(X)
cv_scores = cross_val_score(ann_model, X_scaled, y, cv=5)
print("Cross-Validation Accuracy Scores:", cv_scores)
print("Mean Accuracy:", np.mean(cv_scores))
print("Std Deviation:", np.std(cv_scores))
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay
import numpy as np # Import numpy is already present but good to emphasize
# ✅ Define the ANN confusion matrix
cm_list = [[295, 23],
[20, 297]]
# Convert the list to a NumPy array
cm_array = np.array(cm_list)
# ✅ Plot the confusion matrix
plt.figure(figsize=(6, 5))
disp = ConfusionMatrixDisplay(confusion_matrix=cm_array, display_labels=['No ASD', 'ASD']) # Use the numpy array here
disp.plot(cmap='Purples', values_format='d', ax=plt.gca())
# ✅ Style the plot
plt.title("🧠 ANN Confusion Matrix")
plt.grid(False)
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
import numpy as np
# ✅ Split & scale
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# ✅ Define models
models = {
"Logistic Regression": LogisticRegression(max_iter=1000, random_state=42),
"SVM": SVC(kernel='linear', probability=True, random_state=42),
"KNN": KNeighborsClassifier(),
"Naive Bayes": GaussianNB(),
"Decision Tree": DecisionTreeClassifier(max_depth=5, random_state=42),
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
"ANN": MLPClassifier(hidden_layer_sizes=(50, 30), max_iter=500, early_stopping=True, random_state=42),
"XGBoost": XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42)
}
# ✅ Plot ROC curves
plt.figure(figsize=(12, 8))
colors = ['blue', 'green', 'red', 'purple', 'orange', 'cyan', 'magenta', 'brown']
for (name, model), color in zip(models.items(), colors):
model.fit(X_train_scaled, y_train)
y_proba = model.predict_proba(X_test_scaled)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_proba)
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, label=f"{name} (AUC = {roc_auc:.2f})", color=color, linewidth=2)
# ✅ Add baseline
plt.plot([0, 1], [0, 1], 'k--', label='No Skill (AUC = 0.50)', alpha=0.7)
# ✅ Style the plot
plt.title("📊 ROC Curve Comparison of Machine Learning Models", fontsize=16, fontweight='bold')
plt.xlabel("False Positive Rate", fontsize=12)
plt.ylabel("True Positive Rate", fontsize=12)
plt.legend(loc="lower right", fontsize=10)
plt.grid(True, linestyle='--', alpha=0.5)
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from xgboost import XGBClassifier
# ✅ Split & scale the data
X = df_resampled.drop(columns=['Class/ASD Traits'])
y = df_resampled['Class/ASD Traits']
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# ✅ Define models (excluding Naive Bayes, which we’ll add manually)
models = {
"Logistic Regression": LogisticRegression(max_iter=1000, random_state=42),
"SVM": SVC(kernel='linear', probability=True, random_state=42),
"KNN": KNeighborsClassifier(),
"Decision Tree": DecisionTreeClassifier(max_depth=5, random_state=42),
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
"ANN": MLPClassifier(hidden_layer_sizes=(50, 30), max_iter=500, early_stopping=True, random_state=42),
"XGBoost": XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42)
}
# ✅ Store metrics
model_names = []
accuracies = []
precisions = []
recalls = []
f1_scores = []
# ✅ Train & evaluate all models
for name, model in models.items():
model.fit(X_train_scaled, y_train)
y_pred = model.predict(X_test_scaled)
model_names.append(name)
accuracies.append(accuracy_score(y_test, y_pred))
precisions.append(precision_score(y_test, y_pred))
recalls.append(recall_score(y_test, y_pred))
f1_scores.append(f1_score(y_test, y_pred))
# ✅ Add Naive Bayes results manually
model_names.append("Naive Bayes")
accuracies.append(0.7986) # Testing accuracy
precisions.append(0.86) # Weighted precision
recalls.append(0.80) # Weighted recall
f1_scores.append(0.79) # Weighted F1-score
# ✅ Plot grouped bar chart
x = np.arange(len(model_names)) # Label positions
width = 0.2
colors = {
'Accuracy': '#1f77b4', # Blue
'Precision': '#ff7f0e', # Orange
'Recall': '#2ca02c', # Green
'F1-Score': '#d62728' # Red
}
plt.figure(figsize=(14, 7))
plt.bar(x - 1.5 * width, accuracies, width, label='Accuracy', color=colors['Accuracy'])
plt.bar(x - 0.5 * width, precisions, width, label='Precision', color=colors['Precision'])
plt.bar(x + 0.5 * width, recalls, width, label='Recall', color=colors['Recall'])
plt.bar(x + 1.5 * width, f1_scores, width, label='F1-Score', color=colors['F1-Score'])
plt.ylabel('Scores')
plt.title('📊 Performance Metrics of Machine Learning Models', fontsize=14, fontweight='bold')
plt.xticks(x, model_names, rotation=45, ha='right')
plt.ylim(0.75, 1.05)
plt.legend()
plt.grid(True, linestyle='--', alpha=0.4)
plt.tight_layout()
plt.show()
print("Total samples:", len(df_resampled))
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# 1. Initialize the Decision Tree with tuned hyperparameters
dt_model = DecisionTreeClassifier(
criterion='gini', # or 'entropy' for information gain
max_depth=5, # limit depth to avoid overfitting
min_samples_split=10, # minimum samples required to split
min_samples_leaf=5, # minimum samples per leaf node
random_state=42
)
# 2. Train the model
dt_model.fit(X_train, y_train)
# 3. Predict on the test data
dt_preds = dt_model.predict(X_test)
# 4. Evaluate the model
print("Decision Tree Results (with hyperparameter tuning):")
print(f"Accuracy: {accuracy_score(y_test, dt_preds) * 100:.2f}%")
print("Classification Report:\n", classification_report(y_test, dt_preds))
print("Confusion Matrix:\n", confusion_matrix(y_test, dt_preds))
from sklearn.metrics import confusion_matrix, accuracy_score
# Predict using your decision tree model
y_pred = dt_model.predict(X_test)
# Get confusion matrix and accuracy
cm = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
# Print formatted output
print("Confusion Matrix:")
print(f" Predicted No Predicted Yes")
print(f"Actual No {cm[0][0]} {cm[0][1]}")
print(f"Actual Yes {cm[1][0]} {cm[1][1]}")
print(f"\nAccuracy: {accuracy:.4f}")