Matrix completion¶
Low-rank matrix completion with SoftImpute and probabilistic PCA.
Low-rank matrix completion imputers.
Both algorithms are implemented directly on NumPy so the package does not depend on unmaintained third-party solvers.
SoftImputeImputer
¶
SoftImputeImputer(max_iter: int = 100, init_fill_method: str = 'zero', shrinkage_value: float | None = None, convergence_threshold: float = 0.001, on_error: OnError = None)
Bases: BaseImputer
Impute missing values by low-rank matrix completion (SoftImpute).
Missing entries are initialised with init_fill_method and then
iteratively replaced by a low-rank SVD reconstruction whose singular values
are soft-thresholded, which is equivalent to nuclear-norm regularisation.
Columns with no observed values are left untouched.
Examples:
>>> import numpy as np
>>> import pandas as pd
>>> from imputation_methods import SoftImputeImputer
>>> df = pd.DataFrame({"a": [1.0, np.nan, 3.0], "b": [4.0, 5.0, np.nan]})
>>> bool(SoftImputeImputer().impute(df).notna().all().all())
True
References
Mazumder, R., Hastie, T., & Tibshirani, R. (2010). Spectral regularization algorithms for learning large incomplete matrices. Journal of Machine Learning Research, 11, 2287-2322.
Initialize the imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iter
|
int
|
Maximum number of SVD iterations. |
100
|
init_fill_method
|
str
|
How to initialise missing entries before solving:
|
'zero'
|
shrinkage_value
|
float | None
|
Amount subtracted from each singular value. Defaults to 1/50 of the largest singular value of the initial fill. |
None
|
convergence_threshold
|
float
|
Stop when the relative change of the imputed entries between iterations falls below this value. |
0.001
|
on_error
|
OnError
|
What to do if the model can't be fitted: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is out of range. |
Source code in src/imputation_methods/matrix.py
impute
¶
Fill missing values using a low-rank matrix approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with potential NaN values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with missing entries imputed by SoftImpute. |
Source code in src/imputation_methods/matrix.py
PPCAImputer
¶
PPCAImputer(n_components: int | None = 1, min_obs: int = 1, max_iter: int = 500, tol: float = 1e-06, on_error: OnError = None)
Bases: BaseImputer
Impute missing values with probabilistic PCA (PPCA).
Columns are standardised, then a PPCA model is fitted by EM while the missing entries are repeatedly replaced by their expected value under the model. The model is the maximum-likelihood PPCA of Tipping & Bishop; no priors are placed on the loadings.
Examples:
>>> import numpy as np
>>> import pandas as pd
>>> from imputation_methods import PPCAImputer
>>> df = pd.DataFrame(
... {"a": [1.0, 2.0, np.nan, 4.0], "b": [2.0, np.nan, 6.0, 8.0]}
... )
>>> bool(PPCAImputer().impute(df).notna().all().all())
True
References
Tipping, M. E., & Bishop, C. M. (1999). Probabilistic principal component analysis. Journal of the Royal Statistical Society: Series B, 61(3), 611-622.
Initialize the imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int | None
|
Number of latent dimensions. |
1
|
min_obs
|
int
|
Minimum number of observed values a column needs to take part in the model. Other columns are mean-imputed. |
1
|
max_iter
|
int
|
Maximum number of EM iterations. |
500
|
tol
|
float
|
Stop when the relative change of the imputed entries between iterations falls below this value. |
1e-06
|
on_error
|
OnError
|
What to do if the model can't be fitted: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is out of range. |
Source code in src/imputation_methods/matrix.py
impute
¶
Fill missing values using a probabilistic PCA model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with missing entries filled via probabilistic PCA. A |
DataFrame
|
single-column dataframe is returned unchanged, since PPCA needs at |
DataFrame
|
least two columns. |