survivalpredict.estimators.KNeighborsSurvival

class survivalpredict.estimators.KNeighborsSurvival(n_neighbors=10, algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_param=None, n_jobs=None)

Survival curves implementing the k-nearest neighbors vote.

Parameters docs are taken from scikit-learn.

Parameters:
  • n_neighbors (int, default=5) – Number of neighbors to use by default for kneighbors() queries.

  • algorithm ({'auto', 'ball_tree', 'kd_tree', 'brute'}, default='auto') –

    Algorithm used to compute the nearest neighbors:

    • ’ball_tree’ will use BallTree

    • ’kd_tree’ will use KDTree

    • ’brute’ will use a brute-force search.

    • ’auto’ will attempt to decide the most appropriate algorithm based on the values passed to fit() method.

  • leaf_size (int, default=30) – Leaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

  • p (float, default=2) – Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used. This parameter is expected to be positive.

  • metric (str or callable, default='minkowski') –

    Metric to use for distance computation. Default is “minkowski”, which results in the standard Euclidean distance when p = 2. See the documentation of scipy.spatial.distance and the metrics listed in distance_metrics for valid metric values.

    If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.

    If metric is a callable function, it takes two arrays representing 1D vectors as inputs and must return one value indicating the distance between those vectors. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.

  • metric_param (dict, default=None) – Additional keyword arguments for the metric function.

  • n_jobs (int, default=None) – The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. Doesn’t affect fit method.

Methods

fit(X, times, events[, check_input, times_start])

Fit model.

fit_predict(*args, **kwargs)

Fit model and Build survival curves.

predict(X[, max_time])

Build survival curves on an array of vectors X.

__init__(n_neighbors=10, algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_param=None, n_jobs=None)
Parameters:
  • n_neighbors (int | None)

  • algorithm (Literal['auto', 'ball_tree', 'kd_tree', 'brute'])

  • leaf_size (int)

  • p (int | float)

  • metric (str | Callable)

  • metric_param (dict | None)

  • n_jobs (int | None)

Methods

__init__([n_neighbors, algorithm, ...])

fit(X, times, events[, check_input, times_start])

Fit model.

fit_predict(*args, **kwargs)

Fit model and Build survival curves.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X[, max_time])

Build survival curves on an array of vectors X.

set_fit_request(*[, check_input, events, ...])

Configure whether metadata should be requested to be passed to the fit method.

set_params(**params)

Set the parameters of this estimator.

set_predict_request(*[, max_time])

Configure whether metadata should be requested to be passed to the predict method.

fit(X, times, events, check_input=True, times_start=None)

Fit model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • times (array-like of shape (n_samples), dtype=np.int64) – Point in time last observed.

  • events (array-like of shape (n_samples), dtype=np.bool_) – Experianed event.

  • check_input (bool, default=True) – If True, validates and casts inputs.

  • times_start (array-like of shape (n_samples, dtype=np.int64), default=None) – Starting point for observation. If not passed in, all times_start times are assumed to be 0.

Returns:

Fitted Estimator.

Return type:

object

fit_predict(*args, **kwargs)

Fit model and Build survival curves.

predict(X, max_time=None)

Build survival curves on an array of vectors X.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Predicting data.

  • max_time (int, default=None) – Maximum time of built survival curves. If none, maximum time is max time seen on training data.

Returns:

The estimated survival curves, the left-most column is the probability of survival at time 1, and the right-most column ends at max_time.

Return type:

ndarray of shape (n_samples, max_time), dtype=np.float64