linearrf 1.2.2__tar.gz → 1.2.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: linearrf
3
- Version: 1.2.2
3
+ Version: 1.2.4
4
4
  Summary: A python libary to build Random Forests with Linear Models at the leaves.
5
5
  Author-email: Marian Biermann <marianbiermann@gmx.de>
6
6
  Project-URL: homepage, https://github.com/marianbiermann/lrf
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "linearrf"
7
- version = "1.2.2"
7
+ version = "1.2.4"
8
8
  description = "A python libary to build Random Forests with Linear Models at the leaves."
9
9
  readme = "README.md"
10
10
  authors = [{ name = "Marian Biermann", email = "marianbiermann@gmx.de" }]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: linearrf
3
- Version: 1.2.2
3
+ Version: 1.2.4
4
4
  Summary: A python libary to build Random Forests with Linear Models at the leaves.
5
5
  Author-email: Marian Biermann <marianbiermann@gmx.de>
6
6
  Project-URL: homepage, https://github.com/marianbiermann/lrf
@@ -82,43 +82,61 @@ def neg_mcc(y_true: np.ndarray, y_pred: np.ndarray):
82
82
 
83
83
 
84
84
  def neg_roc_auc(y_true: np.ndarray, y_pred: np.ndarray):
85
- # Rank-based ROC AUC in O(n log n). Sort by descending score and sweep:
86
- # each step adds the next sample to "predicted positive", so cumulative
87
- # positives / negatives give TPR / FPR at every distinct cutoff.
85
+ # Rank-based ROC AUC in O(n log n). Sort by descending score, then evaluate
86
+ # TPR/FPR only at distinct-score endpoints collapsing each tied group
87
+ # into a single point. Without this, tied predictions produce results that
88
+ # depend on the (arbitrary) within-tie sort order.
88
89
  y_true = np.asarray(y_true)
89
90
  y_pred = np.asarray(y_pred)
90
91
 
91
92
  order = np.argsort(-y_pred, kind='stable')
92
93
  y_sorted = y_true[order].astype(np.float64)
94
+ pred_sorted = y_pred[order]
93
95
 
94
- positives = y_sorted.sum()
95
- negatives = y_sorted.size - positives
96
+ tps_full = np.cumsum(y_sorted)
97
+ fps_full = np.arange(1, y_sorted.size + 1, dtype=np.float64) - tps_full
98
+
99
+ # End of each tied group: positions where the next prediction differs,
100
+ # plus the very last position.
101
+ endpoints = np.concatenate((np.where(np.diff(pred_sorted))[0],
102
+ [y_sorted.size - 1]))
103
+ tps = tps_full[endpoints]
104
+ fps = fps_full[endpoints]
105
+
106
+ positives, negatives = tps[-1], fps[-1]
96
107
  if positives == 0 or negatives == 0:
97
108
  return 0.0
98
109
 
99
- tpr = np.concatenate(([0.0], np.cumsum(y_sorted) / positives))
100
- fpr = np.concatenate(([0.0], np.cumsum(1.0 - y_sorted) / negatives))
110
+ tpr = np.concatenate(([0.0], tps / positives))
111
+ fpr = np.concatenate(([0.0], fps / negatives))
101
112
 
102
113
  # negated so the caller minimizes AUC (matches the previous convention)
103
114
  return -_trapz(tpr, fpr)
104
115
 
105
116
 
106
117
  def neg_pr_auc(y_true: np.ndarray, y_pred: np.ndarray):
107
- # Rank-based PR AUC in O(n log n) same idea as neg_roc_auc above.
118
+ # Rank-based PR AUC in O(n log n). Same tie-collapsing as neg_roc_auc.
108
119
  y_true = np.asarray(y_true)
109
120
  y_pred = np.asarray(y_pred)
110
121
 
111
122
  order = np.argsort(-y_pred, kind='stable')
112
123
  y_sorted = y_true[order].astype(np.float64)
124
+ pred_sorted = y_pred[order]
125
+
126
+ tps_full = np.cumsum(y_sorted)
127
+ fps_full = np.arange(1, y_sorted.size + 1, dtype=np.float64) - tps_full
128
+
129
+ endpoints = np.concatenate((np.where(np.diff(pred_sorted))[0],
130
+ [y_sorted.size - 1]))
131
+ tps = tps_full[endpoints]
132
+ fps = fps_full[endpoints]
113
133
 
114
- positives = y_sorted.sum()
134
+ positives = tps[-1]
115
135
  if positives == 0:
116
136
  return 0.0
117
137
 
118
- pos_cum = np.cumsum(y_sorted)
119
- ranks = np.arange(1, y_sorted.size + 1, dtype=np.float64)
120
- precision = pos_cum / ranks
121
- recall = pos_cum / positives
138
+ precision = tps / (tps + fps)
139
+ recall = tps / positives
122
140
 
123
141
  return -_trapz(precision, recall)
124
142
 
@@ -21,6 +21,9 @@ class LRFRegressor(_LinearRandomForest):
21
21
  self.preprocessing = preprocessing
22
22
  self._estimator_type = 'regressor'
23
23
 
24
+ if criterion not in ('mse', 'rmse', 'mae', 'mape', 'wape', 'neg_explained_variance', 'neg_r2'):
25
+ print(' Metric "{}" is not implemented, MSE is used instead.'.format(criterion))
26
+
24
27
  if linear_model is None:
25
28
  linear_model = Regressor(alpha=self.alpha, preprocessing=self.preprocessing, intercept_in_input=True)
26
29
  else:
@@ -83,7 +86,6 @@ class LRFRegressor(_LinearRandomForest):
83
86
  elif self.criterion == 'neg_r2':
84
87
  val = neg_r2(y_true=y_true, y_pred=y_pred)
85
88
  else:
86
- print(' Metric "{}" is not implemented, MSE is used instead.'.format(self.criterion))
87
89
  val = mse(y_true=y_true, y_pred=y_pred)
88
90
 
89
91
  return val
@@ -114,6 +116,10 @@ class LRFClassifier(_LinearRandomForest):
114
116
  self.preprocessing = preprocessing
115
117
  self._estimator_type = 'classifier'
116
118
 
119
+ if criterion not in ('hamming', 'cross_entropy', 'neg_mcc', 'neg_roc_auc', 'neg_pr_auc'):
120
+ print(' Metric "{}" is not implemented, negative Matthews Correlation Coefficient is used instead.'
121
+ .format(criterion))
122
+
117
123
  if linear_model is None:
118
124
  linear_model = Classifier(C=self.C, preprocessing=self.preprocessing, intercept_in_input=True)
119
125
  else:
@@ -157,7 +163,8 @@ class LRFClassifier(_LinearRandomForest):
157
163
  x = x.to_numpy()
158
164
 
159
165
  # add intercept here and not inside linear model for performance reasons
160
- x = np.insert(x, 0, 1, axis=1)
166
+ if self.linear_model is None:
167
+ x = np.insert(x, 0, 1, axis=1)
161
168
 
162
169
  # add columns with row index for sorting after multiprocessing
163
170
  x = np.insert(x, 0, np.arange(x.shape[0]), axis=1)
@@ -213,8 +220,6 @@ class LRFClassifier(_LinearRandomForest):
213
220
  elif self.criterion == 'neg_roc_auc':
214
221
  val = neg_roc_auc(y_true=y_true, y_pred=y_pred)
215
222
  else:
216
- print(' Metric "{}" is not implemented, the negative Matthews Correlation Coefficient is used '
217
- 'instead.'.format(self.criterion))
218
223
  val = neg_mcc(y_true=y_true, y_pred=y_pred)
219
224
 
220
225
  return val
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes