hypertools.impute¶
- hypertools.impute(data, model='PPCA', return_model=False, **kwargs)[source]¶
Fill missing (NaN) values in data, preserving its shape.
Observed (non-NaN) values are preserved exactly by every model; only the missing positions are filled. Output values are float64 (even on a NaN-free passthrough).
- Parameters:
- dataDataFrame/array or list/tuple of these
Dataset(s) to impute. A 1-D array, a flat list of numbers, or a pandas Series is treated as a UNIVARIATE series – n observations of 1 feature, i.e. an (n, 1) column – matching hyp.plot/format_data’s convention; a tuple of datasets is treated exactly like a list. A list of datasets SHARING COLUMNS is stacked (row-wise) and imputed jointly, then split back into a list matching the input structure (each dataset keeps its own index); datasets that do NOT share columns cannot be pooled and are imputed independently instead (with a warning). Degenerate inputs (None, a scalar, empty or entirely-NaN data, infinite values) raise a clear error; empty and all-NaN datasets are distinguished (an empty dataset – alone or inside a list – raises the no-observations error naming its shape, not the all-NaN one).
- modelstr, dict, class, or Imputer instance
Which imputer to use (default: ‘PPCA’, matching the pre-1.0 format_data default). A string is one of IMPUTERS’ names (PPCA, SimpleImputer, KNNImputer, IterativeImputer, Kalman); names are matched case-insensitively (‘ppca’ works too). A dict may be {‘model’: …, ‘params’: {…}} (deprecated) or {‘model’: …, ‘args’: […], ‘kwargs’: {…}}. A class or an already-constructed (unfitted) instance is used directly. An ALREADY-FITTED Imputer instance (returned from a previous return_model=True call) is applied to data via transform (its learned parameters are reused – not re-estimated).
- return_modelbool
If True, also return the fitted (or reused) Imputer instance, so it can be passed back as model= on future calls with new data (default: False). (When mismatched-column datasets are imputed independently, a LIST of fitted imputers is returned instead.)
- **kwargs
Passed through to the imputer’s constructor when model resolves to a name, class, or dict spec (merged into a dict spec’s own
kwargs, with these outer values winning). When model is already a constructed instance they cannot be applied, and a warning is raised. Unknown parameter names raise TypeError (typos used to be swallowed silently).
- Returns:
- The imputed data (and the fitted Imputer if return_model=True). Lists
- in, lists out: a single input dataset returns a single imputed
- DataFrame.
Examples
>>> import numpy as np >>> import hypertools as hyp >>> x = np.cumsum(np.random.default_rng(0).standard_normal((40, 5)), ... axis=0) >>> x[3, 1] = np.nan >>> filled = hyp.impute(x, model='PPCA') >>> filled.shape (40, 5) >>> bool(np.isnan(filled.to_numpy()).any()) False