qolmat.imputations.imputers.ImputerKNN

class qolmat.imputations.imputers.ImputerKNN(groups: Tuple[str, ...] = (), n_neighbors: int = 5, weights: str = 'distance')[source]

K-nnearest neighbors imputer.

Parameters
groups: Tuple[str, …]

List of column names to group by, by default []

n_neighborsint, default=5

Number of neighbors to use by default for kneighbors queries.

weights{uniform, distance}, callable or None, default=`uniform`
Weight function used in prediction. Possible values:
  • uniformuniform weights. All points in each neighborhood

    are weighted equally.

  • distanceweight 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.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from qolmat.imputations import imputers
>>> imputer = imputers.ImputerKNN(n_neighbors=2)
>>> df = pd.DataFrame(
...     data=[
...         [1, 1, 1, 1],
...         [np.nan, np.nan, np.nan, np.nan],
...         [1, 2, 2, 5],
...         [2, 2, 2, 2],
...     ],
...     columns=["var1", "var2", "var3", "var4"],
... )
>>> imputer.fit_transform(df)
       var1      var2      var3      var4
0  1.000000  1.000000  1.000000  1.000000
1  1.333333  1.666667  1.666667  2.666667
2  1.000000  2.000000  2.000000  5.000000
3  2.000000  2.000000  2.000000  2.000000
__init__(groups: Tuple[str, ...] = (), n_neighbors: int = 5, weights: str = 'distance') None[source]