pyerualjetwork 4.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,190 @@
1
+ import numpy as np
2
+
3
+ def metrics(y_ts, test_preds, average='weighted'):
4
+ """
5
+ Calculates precision, recall and F1 score for a classification task.
6
+
7
+ Args:
8
+ y_ts (list or numpy.ndarray): True labels.
9
+ test_preds (list or numpy.ndarray): Predicted labels.
10
+ average (str): Type of averaging ('micro', 'macro', 'weighted').
11
+
12
+ Returns:
13
+ tuple: Precision, recall, F1 score.
14
+ """
15
+
16
+ from .data_operations import decode_one_hot
17
+
18
+ y_test_d = decode_one_hot(y_ts)
19
+ y_test_d = np.array(y_test_d)
20
+ y_pred = np.array(test_preds)
21
+
22
+ if y_test_d.ndim > 1:
23
+ y_test_d = y_test_d.reshape(-1)
24
+ if y_pred.ndim > 1:
25
+ y_pred = y_pred.reshape(-1)
26
+
27
+ tp = {}
28
+ fp = {}
29
+ fn = {}
30
+
31
+ classes = np.unique(np.concatenate((y_test_d, y_pred)))
32
+
33
+ for c in classes:
34
+ tp[c] = 0
35
+ fp[c] = 0
36
+ fn[c] = 0
37
+
38
+ for c in classes:
39
+ for true, pred in zip(y_test_d, y_pred):
40
+ if true == c and pred == c:
41
+ tp[c] += 1
42
+ elif true != c and pred == c:
43
+ fp[c] += 1
44
+ elif true == c and pred != c:
45
+ fn[c] += 1
46
+
47
+ precision = {}
48
+ recall = {}
49
+ f1 = {}
50
+
51
+ for c in classes:
52
+ precision[c] = tp[c] / (tp[c] + fp[c]) if (tp[c] + fp[c]) > 0 else 0
53
+ recall[c] = tp[c] / (tp[c] + fn[c]) if (tp[c] + fn[c]) > 0 else 0
54
+ f1[c] = 2 * (precision[c] * recall[c]) / (precision[c] + recall[c]) if (precision[c] + recall[c]) > 0 else 0
55
+
56
+ if average == 'micro':
57
+ precision_val = np.sum(list(tp.values())) / (np.sum(list(tp.values())) + np.sum(list(fp.values()))) if (np.sum(list(tp.values())) + np.sum(list(fp.values()))) > 0 else 0
58
+ recall_val = np.sum(list(tp.values())) / (np.sum(list(tp.values())) + np.sum(list(fn.values()))) if (np.sum(list(tp.values())) + np.sum(list(fn.values()))) > 0 else 0
59
+ f1_val = 2 * (precision_val * recall_val) / (precision_val + recall_val) if (precision_val + recall_val) > 0 else 0
60
+
61
+ elif average == 'macro':
62
+ precision_val = np.mean(list(precision.values()))
63
+ recall_val = np.mean(list(recall.values()))
64
+ f1_val = np.mean(list(f1.values()))
65
+
66
+ elif average == 'weighted':
67
+ weights = np.array([np.sum(y_test_d == c) for c in classes])
68
+ weights = weights / np.sum(weights)
69
+ precision_val = np.sum([weights[i] * precision[classes[i]] for i in range(len(classes))])
70
+ recall_val = np.sum([weights[i] * recall[classes[i]] for i in range(len(classes))])
71
+ f1_val = np.sum([weights[i] * f1[classes[i]] for i in range(len(classes))])
72
+
73
+ else:
74
+ raise ValueError("Invalid value for 'average'. Choose from 'micro', 'macro', 'weighted'.")
75
+
76
+ return precision_val, recall_val, f1_val
77
+
78
+
79
+ def roc_curve(y_true, y_score):
80
+ """
81
+ Compute Receiver Operating Characteristic (ROC) curve.
82
+
83
+ Parameters:
84
+ y_true : array, shape = [n_samples]
85
+ True binary labels in range {0, 1} or {-1, 1}.
86
+ y_score : array, shape = [n_samples]
87
+ Target scores, can either be probability estimates of the positive class,
88
+ confidence values, or non-thresholded measure of decisions (as returned
89
+ by decision_function on some classifiers).
90
+
91
+ Returns:
92
+ fpr : array, shape = [n]
93
+ Increasing false positive rates such that element i is the false positive rate
94
+ of predictions with score >= thresholds[i].
95
+ tpr : array, shape = [n]
96
+ Increasing true positive rates such that element i is the true positive rate
97
+ of predictions with score >= thresholds[i].
98
+ thresholds : array, shape = [n]
99
+ Decreasing thresholds on the decision function used to compute fpr and tpr.
100
+ """
101
+
102
+ y_true = np.asarray(y_true)
103
+ y_score = np.asarray(y_score)
104
+
105
+ if len(np.unique(y_true)) != 2:
106
+ raise ValueError("Only binary classification is supported.")
107
+
108
+
109
+ desc_score_indices = np.argsort(y_score, kind="mergesort")[::-1]
110
+ y_score = y_score[desc_score_indices]
111
+ y_true = y_true[desc_score_indices]
112
+
113
+
114
+ fpr = []
115
+ tpr = []
116
+ thresholds = []
117
+ n_pos = np.sum(y_true)
118
+ n_neg = len(y_true) - n_pos
119
+
120
+ tp = 0
121
+ fp = 0
122
+ prev_score = None
123
+
124
+
125
+ for i, score in enumerate(y_score):
126
+ if score != prev_score:
127
+ fpr.append(fp / n_neg)
128
+ tpr.append(tp / n_pos)
129
+ thresholds.append(score)
130
+ prev_score = score
131
+
132
+ if y_true[i] == 1:
133
+ tp += 1
134
+ else:
135
+ fp += 1
136
+
137
+ fpr.append(fp / n_neg)
138
+ tpr.append(tp / n_pos)
139
+ thresholds.append(score)
140
+
141
+ return np.array(fpr), np.array(tpr), np.array(thresholds)
142
+
143
+
144
+ def confusion_matrix(y_true, y_pred, class_count):
145
+ """
146
+ Computes confusion matrix.
147
+
148
+ Args:
149
+ y_true (numpy.ndarray): True class labels (1D array).
150
+ y_pred (numpy.ndarray): Predicted class labels (1D array).
151
+ num_classes (int): Number of classes.
152
+
153
+ Returns:
154
+ numpy.ndarray: Confusion matrix of shape (num_classes, num_classes).
155
+ """
156
+ confusion = np.zeros((class_count, class_count), dtype=int)
157
+
158
+ for i in range(len(y_true)):
159
+ true_label = y_true[i]
160
+ pred_label = y_pred[i]
161
+ confusion[true_label, pred_label] += 1
162
+
163
+ return confusion
164
+
165
+
166
+ def pca(X, n_components):
167
+ """
168
+
169
+ Parameters:
170
+ X (numpy array): (n_samples, n_features)
171
+ n_components (int):
172
+
173
+ Returns:
174
+ X_reduced (numpy array): (n_samples, n_components)
175
+ """
176
+
177
+ X_meaned = X - np.mean(X, axis=0)
178
+
179
+ covariance_matrix = np.cov(X_meaned, rowvar=False)
180
+
181
+ eigenvalues, eigenvectors = np.linalg.eigh(covariance_matrix)
182
+
183
+ sorted_index = np.argsort(eigenvalues)[::-1]
184
+ sorted_eigenvectors = eigenvectors[:, sorted_index]
185
+
186
+ eigenvectors_subset = sorted_eigenvectors[:, :n_components]
187
+
188
+ X_reduced = np.dot(X_meaned, eigenvectors_subset)
189
+
190
+ return X_reduced
@@ -0,0 +1,165 @@
1
+ import cupy as cp
2
+ from .data_operations_cuda import decode_one_hot
3
+
4
+ def metrics(y_ts, test_preds, average='weighted'):
5
+ y_test_d = cp.array(decode_one_hot(y_ts))
6
+ y_pred = cp.array(test_preds)
7
+
8
+ if y_test_d.ndim > 1:
9
+ y_test_d = y_test_d.ravel()
10
+ if y_pred.ndim > 1:
11
+ y_pred = y_pred.ravel()
12
+
13
+ classes = cp.unique(cp.concatenate((y_test_d, y_pred)))
14
+ tp = cp.zeros(len(classes), dtype=cp.int32)
15
+ fp = cp.zeros(len(classes), dtype=cp.int32)
16
+ fn = cp.zeros(len(classes), dtype=cp.int32)
17
+
18
+ for i, c in enumerate(classes):
19
+ tp[i] = cp.sum((y_test_d == c) & (y_pred == c))
20
+ fp[i] = cp.sum((y_test_d != c) & (y_pred == c))
21
+ fn[i] = cp.sum((y_test_d == c) & (y_pred != c))
22
+
23
+ precision = tp / (tp + fp + 1e-10)
24
+ recall = tp / (tp + fn + 1e-10)
25
+ f1 = 2 * (precision * recall) / (precision + recall + 1e-10)
26
+
27
+ if average == 'micro':
28
+ tp_sum = cp.sum(tp)
29
+ fp_sum = cp.sum(fp)
30
+ fn_sum = cp.sum(fn)
31
+ precision_val = tp_sum / (tp_sum + fp_sum + 1e-10)
32
+ recall_val = tp_sum / (tp_sum + fn_sum + 1e-10)
33
+ f1_val = 2 * (precision_val * recall_val) / (precision_val + recall_val + 1e-10)
34
+
35
+ elif average == 'macro':
36
+ precision_val = cp.mean(precision)
37
+ recall_val = cp.mean(recall)
38
+ f1_val = cp.mean(f1)
39
+
40
+ elif average == 'weighted':
41
+ weights = cp.array([cp.sum(y_test_d == c) for c in classes])
42
+ weights = weights / cp.sum(weights)
43
+ precision_val = cp.sum(weights * precision)
44
+ recall_val = cp.sum(weights * recall)
45
+ f1_val = cp.sum(weights * f1)
46
+
47
+ else:
48
+ raise ValueError("Invalid value for 'average'. Choose from 'micro', 'macro', 'weighted'.")
49
+
50
+ return precision_val.item(), recall_val.item(), f1_val.item()
51
+
52
+
53
+
54
+ def roc_curve(y_true, y_score):
55
+ """
56
+ Compute Receiver Operating Characteristic (ROC) curve.
57
+
58
+ Parameters:
59
+ y_true : array, shape = [n_samples]
60
+ True binary labels in range {0, 1} or {-1, 1}.
61
+ y_score : array, shape = [n_samples]
62
+ Target scores, can either be probability estimates of the positive class,
63
+ confidence values, or non-thresholded measure of decisions (as returned
64
+ by decision_function on some classifiers).
65
+
66
+ Returns:
67
+ fpr : array, shape = [n]
68
+ Increasing false positive rates such that element i is the false positive rate
69
+ of predictions with score >= thresholds[i].
70
+ tpr : array, shape = [n]
71
+ Increasing true positive rates such that element i is the true positive rate
72
+ of predictions with score >= thresholds[i].
73
+ thresholds : array, shape = [n]
74
+ Decreasing thresholds on the decision function used to compute fpr and tpr.
75
+ """
76
+
77
+ y_true = cp.asarray(y_true)
78
+ y_score = cp.asarray(y_score)
79
+
80
+ if len(cp.unique(y_true)) != 2:
81
+ raise ValueError("Only binary classification is supported.")
82
+
83
+
84
+ desc_score_indices = cp.argsort(y_score, kind="mergesort")[::-1]
85
+ y_score = y_score[desc_score_indices]
86
+ y_true = y_true[desc_score_indices]
87
+
88
+
89
+ fpr = []
90
+ tpr = []
91
+ thresholds = []
92
+ n_pos = cp.sum(y_true)
93
+ n_neg = len(y_true) - n_pos
94
+
95
+ tp = 0
96
+ fp = 0
97
+ prev_score = None
98
+
99
+
100
+ for i, score in enumerate(y_score):
101
+ if score != prev_score:
102
+ fpr.append(fp / n_neg)
103
+ tpr.append(tp / n_pos)
104
+ thresholds.append(score)
105
+ prev_score = score
106
+
107
+ if y_true[i] == 1:
108
+ tp += 1
109
+ else:
110
+ fp += 1
111
+
112
+ fpr.append(fp / n_neg)
113
+ tpr.append(tp / n_pos)
114
+ thresholds.append(score)
115
+
116
+ return cp.array(fpr), cp.array(tpr), cp.array(thresholds)
117
+
118
+
119
+ def confusion_matrix(y_true, y_pred, class_count):
120
+ """
121
+ Computes confusion matrix.
122
+
123
+ Args:
124
+ y_true (numpy.ndarray): True class labels (1D array).
125
+ y_pred (numpy.ndarray): Predicted class labels (1D array).
126
+ num_classes (int): Number of classes.
127
+
128
+ Returns:
129
+ numpy.ndarray: Confusion matrix of shape (num_classes, num_classes).
130
+ """
131
+ confusion = cp.zeros((class_count, class_count), dtype=int)
132
+
133
+ for i in range(len(y_true)):
134
+ true_label = y_true[i]
135
+ pred_label = y_pred[i]
136
+ confusion[true_label, pred_label] += 1
137
+
138
+ return confusion
139
+
140
+
141
+ def pca(X, n_components):
142
+ """
143
+
144
+ Parameters:
145
+ X (numpy array): (n_samples, n_features)
146
+ n_components (int):
147
+
148
+ Returns:
149
+ X_reduced (numpy array): (n_samples, n_components)
150
+ """
151
+
152
+ X_meaned = X - cp.mean(X, axis=0)
153
+
154
+ covariance_matrix = cp.cov(X_meaned, rowvar=False)
155
+
156
+ eigenvalues, eigenvectors = cp.linalg.eigh(covariance_matrix)
157
+
158
+ sorted_index = cp.argsort(eigenvalues)[::-1]
159
+ sorted_eigenvectors = eigenvectors[:, sorted_index]
160
+
161
+ eigenvectors_subset = sorted_eigenvectors[:, :n_components]
162
+
163
+ X_reduced = cp.dot(X_meaned, eigenvectors_subset)
164
+
165
+ return X_reduced