sksurv.svm.NaiveSurvivalSVM#
- class sksurv.svm.NaiveSurvivalSVM(penalty='l2', loss='squared_hinge', *, dual=False, tol=0.0001, alpha=1.0, verbose=0, random_state=None, max_iter=1000)[source]#
Naive implementation of linear Survival Support Vector Machine.
This class uses a regular linear support vector classifier (liblinear) to implement a survival SVM. It constructs a new dataset by computing the difference between feature vectors of comparable pairs from the original data. This approach results in a space complexity of \(O(\text{n_samples}^2)\).
The optimization problem is formulated as:
\[ \begin{align}\begin{aligned}\begin{split}\min_{\mathbf{w}}\quad \frac{1}{2} \lVert \mathbf{w} \rVert_2^2 + \gamma \sum_{i = 1}^n \xi_i \\ \text{subject to}\quad \mathbf{w}^\top \mathbf{x}_i - \mathbf{w}^\top \mathbf{x}_j \geq 1 - \xi_{ij},\quad \forall (i, j) \in \mathcal{P}, \\ \xi_i \geq 0,\quad \forall (i, j) \in \mathcal{P}.\end{split}\\\mathcal{P} = \{ (i, j) \mid y_i > y_j \land \delta_j = 1 \}_{i,j=1,\dots,n}.\end{aligned}\end{align} \]See [1], [2] for further description.
- Parameters:
alpha (float, optional, default: 1.0) – Weight of penalizing the squared hinge loss in the objective function. Must be greater than 0.
loss ({'hinge', 'squared_hinge'}, optional,default: 'squared_hinge') – Specifies the loss function. ‘hinge’ is the standard SVM loss (used e.g. by the SVC class) while ‘squared_hinge’ is the square of the hinge loss.
penalty ({'l1', 'l2'}, optional,default: 'l2') – Specifies the norm used in the penalization. The ‘l2’ penalty is the standard used in SVC. The ‘l1’ leads to coef_ vectors that are sparse.
dual (bool, optional,default: True) – Select the algorithm to either solve the dual or primal optimization problem. Prefer dual=False when n_samples > n_features.
tol (float, optional, default: 1e-4) – Tolerance for stopping criteria.
verbose (int, optional, default: 0) – If
True, enable verbose output. Note that this setting takes advantage of a per-process runtime setting in liblinear that, if enabled, may not work properly in a multithreaded context.random_state (int,
numpy.random.RandomStateinstance, or None, optional, default: None) – Used to resolve ties in survival times. Pass an int for reproducible output across multiplefit()calls.max_iter (int, optional, default: 1000) – The maximum number of iterations taken for the solver to converge.
- n_iter_#
Number of iterations run by the optimization routine to fit the model.
- Type:
int
See also
sksurv.svm.FastSurvivalSVMAlternative implementation with reduced time complexity for training.
sksurv.svm.HingeLossSurvivalSVMNon-linear version of the naive survival SVM based on kernel functions.
References
- __init__(penalty='l2', loss='squared_hinge', *, dual=False, tol=0.0001, alpha=1.0, verbose=0, random_state=None, max_iter=1000)[source]#
Methods
__init__([penalty, loss, dual, tol, alpha, ...])Predict confidence scores for samples.
densify()Convert coefficient matrix to dense array format.
fit(X, y[, sample_weight])Build a survival support vector machine model from training data.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
predict(X)Predict risk scores.
score(X, y)Returns the concordance index of the prediction.
set_fit_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_score_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
scoremethod.sparsify()Convert coefficient matrix to sparse format.
- decision_function(X)#
Predict confidence scores for samples.
The confidence score for a sample is proportional to the signed distance of that sample to the hyperplane.
- Parameters:
X ({array-like, sparse matrix} of shape (n_samples, n_features)) – The data matrix for which we want to get the confidence scores.
- Returns:
scores – Confidence scores per (n_samples, n_classes) combination. In the binary case, confidence score for self.classes_[1] where >0 means this class would be predicted.
- Return type:
ndarray of shape (n_samples,) or (n_samples, n_classes)
- densify()#
Convert coefficient matrix to dense array format.
Converts the
coef_member (back) to a numpy.ndarray. This is the default format ofcoef_and is required for fitting, so calling this method is only required on models that have previously been sparsified; otherwise, it is a no-op.- Returns:
Fitted estimator.
- Return type:
self
- fit(X, y, sample_weight=None)[source]#
Build a survival support vector machine model from training data.
- Parameters:
X (array-like, shape = (n_samples, n_features)) – Data matrix.
y (structured array, shape = (n_samples,)) – A structured array with two fields. The first field is a boolean where
Trueindicates an event andFalseindicates right-censoring. The second field is a float with the time of event or time of censoring.sample_weight (array-like, shape = (n_samples,), optional) – Array of weights that are assigned to individual samples. If not provided, then each sample is given unit weight.
- Return type:
self
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequestencapsulating routing information.- Return type:
MetadataRequest
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
params – Parameter names mapped to their values.
- Return type:
dict
- predict(X)[source]#
Predict risk scores.
Predictions are risk scores (i.e. higher values indicate an increased risk of experiencing an event). The scores have no unit and are only meaningful to rank samples by their risk of experiencing an event.
- Parameters:
X (array-like, shape = (n_samples, n_features,)) – The input samples.
- Returns:
y – Predicted risk scores.
- Return type:
ndarray, shape = (n_samples,), dtype = float
- score(X, y)[source]#
Returns the concordance index of the prediction.
- Parameters:
X (array-like, shape = (n_samples, n_features)) – Test samples.
y (structured array, shape = (n_samples,)) – A structured array containing the binary event indicator as first field, and time of event or time of censoring as second field.
- Returns:
cindex – Estimated concordance index.
- Return type:
float
See also
sksurv.metrics.concordance_index_censoredComputes the concordance index.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') NaiveSurvivalSVM#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.- Returns:
self – The updated object.
- Return type:
object
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
**params (dict) – Estimator parameters.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') NaiveSurvivalSVM#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object
- sparsify()#
Convert coefficient matrix to sparse format.
Converts the
coef_member to a scipy.sparse matrix, which for L1-regularized models can be much more memory- and storage-efficient than the usual numpy.ndarray representation.The
intercept_member is not converted.- Returns:
Fitted estimator.
- Return type:
self
Notes
For non-sparse models, i.e. when there are not many zeros in
coef_, this may actually increase memory usage, so use this method with care. A rule of thumb is that the number of zero elements, which can be computed with(coef_ == 0).sum(), must be more than 50% for this to provide significant benefits.After calling this method, further fitting with the partial_fit method (if any) will not work until you call densify.