Repository navigation
Expand file tree
/
Copy pathevaluation.py
More file actions
84 lines (62 loc) · 2.97 KB
/
Copy pathevaluation.py
File metadata and controls
84 lines (62 loc) · 2.97 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
import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import StratifiedKFold
from distance_metrics import heom_distance, compute_feature_ranges
from config import N_FOLDS, SEED
def fitness_function_cv(individual, X, y, numeric_cols, n_folds=N_FOLDS):
try:
selected = individual.astype(bool)
if np.sum(selected) < 1:
return 0.0
X_selected = X[:, selected]
selected_indices = np.where(selected)[0]
selected_numeric_cols = [i for i, orig_idx in enumerate(selected_indices)
if orig_idx in numeric_cols]
skf = StratifiedKFold(n_splits=n_folds, shuffle=True, random_state=SEED)
aucs = []
ranges = None
for train_idx, val_idx in skf.split(X_selected, y):
X_train, X_val = X_selected[train_idx], X_selected[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
if ranges is None:
ranges = compute_feature_ranges(X_train, selected_numeric_cols)
distances = heom_distance(X_train, X_val, selected_numeric_cols, ranges)
if distances.shape[1] == 0:
aucs.append(0.0)
continue
nearest_idx = np.argmin(distances, axis=1)
y_pred = y_train[nearest_idx]
tn, fp, fn, tp = confusion_matrix(y_val, y_pred, labels=[0, 1]).ravel()
tpr = tp / (tp + fn) if (tp + fn) > 0 else 0
tnr = tn / (tn + fp) if (tn + fp) > 0 else 0
aucs.append((tpr + tnr) / 2)
return np.mean(aucs)
except Exception as e:
print(f" Error in fitness CV: {str(e)}")
return 0.0
def evaluate_test_performance(X_train, y_train, X_test, y_test, selected_features, numeric_cols):
try:
if len(np.unique(y_test)) < 2:
print(f" Warning: Only one class in test set")
return "N/A"
X_train_selected = X_train[:, selected_features]
X_test_selected = X_test[:, selected_features]
selected_indices = np.where(selected_features)[0]
selected_numeric_cols = [i for i, orig_idx in enumerate(selected_indices)
if orig_idx in numeric_cols]
ranges = compute_feature_ranges(X_train_selected, selected_numeric_cols)
distances = heom_distance(X_train_selected, X_test_selected, selected_numeric_cols, ranges)
nearest_idx = np.argmin(distances, axis=1)
y_pred = y_train[nearest_idx]
tn, fp, fn, tp = confusion_matrix(y_test, y_pred, labels=[0, 1]).ravel()
tpr = tp / (tp + fn) if (tp + fn) > 0 else 0
tnr = tn / (tn + fp) if (tn + fp) > 0 else 0
auc = (tpr + tnr) / 2
return auc
except Exception as e:
print(f" Error in test evaluation: {str(e)}")
return "Error"
def calculate_auc_from_confusion_matrix(tn, fp, fn, tp):
tpr = tp / (tp + fn) if (tp + fn) > 0 else 0
tnr = tn / (tn + fp) if (tn + fp) > 0 else 0
return (tpr + tnr) / 2