Neural networks¶
Neural-network imputers: an autoencoder and generative adversarial imputation (GAIN).
Neural-network imputers.
AutoencoderImputer
¶
AutoencoderImputer(hidden_layer_sizes: tuple[int, ...] = (10,), max_iter: int = 200, random_state: int | None = None, on_error: OnError = None)
Bases: BaseImputer
Impute missing values using a simple autoencoder.
Initialize the imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_layer_sizes
|
tuple[int, ...]
|
Architecture of the |
(10,)
|
max_iter
|
int
|
Maximum training iterations. |
200
|
random_state
|
int | None
|
Random seed controlling network initialization. |
None
|
on_error
|
OnError
|
What to do if the model can't be fitted: |
None
|
Source code in src/imputation_methods/neural.py
impute
¶
Fill missing values using an autoencoder reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Dataframe with imputed values predicted by the autoencoder. Columns |
DataFrame
|
with no observed values are left as NaN and don't enter the model. |
Raises:
| Type | Description |
|---|---|
ImputationError
|
If autoencoder training or prediction fails
unexpectedly, or fails and |
Source code in src/imputation_methods/neural.py
GAINImputer
¶
GAINImputer(batch_size: int = 128, hint_rate: float = 0.9, alpha: float = 100.0, max_iter: int = 10000, learning_rate: float = 0.001, random_state: int | None = None)
Bases: BaseImputer
Impute missing values with Generative Adversarial Imputation Nets (GAIN).
A generator network fills in the missing entries, while a discriminator tries to tell which entries were observed and which were imputed. A hint vector reveals part of the missingness mask to the discriminator, and a reconstruction loss on the observed entries keeps the generator faithful to the data. Columns are min-max scaled to [0, 1] for training. Both networks have two hidden layers as wide as the number of columns and are trained with Adam, implemented directly on NumPy.
GAIN needs a reasonable amount of data to train; on small datasets simpler
methods such as :class:~imputation_methods.MICEImputer are usually more
accurate. Columns with no observed values are left untouched.
Examples:
>>> import numpy as np
>>> import pandas as pd
>>> from imputation_methods import GAINImputer
>>> df = pd.DataFrame(
... {"a": [1.0, 2.0, np.nan, 4.0], "b": [2.0, np.nan, 6.0, 8.0]}
... )
>>> imputed = GAINImputer(max_iter=100, random_state=0).impute(df)
>>> bool(imputed.notna().all().all())
True
References
Yoon, J., Jordon, J., & van der Schaar, M. (2018). GAIN: Missing data imputation using generative adversarial nets. ICML, 5689-5698.
Initialize the imputer.
The defaults are those of the reference implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Rows per training step (capped at the number of rows). |
128
|
hint_rate
|
float
|
Probability that each entry of the missingness mask is revealed to the discriminator. |
0.9
|
alpha
|
float
|
Weight of the reconstruction loss on observed entries. |
100.0
|
max_iter
|
int
|
Number of training steps. |
10000
|
learning_rate
|
float
|
Adam learning rate for both networks. |
0.001
|
random_state
|
int | None
|
Seed for initialisation, batching, noise and hints. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an argument is out of range. |
Source code in src/imputation_methods/neural.py
impute
¶
Train GAIN on df and fill its missing values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Dataframe with missing values. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Imputed dataframe. Observed values are unchanged. |