-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0 Concatenation Early Stage with NaNs Fusion.py
More file actions
104 lines (81 loc) · 4.07 KB
/
Copy path0 Concatenation Early Stage with NaNs Fusion.py
File metadata and controls
104 lines (81 loc) · 4.07 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
%reset -f
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
# Load the CSV files into pandas DataFrames
ecg_df = pd.read_csv('ECG.csv')
eye_tracking_df = pd.read_csv('EyeTracking.csv')
gsr_df = pd.read_csv('GSR.csv')
# the first column is labeled consistently as 'Label'
ecg_df.columns = ['Label'] + list(ecg_df.columns[1:])
eye_tracking_df.columns = ['Label'] + list(eye_tracking_df.columns[1:])
gsr_df.columns = ['Label'] + list(gsr_df.columns[1:])
# Adding prefixes to feature columns to have unique feature names
ecg_df.columns = ['Label'] + ['ECG_' + col for col in ecg_df.columns[1:]]
eye_tracking_df.columns = ['Label'] + ['Eye_' + col for col in eye_tracking_df.columns[1:]]
gsr_df.columns = ['Label'] + ['GSR_' + col for col in gsr_df.columns[1:]]
# Padding DataFrames to the same number of rows (using NaN where data is missing)
max_rows = max(len(ecg_df), len(eye_tracking_df), len(gsr_df))
# Reindex each DataFrame to ensure they all have the same number of rows
ecg_df = ecg_df.reindex(range(max_rows), fill_value=np.nan)
eye_tracking_df = eye_tracking_df.reindex(range(max_rows), fill_value=np.nan)
gsr_df = gsr_df.reindex(range(max_rows), fill_value=np.nan)
# Concatenate the features from all DataFrames (dropping the 'Label' column for now)
features_concat = pd.concat([
ecg_df.drop(columns=['Label']),
eye_tracking_df.drop(columns=['Label']),
gsr_df.drop(columns=['Label'])
], axis=1)
# Re-add the 'Label' column
labels = ecg_df['Label'].fillna(method='ffill') # Forward-fill label if any NaN
# Standardize the features (ignoring NaNs in standardization)
scaler = StandardScaler()
features_standardized = pd.DataFrame(scaler.fit_transform(features_concat), columns=features_concat.columns)
# Number of runs and seed for reproducibility
num_runs = 3
random_seed = 42
# Store predictions and true labels across runs
all_true_labels = []
all_predictions = []
cumulative_confusion_matrix = np.zeros((len(labels.unique()), len(labels.unique())))
all_accuracies = []
# Run XGBoost multiple times with shuffling
for run in range(num_runs):
# Shuffle the data
X_train, X_test, y_train, y_test = train_test_split(
features_standardized, labels, test_size=0.2, random_state=random_seed + run, shuffle=True
)
# Initialize XGBoost classifier
model = XGBClassifier(use_label_encoder=False, eval_metric='mlogloss')
# Fit the model
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Accumulate true labels and predictions
all_true_labels.extend(y_test)
all_predictions.extend(y_pred)
# Accumulate confusion matrix
cm = confusion_matrix(y_test, y_pred)
cumulative_confusion_matrix += cm
# Calculate accuracy for this run
accuracy = accuracy_score(y_test, y_pred)
all_accuracies.append(accuracy)
# Final classification report based on all accumulated predictions
print("\nFinal Aggregated Classification Report Across 3 Runs:")
final_report = classification_report(all_true_labels, all_predictions)
print(final_report)
# Print the cumulative confusion matrix
print("\nCumulative Confusion Matrix Across 3 Runs:")
print(cumulative_confusion_matrix.astype(int))
# Print accuracies for each run in a single line
print("Accuracies for each run: ", " | ".join([f"Run {i+1}: {acc:.4f}" for i, acc in enumerate(all_accuracies)]))
# Print the final averaged accuracy across all runs
average_accuracy = np.mean(all_accuracies)
print(f"\nAveraged Accuracy Across 3 Runs: {average_accuracy:.4f}")
# Combine the standardized features with the Label column
final_data_with_labels = pd.concat([features_standardized, labels.reset_index(drop=True)], axis=1)
# Save the final standardized dataset with the label column to a CSV file
final_data_with_labels.to_csv('Concatenated.csv', index=False)