scikit-survival 0.26.0__cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.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.
- scikit_survival-0.26.0.dist-info/METADATA +185 -0
- scikit_survival-0.26.0.dist-info/RECORD +58 -0
- scikit_survival-0.26.0.dist-info/WHEEL +6 -0
- scikit_survival-0.26.0.dist-info/licenses/COPYING +674 -0
- scikit_survival-0.26.0.dist-info/top_level.txt +1 -0
- sksurv/__init__.py +183 -0
- sksurv/base.py +115 -0
- sksurv/bintrees/__init__.py +15 -0
- sksurv/bintrees/_binarytrees.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/column.py +204 -0
- sksurv/compare.py +123 -0
- sksurv/datasets/__init__.py +12 -0
- sksurv/datasets/base.py +614 -0
- sksurv/datasets/data/GBSG2.arff +700 -0
- sksurv/datasets/data/actg320.arff +1169 -0
- sksurv/datasets/data/bmt.arff +46 -0
- sksurv/datasets/data/breast_cancer_GSE7390-metastasis.arff +283 -0
- sksurv/datasets/data/cgvhd.arff +118 -0
- sksurv/datasets/data/flchain.arff +7887 -0
- sksurv/datasets/data/veteran.arff +148 -0
- sksurv/datasets/data/whas500.arff +520 -0
- sksurv/docstrings.py +99 -0
- sksurv/ensemble/__init__.py +2 -0
- sksurv/ensemble/_coxph_loss.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/ensemble/boosting.py +1564 -0
- sksurv/ensemble/forest.py +902 -0
- sksurv/ensemble/survival_loss.py +151 -0
- sksurv/exceptions.py +18 -0
- sksurv/functions.py +114 -0
- sksurv/io/__init__.py +2 -0
- sksurv/io/arffread.py +91 -0
- sksurv/io/arffwrite.py +181 -0
- sksurv/kernels/__init__.py +1 -0
- sksurv/kernels/_clinical_kernel.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/kernels/clinical.py +348 -0
- sksurv/linear_model/__init__.py +3 -0
- sksurv/linear_model/_coxnet.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/linear_model/aft.py +208 -0
- sksurv/linear_model/coxnet.py +592 -0
- sksurv/linear_model/coxph.py +637 -0
- sksurv/meta/__init__.py +4 -0
- sksurv/meta/base.py +35 -0
- sksurv/meta/ensemble_selection.py +724 -0
- sksurv/meta/stacking.py +370 -0
- sksurv/metrics.py +1028 -0
- sksurv/nonparametric.py +911 -0
- sksurv/preprocessing.py +195 -0
- sksurv/svm/__init__.py +11 -0
- sksurv/svm/_minlip.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/svm/_prsvm.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/svm/minlip.py +695 -0
- sksurv/svm/naive_survival_svm.py +249 -0
- sksurv/svm/survival_svm.py +1236 -0
- sksurv/testing.py +155 -0
- sksurv/tree/__init__.py +1 -0
- sksurv/tree/_criterion.cpython-312-x86_64-linux-gnu.so +0 -0
- sksurv/tree/tree.py +790 -0
- sksurv/util.py +416 -0
sksurv/meta/stacking.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
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, 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_ : ndarray, 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 the number of base estimators."""
|
|
203
|
+
return len(self.base_estimators)
|
|
204
|
+
|
|
205
|
+
def fit(self, X, y=None, **fit_params):
|
|
206
|
+
"""Fit base estimators.
|
|
207
|
+
|
|
208
|
+
Parameters
|
|
209
|
+
----------
|
|
210
|
+
X : array-like, shape = (n_samples, n_features)
|
|
211
|
+
Training data.
|
|
212
|
+
|
|
213
|
+
y : array-like, shape = (n_samples,), optional
|
|
214
|
+
Target data if base estimators are supervised.
|
|
215
|
+
|
|
216
|
+
**fit_params : dict
|
|
217
|
+
Parameters passed to the ``fit`` method of each base estimator.
|
|
218
|
+
|
|
219
|
+
Returns
|
|
220
|
+
-------
|
|
221
|
+
self
|
|
222
|
+
"""
|
|
223
|
+
self._validate_params()
|
|
224
|
+
self._validate_estimators()
|
|
225
|
+
self._fit_estimators(X, y, **fit_params)
|
|
226
|
+
Xt = self._predict_estimators(X)
|
|
227
|
+
self.final_estimator_ = self.meta_estimator.fit(Xt, y)
|
|
228
|
+
|
|
229
|
+
return self
|
|
230
|
+
|
|
231
|
+
@available_if(_meta_estimator_has("predict"))
|
|
232
|
+
def predict(self, X):
|
|
233
|
+
"""Perform prediction.
|
|
234
|
+
|
|
235
|
+
Only available if the meta estimator has a ``predict`` method.
|
|
236
|
+
|
|
237
|
+
Parameters
|
|
238
|
+
----------
|
|
239
|
+
X : array-like, shape = (n_samples, n_features)
|
|
240
|
+
Data with samples to predict.
|
|
241
|
+
|
|
242
|
+
Returns
|
|
243
|
+
-------
|
|
244
|
+
prediction : ndarray, shape = (n_samples, n_dim)
|
|
245
|
+
Prediction of meta estimator that combines
|
|
246
|
+
predictions of base estimators. `n_dim` depends
|
|
247
|
+
on the return value of meta estimator's ``predict``
|
|
248
|
+
method.
|
|
249
|
+
"""
|
|
250
|
+
Xt = self._predict_estimators(X)
|
|
251
|
+
return self.final_estimator_.predict(Xt)
|
|
252
|
+
|
|
253
|
+
@available_if(_meta_estimator_has("predict_proba"))
|
|
254
|
+
def predict_proba(self, X):
|
|
255
|
+
"""Perform prediction.
|
|
256
|
+
|
|
257
|
+
Only available if the meta estimator has a ``predict_proba`` method.
|
|
258
|
+
|
|
259
|
+
Parameters
|
|
260
|
+
----------
|
|
261
|
+
X : array-like, shape = (n_samples, n_features)
|
|
262
|
+
Data with samples to predict.
|
|
263
|
+
|
|
264
|
+
Returns
|
|
265
|
+
-------
|
|
266
|
+
prediction : ndarray, shape = (n_samples, n_dim)
|
|
267
|
+
Prediction of meta estimator that combines
|
|
268
|
+
predictions of base estimators. `n_dim` depends
|
|
269
|
+
on the return value of meta estimator's `predict`
|
|
270
|
+
method.
|
|
271
|
+
"""
|
|
272
|
+
Xt = self._predict_estimators(X)
|
|
273
|
+
return self.final_estimator_.predict_proba(Xt)
|
|
274
|
+
|
|
275
|
+
@available_if(_meta_estimator_has("predict_log_proba"))
|
|
276
|
+
def predict_log_proba(self, X):
|
|
277
|
+
"""Perform prediction.
|
|
278
|
+
|
|
279
|
+
Only available if the meta estimator has a ``predict_log_proba`` method.
|
|
280
|
+
|
|
281
|
+
Parameters
|
|
282
|
+
----------
|
|
283
|
+
X : array-like, shape = (n_samples, n_features)
|
|
284
|
+
Data with samples to predict.
|
|
285
|
+
|
|
286
|
+
Returns
|
|
287
|
+
-------
|
|
288
|
+
prediction : ndarray, shape = (n_samples, n_dim)
|
|
289
|
+
Prediction of meta estimator that combines
|
|
290
|
+
predictions of base estimators. `n_dim` depends
|
|
291
|
+
on the return value of meta estimator's `predict`
|
|
292
|
+
method.
|
|
293
|
+
"""
|
|
294
|
+
Xt = self._predict_estimators(X)
|
|
295
|
+
return self.final_estimator_.predict_log_proba(Xt)
|
|
296
|
+
|
|
297
|
+
@property_available_if(_meta_estimator_has("unique_times_"))
|
|
298
|
+
def unique_times_(self):
|
|
299
|
+
return self.meta_estimator.unique_times_
|
|
300
|
+
|
|
301
|
+
@available_if(_meta_estimator_has("predict_cumulative_hazard_function"))
|
|
302
|
+
def predict_cumulative_hazard_function(self, X, return_array=False):
|
|
303
|
+
"""Perform prediction.
|
|
304
|
+
|
|
305
|
+
Only available if the meta estimator has a ``predict_cumulative_hazard_function`` method.
|
|
306
|
+
|
|
307
|
+
Parameters
|
|
308
|
+
----------
|
|
309
|
+
X : array-like, shape = (n_samples, n_features)
|
|
310
|
+
Data with samples to predict.
|
|
311
|
+
|
|
312
|
+
return_array : bool, default: False
|
|
313
|
+
Whether to return a single array of cumulative hazard values
|
|
314
|
+
or a list of step functions.
|
|
315
|
+
|
|
316
|
+
If `False`, a list of :class:`sksurv.functions.StepFunction`
|
|
317
|
+
objects is returned.
|
|
318
|
+
|
|
319
|
+
If `True`, a 2d-array of shape `(n_samples, n_unique_times)` is
|
|
320
|
+
returned, where `n_unique_times` is the number of unique
|
|
321
|
+
event times in the training data. Each row represents the cumulative
|
|
322
|
+
hazard function of an individual evaluated at `unique_times_`.
|
|
323
|
+
|
|
324
|
+
Returns
|
|
325
|
+
-------
|
|
326
|
+
cum_hazard : ndarray
|
|
327
|
+
If `return_array` is `False`, an array of `n_samples`
|
|
328
|
+
:class:`sksurv.functions.StepFunction` instances is returned.
|
|
329
|
+
|
|
330
|
+
If `return_array` is `True`, a numeric array of shape
|
|
331
|
+
`(n_samples, n_unique_times_)` is returned.
|
|
332
|
+
"""
|
|
333
|
+
Xt = self._predict_estimators(X)
|
|
334
|
+
return self.final_estimator_.predict_cumulative_hazard_function(Xt, return_array)
|
|
335
|
+
|
|
336
|
+
@available_if(_meta_estimator_has("predict_survival_function"))
|
|
337
|
+
def predict_survival_function(self, X, return_array=False):
|
|
338
|
+
"""Perform prediction.
|
|
339
|
+
|
|
340
|
+
Only available if the meta estimator has a ``predict_survival_function`` method.
|
|
341
|
+
|
|
342
|
+
Parameters
|
|
343
|
+
----------
|
|
344
|
+
X : array-like, shape = (n_samples, n_features)
|
|
345
|
+
Data with samples to predict.
|
|
346
|
+
|
|
347
|
+
return_array : bool, default: False
|
|
348
|
+
Whether to return a single array of survival probabilities
|
|
349
|
+
or a list of step functions.
|
|
350
|
+
|
|
351
|
+
If `False`, a list of :class:`sksurv.functions.StepFunction`
|
|
352
|
+
objects is returned.
|
|
353
|
+
|
|
354
|
+
If `True`, a 2d-array of shape `(n_samples, n_unique_times)` is
|
|
355
|
+
returned, where `n_unique_times` is the number of unique
|
|
356
|
+
event times in the training data. Each row represents the survival
|
|
357
|
+
function of an individual evaluated at `unique_times_`.
|
|
358
|
+
|
|
359
|
+
Returns
|
|
360
|
+
-------
|
|
361
|
+
survival : ndarray
|
|
362
|
+
If `return_array` is `False`, an array of `n_samples`
|
|
363
|
+
:class:`sksurv.functions.StepFunction` instances is returned.
|
|
364
|
+
|
|
365
|
+
If `return_array` is `True`, a numeric array of shape
|
|
366
|
+
`(n_samples, n_unique_times_)` is returned.
|
|
367
|
+
|
|
368
|
+
"""
|
|
369
|
+
Xt = self._predict_estimators(X)
|
|
370
|
+
return self.final_estimator_.predict_survival_function(Xt, return_array)
|