scikit-survival 0.23.1__cp313-cp313-win_amd64.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.
Files changed (55) hide show
  1. scikit_survival-0.23.1.dist-info/COPYING +674 -0
  2. scikit_survival-0.23.1.dist-info/METADATA +888 -0
  3. scikit_survival-0.23.1.dist-info/RECORD +55 -0
  4. scikit_survival-0.23.1.dist-info/WHEEL +5 -0
  5. scikit_survival-0.23.1.dist-info/top_level.txt +1 -0
  6. sksurv/__init__.py +138 -0
  7. sksurv/base.py +103 -0
  8. sksurv/bintrees/__init__.py +15 -0
  9. sksurv/bintrees/_binarytrees.cp313-win_amd64.pyd +0 -0
  10. sksurv/column.py +201 -0
  11. sksurv/compare.py +123 -0
  12. sksurv/datasets/__init__.py +10 -0
  13. sksurv/datasets/base.py +436 -0
  14. sksurv/datasets/data/GBSG2.arff +700 -0
  15. sksurv/datasets/data/actg320.arff +1169 -0
  16. sksurv/datasets/data/breast_cancer_GSE7390-metastasis.arff +283 -0
  17. sksurv/datasets/data/flchain.arff +7887 -0
  18. sksurv/datasets/data/veteran.arff +148 -0
  19. sksurv/datasets/data/whas500.arff +520 -0
  20. sksurv/ensemble/__init__.py +2 -0
  21. sksurv/ensemble/_coxph_loss.cp313-win_amd64.pyd +0 -0
  22. sksurv/ensemble/boosting.py +1610 -0
  23. sksurv/ensemble/forest.py +947 -0
  24. sksurv/ensemble/survival_loss.py +151 -0
  25. sksurv/exceptions.py +18 -0
  26. sksurv/functions.py +114 -0
  27. sksurv/io/__init__.py +2 -0
  28. sksurv/io/arffread.py +58 -0
  29. sksurv/io/arffwrite.py +145 -0
  30. sksurv/kernels/__init__.py +1 -0
  31. sksurv/kernels/_clinical_kernel.cp313-win_amd64.pyd +0 -0
  32. sksurv/kernels/clinical.py +328 -0
  33. sksurv/linear_model/__init__.py +3 -0
  34. sksurv/linear_model/_coxnet.cp313-win_amd64.pyd +0 -0
  35. sksurv/linear_model/aft.py +205 -0
  36. sksurv/linear_model/coxnet.py +543 -0
  37. sksurv/linear_model/coxph.py +618 -0
  38. sksurv/meta/__init__.py +4 -0
  39. sksurv/meta/base.py +35 -0
  40. sksurv/meta/ensemble_selection.py +642 -0
  41. sksurv/meta/stacking.py +349 -0
  42. sksurv/metrics.py +996 -0
  43. sksurv/nonparametric.py +588 -0
  44. sksurv/preprocessing.py +155 -0
  45. sksurv/svm/__init__.py +11 -0
  46. sksurv/svm/_minlip.cp313-win_amd64.pyd +0 -0
  47. sksurv/svm/_prsvm.cp313-win_amd64.pyd +0 -0
  48. sksurv/svm/minlip.py +606 -0
  49. sksurv/svm/naive_survival_svm.py +221 -0
  50. sksurv/svm/survival_svm.py +1228 -0
  51. sksurv/testing.py +108 -0
  52. sksurv/tree/__init__.py +1 -0
  53. sksurv/tree/_criterion.cp313-win_amd64.pyd +0 -0
  54. sksurv/tree/tree.py +703 -0
  55. sksurv/util.py +333 -0
@@ -0,0 +1,349 @@
1
+ # This program is free software: you can redistribute it and/or modify
2
+ # it under the terms of the GNU General Public License as published by
3
+ # the Free Software Foundation, either version 3 of the License, or
4
+ # (at your option) any later version.
5
+ #
6
+ # This program is distributed in the hope that it will be useful,
7
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
8
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
+ # GNU General Public License for more details.
10
+ #
11
+ # You should have received a copy of the GNU General Public License
12
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
13
+ import numpy as np
14
+ from sklearn.base import MetaEstimatorMixin, clone
15
+ from sklearn.utils._param_validation import HasMethods
16
+ from sklearn.utils.metaestimators import _BaseComposition, available_if
17
+
18
+ from ..base import SurvivalAnalysisMixin
19
+ from ..util import property_available_if
20
+
21
+
22
+ def _meta_estimator_has(attr):
23
+ """Check that meta_estimator has `attr`.
24
+
25
+ Used together with `available_if`."""
26
+
27
+ def check(self):
28
+ # raise original `AttributeError` if `attr` does not exist
29
+ getattr(self.meta_estimator, attr)
30
+ return True
31
+
32
+ return check
33
+
34
+
35
+ class Stacking(MetaEstimatorMixin, SurvivalAnalysisMixin, _BaseComposition):
36
+ """Meta estimator that combines multiple base learners.
37
+
38
+ By default, base estimators' output corresponds to the array returned
39
+ by `predict_proba`. If `predict_proba` is not available or `probabilities = False`,
40
+ the output of `predict` is used.
41
+
42
+ Parameters
43
+ ----------
44
+ meta_estimator : instance of estimator
45
+ The estimator that is used to combine the output of different
46
+ base estimators.
47
+
48
+ base_estimators : list
49
+ List of (name, estimator) tuples (implementing fit/predict) that are
50
+ part of the ensemble.
51
+
52
+ probabilities : bool, optional, default: True
53
+ Whether to allow using `predict_proba` method of base learners, if available.
54
+
55
+ Attributes
56
+ ----------
57
+ estimators_ : list of estimators
58
+ The elements of the estimators parameter, having been fitted on the
59
+ training data.
60
+
61
+ named_estimators_ : dict
62
+ Attribute to access any fitted sub-estimators by name.
63
+
64
+ final_estimator_ : estimator
65
+ The estimator which combines the output of `estimators_`.
66
+
67
+ n_features_in_ : int
68
+ Number of features seen during ``fit``.
69
+
70
+ feature_names_in_ : ndarray of shape (`n_features_in_`,)
71
+ Names of features seen during ``fit``. Defined only when `X`
72
+ has feature names that are all strings.
73
+
74
+ unique_times_ : array of shape = (n_unique_times,)
75
+ Unique time points.
76
+ """
77
+
78
+ _parameter_constraints = {
79
+ "meta_estimator": [HasMethods(["fit"])],
80
+ "base_estimators": [list],
81
+ "probabilities": ["boolean"],
82
+ }
83
+
84
+ def __init__(self, meta_estimator, base_estimators, *, probabilities=True):
85
+ self.meta_estimator = meta_estimator
86
+ self.base_estimators = base_estimators
87
+ self.probabilities = probabilities
88
+
89
+ self._extra_params = ["meta_estimator", "base_estimators", "probabilities"]
90
+
91
+ def _validate_estimators(self):
92
+ names, estimators = zip(*self.base_estimators)
93
+ if len(set(names)) != len(self.base_estimators):
94
+ raise ValueError(f"Names provided are not unique: {names}")
95
+
96
+ for t in estimators:
97
+ if not hasattr(t, "fit") or not (hasattr(t, "predict") or hasattr(t, "predict_proba")):
98
+ raise TypeError(
99
+ "All base estimators should implement "
100
+ "fit and predict/predict_proba"
101
+ f" {t!s} (type {type(t)}) doesn't)"
102
+ )
103
+
104
+ def set_params(self, **params):
105
+ """
106
+ Set the parameters of an estimator from the ensemble.
107
+
108
+ Valid parameter keys can be listed with `get_params()`. Note that you
109
+ can directly set the parameters of the estimators contained in
110
+ `estimators`.
111
+
112
+ Parameters
113
+ ----------
114
+ **params : keyword arguments
115
+ Specific parameters using e.g.
116
+ `set_params(parameter_name=new_value)`. In addition, to setting the
117
+ parameters of the estimator, the individual estimator of the
118
+ estimators can also be set, or can be removed by setting them to
119
+ 'drop'.
120
+
121
+ Returns
122
+ -------
123
+ self : object
124
+ Estimator instance.
125
+ """
126
+ super()._set_params("base_estimators", **params)
127
+ return self
128
+
129
+ def get_params(self, deep=True):
130
+ """
131
+ Get the parameters of an estimator from the ensemble.
132
+
133
+ Returns the parameters given in the constructor as well as the
134
+ estimators contained within the `estimators` parameter.
135
+
136
+ Parameters
137
+ ----------
138
+ deep : bool, default=True
139
+ Setting it to True gets the various estimators and the parameters
140
+ of the estimators as well.
141
+
142
+ Returns
143
+ -------
144
+ params : dict
145
+ Parameter and estimator names mapped to their values or parameter
146
+ names mapped to their values.
147
+ """
148
+ return super()._get_params("base_estimators", deep=deep)
149
+
150
+ def _split_fit_params(self, fit_params):
151
+ fit_params_steps = {step: {} for step, _ in self.base_estimators}
152
+ for pname, pval in fit_params.items():
153
+ step, param = pname.split("__", 1)
154
+ fit_params_steps[step][param] = pval
155
+ return fit_params_steps
156
+
157
+ def _fit_estimators(self, X, y=None, **fit_params):
158
+ if hasattr(self, "feature_names_in_"):
159
+ # Delete the attribute when the estimator is fitted on a new dataset
160
+ # that has no feature names.
161
+ delattr(self, "feature_names_in_")
162
+
163
+ fit_params_steps = self._split_fit_params(fit_params)
164
+ names = []
165
+ estimators = []
166
+ for name, estimator in self.base_estimators:
167
+ est = clone(estimator).fit(X, y, **fit_params_steps[name])
168
+
169
+ if hasattr(est, "n_features_in_"):
170
+ self.n_features_in_ = est.n_features_in_
171
+ if hasattr(est, "feature_names_in_"):
172
+ self.feature_names_in_ = est.feature_names_in_
173
+
174
+ estimators.append(est)
175
+ names.append(name)
176
+
177
+ self.named_estimators = dict(zip(names, estimators))
178
+ self.estimators_ = estimators
179
+
180
+ def _predict_estimators(self, X):
181
+ Xt = None
182
+ start = 0
183
+ for estimator in self.estimators_:
184
+ if self.probabilities and hasattr(estimator, "predict_proba"):
185
+ p = estimator.predict_proba(X)
186
+ else:
187
+ p = estimator.predict(X)
188
+
189
+ if p.ndim == 1:
190
+ p = p[:, np.newaxis]
191
+
192
+ if Xt is None:
193
+ # assume that prediction array has the same size for all base learners
194
+ n_classes = p.shape[1]
195
+ Xt = np.empty((p.shape[0], n_classes * len(self.base_estimators)), order="F")
196
+ Xt[:, slice(start, start + n_classes)] = p
197
+ start += n_classes
198
+
199
+ return Xt
200
+
201
+ def __len__(self):
202
+ return len(self.base_estimators)
203
+
204
+ def fit(self, X, y=None, **fit_params):
205
+ """Fit base estimators.
206
+
207
+ Parameters
208
+ ----------
209
+ X : array-like, shape = (n_samples, n_features)
210
+ Training data.
211
+
212
+ y : array-like, optional
213
+ Target data if base estimators are supervised.
214
+
215
+ Returns
216
+ -------
217
+ self
218
+ """
219
+ self._validate_params()
220
+ self._validate_estimators()
221
+ self._fit_estimators(X, y, **fit_params)
222
+ Xt = self._predict_estimators(X)
223
+ self.final_estimator_ = self.meta_estimator.fit(Xt, y)
224
+
225
+ return self
226
+
227
+ @available_if(_meta_estimator_has("predict"))
228
+ def predict(self, X):
229
+ """Perform prediction.
230
+
231
+ Only available of the meta estimator has a predict method.
232
+
233
+ Parameters
234
+ ----------
235
+ X : array-like, shape = (n_samples, n_features)
236
+ Data with samples to predict.
237
+
238
+ Returns
239
+ -------
240
+ prediction : array, shape = (n_samples, n_dim)
241
+ Prediction of meta estimator that combines
242
+ predictions of base estimators. `n_dim` depends
243
+ on the return value of meta estimator's `predict`
244
+ method.
245
+ """
246
+ Xt = self._predict_estimators(X)
247
+ return self.final_estimator_.predict(Xt)
248
+
249
+ @available_if(_meta_estimator_has("predict_proba"))
250
+ def predict_proba(self, X):
251
+ """Perform prediction.
252
+
253
+ Only available if the meta estimator has a predict_proba method.
254
+
255
+ Parameters
256
+ ----------
257
+ X : array-like, shape = (n_samples, n_features)
258
+ Data with samples to predict.
259
+
260
+ Returns
261
+ -------
262
+ prediction : ndarray, shape = (n_samples, n_dim)
263
+ Prediction of meta estimator that combines
264
+ predictions of base estimators. `n_dim` depends
265
+ on the return value of meta estimator's `predict`
266
+ method.
267
+ """
268
+ Xt = self._predict_estimators(X)
269
+ return self.final_estimator_.predict_proba(Xt)
270
+
271
+ @available_if(_meta_estimator_has("predict_log_proba"))
272
+ def predict_log_proba(self, X):
273
+ """Perform prediction.
274
+
275
+ Only available if the meta estimator has a predict_log_proba method.
276
+
277
+ Parameters
278
+ ----------
279
+ X : array-like, shape = (n_samples, n_features)
280
+ Data with samples to predict.
281
+
282
+ Returns
283
+ -------
284
+ prediction : ndarray, shape = (n_samples, n_dim)
285
+ Prediction of meta estimator that combines
286
+ predictions of base estimators. `n_dim` depends
287
+ on the return value of meta estimator's `predict`
288
+ method.
289
+ """
290
+ Xt = self._predict_estimators(X)
291
+ return self.final_estimator_.predict_log_proba(Xt)
292
+
293
+ @property_available_if(_meta_estimator_has("unique_times_"))
294
+ def unique_times_(self):
295
+ return self.meta_estimator.unique_times_
296
+
297
+ @available_if(_meta_estimator_has("predict_cumulative_hazard_function"))
298
+ def predict_cumulative_hazard_function(self, X, return_array=False):
299
+ """Perform prediction.
300
+
301
+ Only available if the meta estimator has a predict_cumulative_hazard_function method.
302
+
303
+ Parameters
304
+ ----------
305
+ X : array-like, shape = (n_samples, n_features)
306
+ Data with samples to predict.
307
+
308
+ return_array : boolean, default: False
309
+ If set, return an array with the cumulative hazard rate
310
+ for each `self.unique_times_`, otherwise an array of
311
+ :class:`sksurv.functions.StepFunction`.
312
+
313
+ Returns
314
+ -------
315
+ cum_hazard : ndarray
316
+ If `return_array` is set, an array with the cumulative hazard rate
317
+ for each `self.unique_times_`, otherwise an array of length `n_samples`
318
+ of :class:`sksurv.functions.StepFunction` instances will be returned.
319
+ """
320
+ Xt = self._predict_estimators(X)
321
+ return self.final_estimator_.predict_cumulative_hazard_function(Xt, return_array)
322
+
323
+ @available_if(_meta_estimator_has("predict_survival_function"))
324
+ def predict_survival_function(self, X, return_array=False):
325
+ """Perform prediction.
326
+
327
+ Only available if the meta estimator has a predict_survival_function method.
328
+
329
+ Parameters
330
+ ----------
331
+ X : array-like, shape = (n_samples, n_features)
332
+ Data with samples to predict.
333
+
334
+ Returns
335
+ -------
336
+ survival : ndarray
337
+ If `return_array` is set, an array with the probability of
338
+ survival for each `self.unique_times_`, otherwise an array of
339
+ length `n_samples` of :class:`sksurv.functions.StepFunction`
340
+ instances will be returned.
341
+
342
+ return_array : boolean, default: False
343
+ If set, return an array with the probability
344
+ of survival for each `self.unique_times_`,
345
+ otherwise an array of :class:`sksurv.functions.StepFunction`.
346
+
347
+ """
348
+ Xt = self._predict_estimators(X)
349
+ return self.final_estimator_.predict_survival_function(Xt, return_array)