package sklearn

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `KNNImputer
]
type t = [ `BaseEstimator | `KNNImputer | `Object | `TransformerMixin ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val as_transformer : t -> [ `TransformerMixin ] Obj.t
val as_estimator : t -> [ `BaseEstimator ] Obj.t
val create : ?missing_values: [ `F of float | `S of string | `Np_nan of Py.Object.t | `I of int | `None ] -> ?n_neighbors:int -> ?weights:[ `Distance | `Callable of Py.Object.t | `Uniform ] -> ?metric:[ `Callable of Py.Object.t | `Nan_euclidean ] -> ?copy:bool -> ?add_indicator:bool -> unit -> t

Imputation for completing missing values using k-Nearest Neighbors.

Each sample's missing values are imputed using the mean value from `n_neighbors` nearest neighbors found in the training set. Two samples are close if the features that neither is missing are close.

Read more in the :ref:`User Guide <knnimpute>`.

.. versionadded:: 0.22

Parameters ---------- missing_values : number, string, np.nan or None, default=`np.nan` The placeholder for the missing values. All occurrences of `missing_values` will be imputed.

n_neighbors : int, default=5 Number of neighboring samples to use for imputation.

weights : 'uniform', 'distance' or callable, default='uniform' Weight function used in prediction. Possible values:

  • 'uniform' : uniform weights. All points in each neighborhood are weighted equally.
  • 'distance' : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.
  • callable : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

metric : 'nan_euclidean' or callable, default='nan_euclidean' Distance metric for searching neighbors. Possible values:

  • 'nan_euclidean'
  • callable : a user-defined function which conforms to the definition of ``_pairwise_callable(X, Y, metric, **kwds)``. The function accepts two arrays, X and Y, and a `missing_values` keyword in `kwds` and returns a scalar distance value.

copy : bool, default=True If True, a copy of X will be created. If False, imputation will be done in-place whenever possible.

add_indicator : bool, default=False If True, a :class:`MissingIndicator` transform will stack onto the output of the imputer's transform. This allows a predictive estimator to account for missingness despite imputation. If a feature has no missing values at fit/train time, the feature won't appear on the missing indicator even if there are missing values at transform/test time.

Attributes ---------- indicator_ : :class:`sklearn.impute.MissingIndicator` Indicator used to add binary indicators for missing values. ``None`` if add_indicator is False.

References ---------- * Olga Troyanskaya, Michael Cantor, Gavin Sherlock, Pat Brown, Trevor Hastie, Robert Tibshirani, David Botstein and Russ B. Altman, Missing value estimation methods for DNA microarrays, BIOINFORMATICS Vol. 17 no. 6, 2001 Pages 520-525.

Examples -------- >>> import numpy as np >>> from sklearn.impute import KNNImputer >>> X = [1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7] >>> imputer = KNNImputer(n_neighbors=2) >>> imputer.fit_transform(X) array([1. , 2. , 4. ], [3. , 4. , 3. ], [5.5, 6. , 5. ], [8. , 8. , 7. ])

val fit : ?y:Py.Object.t -> x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> t

Fit the imputer on X.

Parameters ---------- X : array-like shape of (n_samples, n_features) Input data, where `n_samples` is the number of samples and `n_features` is the number of features.

Returns ------- self : object

val fit_transform : ?y:[> `ArrayLike ] Np.Obj.t -> ?fit_params:(string * Py.Object.t) list -> x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters ---------- X : numpy array of shape n_samples, n_features Training set.

y : numpy array of shape n_samples Target values.

**fit_params : dict Additional fit parameters.

Returns ------- X_new : numpy array of shape n_samples, n_features_new Transformed array.

val get_params : ?deep:bool -> [> tag ] Obj.t -> Dict.t

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 : mapping of string to any Parameter names mapped to their values.

val set_params : ?params:(string * Py.Object.t) list -> [> tag ] Obj.t -> t

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as pipelines). 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 : object Estimator instance.

val transform : x:[> `ArrayLike ] Np.Obj.t -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

Impute all missing values in X.

Parameters ---------- X : array-like of shape (n_samples, n_features) The input data to complete.

Returns ------- X : array-like of shape (n_samples, n_output_features) The imputed dataset. `n_output_features` is the number of features that is not always missing during `fit`.

val indicator_ : t -> Py.Object.t

Attribute indicator_: get value or raise Not_found if None.

val indicator_opt : t -> Py.Object.t option

Attribute indicator_: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.