miceforest


Namemiceforest JSON
Version 6.0.3 PyPI version JSON
download
home_pageNone
SummaryMultiple Imputation by Chained Equations with LightGBM
upload_time2024-08-02 00:43:50
maintainerNone
docs_urlNone
authorSam Von Wilson
requires_pythonNone
licenseThe MIT License (MIT) Copyright (c) 2020 Samuel Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![DOI](https://zenodo.org/badge/289387436.svg)](https://zenodo.org/badge/latestdoi/289387436)
[![Downloads](https://static.pepy.tech/badge/miceforest)](https://pepy.tech/project/miceforest)
[![Pypi](https://img.shields.io/pypi/v/miceforest.svg)](https://pypi.python.org/pypi/miceforest)
[![Conda
Version](https://img.shields.io/conda/vn/conda-forge/miceforest.svg)](https://anaconda.org/conda-forge/miceforest)
[![PyVersions](https://img.shields.io/pypi/pyversions/miceforest.svg?logo=python&logoColor=white)](https://pypi.org/project/miceforest/)  
[![tests +
mypy](https://github.com/AnotherSamWilson/miceforest/actions/workflows/run_tests.yml/badge.svg)](https://github.com/AnotherSamWilson/miceforest/actions/workflows/run_tests.yml)
[![Documentation
Status](https://readthedocs.org/projects/miceforest/badge/?version=latest)](https://miceforest.readthedocs.io/en/latest/?badge=latest)
[![CodeCov](https://codecov.io/gh/AnotherSamWilson/miceforest/branch/master/graphs/badge.svg?branch=master&service=github)](https://codecov.io/gh/AnotherSamWilson/miceforest)
<!-- [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT) -->
<!-- [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)   -->
<!-- [![DEV_Version_Badge](https://img.shields.io/badge/Dev-5.6.3-blue.svg)](https://pypi.org/project/miceforest/) -->


# miceforest: Fast, Memory Efficient Imputation with LightGBM

<a href='https://github.com/AnotherSamWilson/miceforest'><img src='https://i.imgur.com/jJPThrd.png' align="right" height="300" /></a>

Fast, memory efficient Multiple Imputation by Chained Equations (MICE)
with lightgbm. The R version of this package may be found
[here](https://github.com/FarrellDay/miceRanger).

`miceforest` was designed to be:

  - **Fast**
      - Uses lightgbm as a backend
      - Has efficient mean matching solutions.
      - Can utilize GPU training
  - **Flexible**
      - Can impute pandas dataframes
      - Handles categorical data automatically
      - Fits into a sklearn pipeline
      - User can customize every aspect of the imputation process
  - **Production Ready**
      - Can impute new, unseen datasets quickly
      - Kernels are efficiently compressed during saving and loading
      - Data can be imputed in place to save memory
      - Can build models on non-missing data

This document contains a thorough walkthrough of the package,
benchmarks, and an introduction to multiple imputation. More information
on MICE can be found in Stef van Buuren’s excellent online book, which
you can find
[here](https://stefvanbuuren.name/fimd/ch-introduction.html).

#### Table of Contents:

  - [Classes](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#classes)
  - [Basic Usage](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#basic-usage)
      - [Example](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#basic-usage)
      - [Customizing LightGBM Parameters](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#customizing-lightgbm-parameters)
      - [Available Mean Match Schemes](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#adjusting-the-mean-matching-scheme)
      - [Imputing New Data with Existing Models](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#imputing-new-data-with-existing-models)
      - [Saving and Loading Kernels](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#saving-and-loading-kernels)
      - [Implementing sklearn Pipelines](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#saving-and-loading-kernels)
  - [Advanced Features](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#advanced-features)
      - [Building Models on Nonmissing Data](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#building-models-on-nonmissing-data)
      - [Tuning Parameters](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#tuning-parameters)
      - [On Reproducibility](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#on-reproducibility)
      - [How to Make the Process Faster](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#how-to-make-the-process-faster)
      - [Imputing Data In Place](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#imputing-data-in-place)
  - [Diagnostic Plotting](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#diagnostic-plotting)
      - [Feature Importance](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#feature-importance)
      - [Imputed Distributions](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#plot-imputed-distributions)
  - [Using the Imputed Data](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#using-the-imputed-data)
  - [The MICE Algorithm](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#the-mice-algorithm)
      - [Introduction](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#the-mice-algorithm)
      - [Common Use Cases](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#common-use-cases)
      - [Predictive Mean Matching](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#predictive-mean-matching)
      - [Effects of Mean Matching](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#effects-of-mean-matching)

## Installation

This package can be installed using either pip or conda, through
conda-forge:

``` bash
# Using pip
$ pip install miceforest --no-cache-dir

# Using conda
$ conda install -c conda-forge miceforest
```

You can also download the latest development version from this
repository. If you want to install from github with conda, you must
first run `conda install pip git`.

``` bash
$ pip install git+https://github.com/AnotherSamWilson/miceforest.git
```

## Classes

miceforest has 2 main classes which the user will interact with:

  - [`ImputationKernel`](https://miceforest.readthedocs.io/en/latest/ImputationKernel.html)
    - This class contains the raw data off of which the `mice` algorithm
    is performed. During this process, models will be trained, and the
    imputed (predicted) values will be stored. These values can be used
    to fill in the missing values of the raw data. The raw data can be
    copied, or referenced directly. Models can be saved, and used to
    impute new datasets.
  - [`ImputedData`](https://miceforest.readthedocs.io/en/latest/ImputedData.html)
    - The result of `ImputationKernel.impute_new_data(new_data)`. This
    contains the raw data in `new_data` as well as the imputed values.  


## Basic Usage

We will be looking at a few simple examples of imputation. We need to
load the packages, and define the data:


```python
import miceforest as mf
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np

# Load data and introduce missing values
iris = pd.concat(load_iris(as_frame=True,return_X_y=True),axis=1)
iris.rename({"target": "species"}, inplace=True, axis=1)
iris['species'] = iris['species'].astype('category')
iris_amp = mf.ampute_data(iris,perc=0.25,random_state=1991)
```

If you only want to create a single imputed dataset, you can use
[`ImputationKernel`](https://miceforest.readthedocs.io/en/latest/ImputationKernel.html)
with some default settings:


```python
# Create kernel. 
kds = mf.ImputationKernel(
  iris_amp,
  random_state=1991
)

# Run the MICE algorithm for 2 iterations
kds.mice(2)

# Return the completed dataset.
iris_complete = kds.complete_data()
```

There are also an array of plotting functions available, these are
discussed below in the section [Diagnostic
Plotting](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#diagnostic-plotting).  

We usually don’t want to impute just a single dataset. In statistics,
multiple imputation is a process by which the uncertainty/other effects
caused by missing values can be examined by creating multiple different
imputed datasets.
[`ImputationKernel`](https://miceforest.readthedocs.io/en/latest/ImputationKernel.html)
can contain an arbitrary number of different datasets, all of which have
gone through mutually exclusive imputation processes:


```python
# Create kernel. 
kernel = mf.ImputationKernel(
  iris_amp,
  num_datasets=4,
  random_state=1
)

# Run the MICE algorithm for 2 iterations on each of the datasets
kernel.mice(2)

# Printing the kernel will show you some high level information.
print(kernel)
```

    
                  Class: ImputationKernel
                Datasets: 4
              Iterations: 2
            Data Samples: 150
            Data Columns: 5
       Imputed Variables: 5
       Modeled Variables: 5
    All Iterations Saved: True
            


After we have run mice, we can obtain our completed dataset directly
from the kernel:


```python
completed_dataset = kernel.complete_data(dataset=2)
print(completed_dataset.isnull().sum(0))
```

    sepal length (cm)    0
    sepal width (cm)     0
    petal length (cm)    0
    petal width (cm)     0
    species              0
    dtype: int64


## Customizing LightGBM Parameters

Parameters can be passed directly to lightgbm in several different ways.
Parameters you wish to apply globally to every model can simply be
passed as kwargs to `mice`:


```python
# Run the MICE algorithm for 1 more iteration on the kernel with new parameters
kernel.mice(iterations=1, n_estimators=50)
```

You can also pass pass variable-specific arguments to
`variable_parameters` in mice. For instance, let’s say you noticed the
imputation of the `[species]` column was taking a little longer, because
it is multiclass. You could decrease the n\_estimators specifically for
that column with:


```python
# Run the MICE algorithm for 2 more iterations on the kernel 
kernel.mice(
  iterations=1,
  variable_parameters={'species': {'n_estimators': 25}},
  n_estimators=50
)

# Let's get the actual models for these variables:
species_model = kernel.get_model(dataset=0,variable="species")
sepalwidth_model = kernel.get_model(dataset=0,variable="sepal width (cm)")

print(
f"""Species used {str(species_model.params["num_iterations"])} iterations
Sepal Width used {str(sepalwidth_model.params["num_iterations"])} iterations
"""
)
```

    Species used 25 iterations
    Sepal Width used 50 iterations
    


In this scenario, any parameters specified in `variable_parameters`
takes presidence over the kwargs.

Since we can pass any parameters we want to LightGBM, we can completely
customize how our models are built. That includes how the data should be
modeled. If your data contains count data, or any other data which can
be parameterized by lightgbm, you can simply specify that variable to be
modeled with the corresponding objective function.

For example, let’s pretend `sepal width (cm)` is a count field which can
be parameterized by a Poisson distribution. Let’s also change our
boosting method to gradient boosted trees:


```python
# Create kernel. 
cust_kernel = mf.ImputationKernel(
  iris_amp,
  num_datasets=1,
  random_state=1
)

cust_kernel.mice(
  iterations=1, 
  variable_parameters={'sepal width (cm)': {'objective': 'poisson'}},
  boosting = 'gbdt',
  min_sum_hessian_in_leaf=0.01
)
```

Other nice parameters like `monotone_constraints` can also be passed.
Setting the parameter `device: 'gpu'` will utilize GPU learning, if
LightGBM is set up to do this on your machine.

## Adjusting The Mean Matching Scheme

Note: It is probably a good idea to read [this
section](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#predictive-mean-matching)
first, to get some context on how mean matching works.

There are 4 imputation strategies employed by `miceforest`:
- **Fast** Mean Matching: Available only on binary and categorical variables. Chooses a class randomly based on the predicted probabilities output by lightgbm.
- **Normal** Mean Matching: Employs mean matching as described in the section below.
- **Shap** Mean Matching: Runs a nearest neighbor search on the shap values of the bachelor predictions in the shap values of the candidate predictions. Finds the `mean_match_candidates` nearest neighbors, and chooses one randomly as the imputation value.
- Value Imputation: Uses the value output by lightgbm as the imputation value. Skips mean matching entirely. To use, set `mean_match_candidates = 0`.

Here is the code required to use each method:


```python
# Create kernel. 
cust_kernel = mf.ImputationKernel(
  iris_amp,
  num_datasets=1,
  random_state=1,
  mean_match_strategy={
      'sepal length (cm)': 'normal',
      'sepal width (cm)': 'shap',
      'species': 'fast',
  },
  mean_match_candidates={
      'petal length (cm)': 0,
      }
)

cust_kernel.mice(
  iterations=1, 
)
```

## Imputing New Data with Existing Models

Multiple Imputation can take a long time. If you wish to impute a
dataset using the MICE algorithm, but don’t have time to train new
models, it is possible to impute new datasets using a `ImputationKernel`
object. The `impute_new_data()` function uses the models collected by
`ImputationKernel` to perform multiple imputation without updating the
models at each iteration:


```python
# Our 'new data' is just the first 15 rows of iris_amp
from datetime import datetime

# Define our new data as the first 15 rows
new_data = iris_amp.iloc[range(15)].reset_index(drop=True)

start_t = datetime.now()
new_data_imputed = cust_kernel.impute_new_data(new_data=new_data)
print(f"New Data imputed in {(datetime.now() - start_t).total_seconds()} seconds")
```

    New Data imputed in 0.040396 seconds


## Saving and Loading Kernels

Saving `miceforest` kernels is efficient. During the pickling process, the following steps are taken:

1.  Convert working data to parquet bytes.
2.  Serialize the kernel.
4.  Save to a file.

You can save and load the kernel like any other object using `pickle` or `dill`:



```python
from tempfile import mkstemp
import dill
new_file, filename = mkstemp()

with open(filename, "wb") as f:
    dill.dump(kernel, f)

with open(filename, "rb") as f:
    kernel_from_pickle = dill.load(f)
```

## Implementing sklearn Pipelines

`miceforest` kernels can be fit into sklearn pipelines to impute training and scoring
datasets:


```python
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
import miceforest as mf

kernel = mf.ImputationKernel(iris_amp, num_datasets=1, random_state=1)

pipe = Pipeline([
    ('impute', kernel),
    ('scaler', StandardScaler()),
])

# The pipeline can be used as any other estimator
# and avoids leaking the test set into the train set
X_train_t = pipe.fit_transform(
    X=iris_amp,
    y=None,
    impute__iterations=2
)
X_test_t = pipe.transform(new_data)

# Show that neither now have missing values.
assert not np.any(np.isnan(X_train_t))
assert not np.any(np.isnan(X_test_t))
```

# Advanced Features

## Building Models on Nonmissing Data

The MICE process itself is used to impute missing data in a dataset.
However, sometimes a variable can be fully recognized in the training
data, but needs to be imputed later on in a different dataset. It is
possible to train models to impute variables even if they have no
missing values by specifying them in the `variable_schema` parameter. 
In this case, `variable_schema` is treated as the list of variables 
to train models on.


```python
# Set petal length (cm) in our amputed data 
# to original values with no missing data.
iris_amp['sepal width (cm)'] = iris['sepal width (cm)'].copy()
iris_amp.isnull().sum()
```




    sepal length (cm)    37
    sepal width (cm)      0
    petal length (cm)    37
    petal width (cm)     37
    species              37
    dtype: int64




```python
kernel = mf.ImputationKernel(
    data=iris_amp, 
    variable_schema=iris_amp.columns.to_list(), 
    num_datasets=1,
    random_state=1,
)
kernel.mice(1)
```


```python
# Remember, the dataset we are imputing does have 
# missing values in the sepal width (cm) column
new_data.isnull().sum()
```




    sepal length (cm)    4
    sepal width (cm)     3
    petal length (cm)    1
    petal width (cm)     3
    species              3
    dtype: int64




```python
new_data_imp = kernel.impute_new_data(new_data)
new_data_imp = new_data_imp.complete_data()

# All columns have been imputed.
new_data_imp.isnull().sum()
```




    sepal length (cm)    0
    sepal width (cm)     0
    petal length (cm)    0
    petal width (cm)     0
    species              0
    dtype: int64



## Tuning Parameters

`miceforest` allows you to tune the parameters on a kernel dataset.
These parameters can then be used to build the models in future
iterations of mice. In its most simple invocation, you can just call the
function with the desired optimization steps:


```python
optimal_params = kernel.tune_parameters(
    dataset=0, 
    use_gbdt=True,
    num_iterations=500,
    random_state=1,
)
kernel.mice(1, variable_parameters=optimal_params)
pd.DataFrame(optimal_params)
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>sepal length (cm)</th>
      <th>petal length (cm)</th>
      <th>petal width (cm)</th>
      <th>species</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>boosting</th>
      <td>gbdt</td>
      <td>gbdt</td>
      <td>gbdt</td>
      <td>gbdt</td>
    </tr>
    <tr>
      <th>data_sample_strategy</th>
      <td>bagging</td>
      <td>bagging</td>
      <td>bagging</td>
      <td>bagging</td>
    </tr>
    <tr>
      <th>num_iterations</th>
      <td>142</td>
      <td>248</td>
      <td>262</td>
      <td>172</td>
    </tr>
    <tr>
      <th>max_depth</th>
      <td>4</td>
      <td>4</td>
      <td>5</td>
      <td>5</td>
    </tr>
    <tr>
      <th>num_leaves</th>
      <td>12</td>
      <td>17</td>
      <td>2</td>
      <td>19</td>
    </tr>
    <tr>
      <th>min_data_in_leaf</th>
      <td>2</td>
      <td>2</td>
      <td>15</td>
      <td>5</td>
    </tr>
    <tr>
      <th>min_sum_hessian_in_leaf</th>
      <td>0.1</td>
      <td>0.1</td>
      <td>0.1</td>
      <td>0.1</td>
    </tr>
    <tr>
      <th>min_gain_to_split</th>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>bagging_fraction</th>
      <td>0.580973</td>
      <td>0.501521</td>
      <td>0.586709</td>
      <td>0.795465</td>
    </tr>
    <tr>
      <th>feature_fraction_bynode</th>
      <td>0.922566</td>
      <td>0.299912</td>
      <td>0.503182</td>
      <td>0.237637</td>
    </tr>
    <tr>
      <th>bagging_freq</th>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <th>verbosity</th>
      <td>-1</td>
      <td>-1</td>
      <td>-1</td>
      <td>-1</td>
    </tr>
    <tr>
      <th>learning_rate</th>
      <td>0.02</td>
      <td>0.02</td>
      <td>0.02</td>
      <td>0.02</td>
    </tr>
    <tr>
      <th>objective</th>
      <td>regression</td>
      <td>regression</td>
      <td>regression</td>
      <td>multiclass</td>
    </tr>
    <tr>
      <th>num_class</th>
      <td>NaN</td>
      <td>NaN</td>
      <td>NaN</td>
      <td>3</td>
    </tr>
  </tbody>
</table>
</div>



This will perform 10 fold cross validation on random samples of
parameters. By default, all variables models are tuned.  

The parameter tuning is pretty flexible. If you wish to set some model
parameters static, or to change the bounds that are searched in, you can
simply pass this information to either the `variable_parameters`
parameter, `**kwbounds`, or both:


```python
optimal_params = kernel.tune_parameters(
  dataset=0,
  variables = ['sepal width (cm)','species','petal width (cm)'],
  variable_parameters = {
    'sepal width (cm)': {'bagging_fraction': 0.5},
    'species': {'bagging_freq': (5,10)}
  },
  use_gbdt=True,
  optimization_steps=5,
  extra_trees = [True, False]
)

kernel.mice(1, variable_parameters=optimal_params)
```

In this example, we did a few things - we specified that only `sepal
width (cm)`, `species`, and `petal width (cm)` should be tuned. We also
specified some specific parameters in `variable_parameters`. Notice that
`bagging_fraction` was passed as a scalar, `0.5`. This means that, for
the variable `sepal width (cm)`, the parameter `bagging_fraction` will
be set as that number and not be tuned. We did the opposite for
`bagging_freq`. We specified bounds that the process should search in.
We also passed the argument `extra_trees` as a list. Since it was passed
to \*\*kwbounds, this parameter will apply to all variables that are
being tuned. Passing values as a list tells the process that it should
randomly sample values from the list, instead of treating them as set of
counts to search within.

Additionally, we set `use_gbdt=True`. This switches the process to use
gradient boosted trees, instead of random forests. Typically, gradient
boosted trees will perform better. The optimal `num_iterations` is also
determined by early stopping in cross validation.

The tuning process follows these rules for different parameter values it
finds:

  - Scalar: That value is used, and not tuned.  
  - Tuple: Should be length 2. Treated as the lower and upper bound to
    search in.  
  - List: Treated as a distinct list of values to try randomly.


## On Reproducibility

`miceforest` allows for different “levels” of reproducibility, global
and record-level.

##### **Global Reproducibility**

Global reproducibility ensures that the same values will be imputed if
the same code is run multiple times. To ensure global reproducibility,
all the user needs to do is set a `random_state` when the kernel is
initialized.

##### **Record-Level Reproducibility**

Sometimes we want to obtain reproducible imputations at the record
level, without having to pass the same dataset. This is possible by
passing a list of record-specific seeds to the `random_seed_array`
parameter. This is useful if imputing new data multiple times, and you
would like imputations for each row to match each time it is imputed.



```python
# Define seeds for the data, and impute iris
import numpy as np
random_seed_array = np.random.randint(0, 9999, size=iris_amp.shape[0], dtype='uint32')
iris_imputed = kernel.impute_new_data(
    iris_amp,
    random_state=4,
    random_seed_array=random_seed_array
)

# Select a random sample
new_inds = np.random.choice(150, size=15)
new_data = iris_amp.loc[new_inds].reset_index(drop=True)
new_seeds = random_seed_array[new_inds]
new_imputed = kernel.impute_new_data(
    new_data,
    random_state=4,
    random_seed_array=new_seeds
)

# We imputed the same values for the 15 values each time,
# because each record was associated with the same seed.
assert new_imputed.complete_data(0).equals(
    iris_imputed.complete_data(0).loc[new_inds].reset_index(drop=True)
)
```

## How to Make the Process Faster

Multiple Imputation is one of the most robust ways to handle missing
data - but it can take a long time. There are several strategies you can
use to decrease the time a process takes to run:

  - Decrease `data_subset`. By default all non-missing datapoints for
    each variable are used to train the model and perform mean matching.
    This can cause the model training nearest-neighbors search to take a
    long time for large data. A subset of these points can be searched
    instead by using `data_subset`.  
  - If categorical columns are taking a long time, you can set 
    `mean_match_strategy="fast"`. You can also set different parameters
    specifically for categorical columns, like smaller `bagging_fraction` 
    or `num_iterations`, or try grouping the categories before they are
    imputed. Model training time for categorical variables is linear with
    the number of distinct categories. 
  - Decrease `mean_match_candidates`. The maximum number of neighbors
    that are considered with the default parameters is 10. However, for
    large datasets, this can still be an expensive operation. Consider
    explicitly setting `mean_match_candidates` lower. Setting 
    `mean_match_candidates=0` will skip mean matching entirely, and 
    just use the lightgbm predictions as the imputation values.
  - Use different lightgbm parameters. lightgbm is usually not the
    problem, however if a certain variable has a large number of
    classes, then the max number of trees actually grown is (\# classes)
    \* (n\_estimators). You can specifically decrease the bagging
    fraction or n\_estimators for large multi-class variables, or grow
    less trees in general.  

## Imputing Data In Place

It is possible to run the entire process without copying the dataset. If
`copy_data=False`, then the data is referenced directly:



```python
kernel_inplace = mf.ImputationKernel(
  iris_amp,
  num_datasets=1,
  copy_data=False,
  random_state=1,
)
kernel_inplace.mice(2)
```

Note, that this probably won’t (but could) change the original dataset
in undesirable ways. Throughout the `mice` procedure, imputed values are
stored directly in the original data. At the end, the missing values are
put back as `np.NaN`.

We can also complete our original data in place. This is useful if the dataset is large, and copies can’t be made in
memory:


```python
kernel_inplace.complete_data(dataset=0, inplace=True)
print(iris_amp.isnull().sum(0))
```

    sepal length (cm)    0
    sepal width (cm)     0
    petal length (cm)    0
    petal width (cm)     0
    species              0
    dtype: int64


# Diagnostic Plotting

As of now, there are 2 diagnostic plot available. More coming soon!

### Feature Importance


```python
kernel.plot_feature_importance(dataset=0)
```


    
![png](README_files/README_48_0.png)
    


### Plot Imputed Distributions


```python
kernel.plot_imputed_distributions()
```


    
![png](README_files/README_50_0.png)
    


## Using the Imputed Data

To return the imputed data simply use the `complete_data` method:


```python
dataset_1 = kernel.complete_data(0)
```

This will return a single specified dataset. Multiple datasets are
typically created so that some measure of confidence around each
prediction can be created.

Since we know what the original data looked like, we can cheat and see
how well the imputations compare to the original data:


```python
acclist = []
iterations = kernel.iteration_count()+1
for iteration in range(iterations):
    species_na_count = kernel.na_counts['species']
    compdat = kernel.complete_data(dataset=0,iteration=iteration)
    
    # Record the accuract of the imputations of species.
    acclist.append(
      round(1-sum(compdat['species'] != iris['species'])/species_na_count,2)
    )

# acclist shows the accuracy of the imputations over the iterations.
acclist = pd.Series(acclist).rename("Species Imputation Accuracy")
acclist.index = range(iterations)
acclist.index.name = "Iteration"
acclist
```




    Iteration
    0    0.35
    1    0.81
    2    0.81
    3    0.78
    Name: Species Imputation Accuracy, dtype: float64



In this instance, we went from a low accuracy (what is expected with
random sampling) to a much higher accuracy.

## The MICE Algorithm

Multiple Imputation by Chained Equations ‘fills in’ (imputes) missing
data in a dataset through an iterative series of predictive models. In
each iteration, each specified variable in the dataset is imputed using
the other variables in the dataset. These iterations should be run until
it appears that convergence has been met.

<img src="https://i.imgur.com/2L403kU.png" style="display: block; margin: auto;" />

This process is continued until all specified variables have been
imputed. Additional iterations can be run if it appears that the average
imputed values have not converged, although no more than 5 iterations
are usually necessary.

### Common Use Cases

##### **Data Leakage:**

MICE is particularly useful if missing values are associated with the
target variable in a way that introduces leakage. For instance, let’s
say you wanted to model customer retention at the time of sign up. A
certain variable is collected at sign up or 1 month after sign up. The
absence of that variable is a data leak, since it tells you that the
customer did not retain for 1 month.

##### **Funnel Analysis:**

Information is often collected at different stages of a ‘funnel’. MICE
can be used to make educated guesses about the characteristics of
entities at different points in a funnel.

##### **Confidence Intervals:**

MICE can be used to impute missing values, however it is important to
keep in mind that these imputed values are a prediction. Creating
multiple datasets with different imputed values allows you to do two
types of inference:

  - Imputed Value Distribution: A profile can be built for each imputed
    value, allowing you to make statements about the likely distribution
    of that value.  
  - Model Prediction Distribution: With multiple datasets, you can build
    multiple models and create a distribution of predictions for each
    sample. Those samples with imputed values which were not able to be
    imputed with much confidence would have a larger variance in their
    predictions.


## Predictive Mean Matching

`miceforest` can make use of a procedure called predictive mean matching
(PMM) to select which values are imputed. PMM involves selecting a
datapoint from the original, nonmissing data (candidates) which has a
predicted value close to the predicted value of the missing sample
(bachelors). The closest N (`mean_match_candidates` parameter) values
are selected, from which a value is chosen at random. This can be
specified on a column-by-column basis. Going into more detail from our
example above, we see how this works in practice:

<img src="https://i.imgur.com/3DBCXnL.png" style="display: block; margin: auto;" />

This method is very useful if you have a variable which needs imputing
which has any of the following characteristics:

  - Multimodal  
  - Integer  
  - Skewed


### Effects of Mean Matching

As an example, let’s construct a dataset with some of the above
characteristics:


```python
randst = np.random.RandomState(1991)
# random uniform variable
nrws = 1000
uniform_vec = randst.uniform(size=nrws)

def make_bimodal(mean1,mean2,size):
    bimodal_1 = randst.normal(size=nrws, loc=mean1)
    bimodal_2 = randst.normal(size=nrws, loc=mean2)
    bimdvec = []
    for i in range(size):
        bimdvec.append(randst.choice([bimodal_1[i], bimodal_2[i]]))
    return np.array(bimdvec)

# Make 2 Bimodal Variables
close_bimodal_vec = make_bimodal(2,-2,nrws)
far_bimodal_vec = make_bimodal(3,-3,nrws)


# Highly skewed variable correlated with Uniform_Variable
skewed_vec = np.exp(uniform_vec*randst.uniform(size=nrws)*3) + randst.uniform(size=nrws)*3

# Integer variable correlated with Close_Bimodal_Variable and Uniform_Variable
integer_vec = np.round(uniform_vec + close_bimodal_vec/3 + randst.uniform(size=nrws)*2)

# Make a DataFrame
dat = pd.DataFrame(
    {
    'uniform_var':uniform_vec,
    'close_bimodal_var':close_bimodal_vec,
    'far_bimodal_var':far_bimodal_vec,
    'skewed_var':skewed_vec,
    'integer_var':integer_vec
    }
)

# Ampute the data.
ampdat = mf.ampute_data(dat,perc=0.25,random_state=randst)
```


```python
import plotnine as p9
import itertools

def plot_matrix(df, columns):
    pdf = []
    for a1, b1 in itertools.combinations(columns, 2):
        for (a,b) in ((a1, b1), (b1, a1)):
            sub = df[[a, b]].rename(columns={a: "x", b: "y"}).assign(a=a, b=b)
            pdf.append(sub)

    g = (
        p9.ggplot(pd.concat(pdf))
            + p9.geom_point(p9.aes('x','y'))
            + p9.facet_grid('b~a', scales='free')
            + p9.theme(figure_size=(7, 7))
            + p9.xlab("") + p9.ylab("")
    )
    return g

plot_matrix(dat, dat.columns)
```


    
![png](README_files/README_60_0.png)
    


We can see how our variables are distributed and correlated in the graph
above. Now let’s run our imputation process twice, once using mean
matching, and once using the model prediction.


```python
kernel_mean_match = mf.ImputationKernel(
    data=ampdat,
    num_datasets=3,
    mean_match_candidates=5,
    random_state=1
)
kernel_mean_match.mice(2)
kernel_no_mean_match = mf.ImputationKernel(
    data=ampdat,
    num_datasets=3,
    mean_match_candidates=0,
    random_state=1
)
kernel_no_mean_match.mice(2)
```


```python
kernel_mean_match.plot_imputed_distributions()
```


    
![png](README_files/README_63_0.png)
    



```python
kernel_no_mean_match.plot_imputed_distributions()
```


    
![png](README_files/README_64_0.png)
    


You can see the effects that mean matching has, depending on the
distribution of the data. Simply returning the value from the model
prediction, while it may provide a better ‘fit’, will not provide
imputations with a similair distribution to the original. This may be
beneficial, depending on your goal.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "miceforest",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Sam Von Wilson",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/f5/3c/dfd74af1aa573a56fbe54a6a4c524586747ef636ef501295bb1d5bc9935b/miceforest-6.0.3.tar.gz",
    "platform": null,
    "description": "[![DOI](https://zenodo.org/badge/289387436.svg)](https://zenodo.org/badge/latestdoi/289387436)\n[![Downloads](https://static.pepy.tech/badge/miceforest)](https://pepy.tech/project/miceforest)\n[![Pypi](https://img.shields.io/pypi/v/miceforest.svg)](https://pypi.python.org/pypi/miceforest)\n[![Conda\nVersion](https://img.shields.io/conda/vn/conda-forge/miceforest.svg)](https://anaconda.org/conda-forge/miceforest)\n[![PyVersions](https://img.shields.io/pypi/pyversions/miceforest.svg?logo=python&logoColor=white)](https://pypi.org/project/miceforest/)  \n[![tests +\nmypy](https://github.com/AnotherSamWilson/miceforest/actions/workflows/run_tests.yml/badge.svg)](https://github.com/AnotherSamWilson/miceforest/actions/workflows/run_tests.yml)\n[![Documentation\nStatus](https://readthedocs.org/projects/miceforest/badge/?version=latest)](https://miceforest.readthedocs.io/en/latest/?badge=latest)\n[![CodeCov](https://codecov.io/gh/AnotherSamWilson/miceforest/branch/master/graphs/badge.svg?branch=master&service=github)](https://codecov.io/gh/AnotherSamWilson/miceforest)\n<!-- [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT) -->\n<!-- [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)   -->\n<!-- [![DEV_Version_Badge](https://img.shields.io/badge/Dev-5.6.3-blue.svg)](https://pypi.org/project/miceforest/) -->\n\n\n# miceforest: Fast, Memory Efficient Imputation with LightGBM\n\n<a href='https://github.com/AnotherSamWilson/miceforest'><img src='https://i.imgur.com/jJPThrd.png' align=\"right\" height=\"300\" /></a>\n\nFast, memory efficient Multiple Imputation by Chained Equations (MICE)\nwith lightgbm. The R version of this package may be found\n[here](https://github.com/FarrellDay/miceRanger).\n\n`miceforest` was designed to be:\n\n  - **Fast**\n      - Uses lightgbm as a backend\n      - Has efficient mean matching solutions.\n      - Can utilize GPU training\n  - **Flexible**\n      - Can impute pandas dataframes\n      - Handles categorical data automatically\n      - Fits into a sklearn pipeline\n      - User can customize every aspect of the imputation process\n  - **Production Ready**\n      - Can impute new, unseen datasets quickly\n      - Kernels are efficiently compressed during saving and loading\n      - Data can be imputed in place to save memory\n      - Can build models on non-missing data\n\nThis document contains a thorough walkthrough of the package,\nbenchmarks, and an introduction to multiple imputation. More information\non MICE can be found in Stef van Buuren\u2019s excellent online book, which\nyou can find\n[here](https://stefvanbuuren.name/fimd/ch-introduction.html).\n\n#### Table of Contents:\n\n  - [Classes](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#classes)\n  - [Basic Usage](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#basic-usage)\n      - [Example](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#basic-usage)\n      - [Customizing LightGBM Parameters](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#customizing-lightgbm-parameters)\n      - [Available Mean Match Schemes](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#adjusting-the-mean-matching-scheme)\n      - [Imputing New Data with Existing Models](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#imputing-new-data-with-existing-models)\n      - [Saving and Loading Kernels](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#saving-and-loading-kernels)\n      - [Implementing sklearn Pipelines](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#saving-and-loading-kernels)\n  - [Advanced Features](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#advanced-features)\n      - [Building Models on Nonmissing Data](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#building-models-on-nonmissing-data)\n      - [Tuning Parameters](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#tuning-parameters)\n      - [On Reproducibility](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#on-reproducibility)\n      - [How to Make the Process Faster](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#how-to-make-the-process-faster)\n      - [Imputing Data In Place](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#imputing-data-in-place)\n  - [Diagnostic Plotting](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#diagnostic-plotting)\n      - [Feature Importance](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#feature-importance)\n      - [Imputed Distributions](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#plot-imputed-distributions)\n  - [Using the Imputed Data](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#using-the-imputed-data)\n  - [The MICE Algorithm](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#the-mice-algorithm)\n      - [Introduction](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#the-mice-algorithm)\n      - [Common Use Cases](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#common-use-cases)\n      - [Predictive Mean Matching](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#predictive-mean-matching)\n      - [Effects of Mean Matching](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#effects-of-mean-matching)\n\n## Installation\n\nThis package can be installed using either pip or conda, through\nconda-forge:\n\n``` bash\n# Using pip\n$ pip install miceforest --no-cache-dir\n\n# Using conda\n$ conda install -c conda-forge miceforest\n```\n\nYou can also download the latest development version from this\nrepository. If you want to install from github with conda, you must\nfirst run `conda install pip git`.\n\n``` bash\n$ pip install git+https://github.com/AnotherSamWilson/miceforest.git\n```\n\n## Classes\n\nmiceforest has 2 main classes which the user will interact with:\n\n  - [`ImputationKernel`](https://miceforest.readthedocs.io/en/latest/ImputationKernel.html)\n    - This class contains the raw data off of which the `mice` algorithm\n    is performed. During this process, models will be trained, and the\n    imputed (predicted) values will be stored. These values can be used\n    to fill in the missing values of the raw data. The raw data can be\n    copied, or referenced directly. Models can be saved, and used to\n    impute new datasets.\n  - [`ImputedData`](https://miceforest.readthedocs.io/en/latest/ImputedData.html)\n    - The result of `ImputationKernel.impute_new_data(new_data)`. This\n    contains the raw data in `new_data` as well as the imputed values.  \n\n\n## Basic Usage\n\nWe will be looking at a few simple examples of imputation. We need to\nload the packages, and define the data:\n\n\n```python\nimport miceforest as mf\nfrom sklearn.datasets import load_iris\nimport pandas as pd\nimport numpy as np\n\n# Load data and introduce missing values\niris = pd.concat(load_iris(as_frame=True,return_X_y=True),axis=1)\niris.rename({\"target\": \"species\"}, inplace=True, axis=1)\niris['species'] = iris['species'].astype('category')\niris_amp = mf.ampute_data(iris,perc=0.25,random_state=1991)\n```\n\nIf you only want to create a single imputed dataset, you can use\n[`ImputationKernel`](https://miceforest.readthedocs.io/en/latest/ImputationKernel.html)\nwith some default settings:\n\n\n```python\n# Create kernel. \nkds = mf.ImputationKernel(\n  iris_amp,\n  random_state=1991\n)\n\n# Run the MICE algorithm for 2 iterations\nkds.mice(2)\n\n# Return the completed dataset.\niris_complete = kds.complete_data()\n```\n\nThere are also an array of plotting functions available, these are\ndiscussed below in the section [Diagnostic\nPlotting](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#diagnostic-plotting).  \n\nWe usually don\u2019t want to impute just a single dataset. In statistics,\nmultiple imputation is a process by which the uncertainty/other effects\ncaused by missing values can be examined by creating multiple different\nimputed datasets.\n[`ImputationKernel`](https://miceforest.readthedocs.io/en/latest/ImputationKernel.html)\ncan contain an arbitrary number of different datasets, all of which have\ngone through mutually exclusive imputation processes:\n\n\n```python\n# Create kernel. \nkernel = mf.ImputationKernel(\n  iris_amp,\n  num_datasets=4,\n  random_state=1\n)\n\n# Run the MICE algorithm for 2 iterations on each of the datasets\nkernel.mice(2)\n\n# Printing the kernel will show you some high level information.\nprint(kernel)\n```\n\n    \n                  Class: ImputationKernel\n                Datasets: 4\n              Iterations: 2\n            Data Samples: 150\n            Data Columns: 5\n       Imputed Variables: 5\n       Modeled Variables: 5\n    All Iterations Saved: True\n            \n\n\nAfter we have run mice, we can obtain our completed dataset directly\nfrom the kernel:\n\n\n```python\ncompleted_dataset = kernel.complete_data(dataset=2)\nprint(completed_dataset.isnull().sum(0))\n```\n\n    sepal length (cm)    0\n    sepal width (cm)     0\n    petal length (cm)    0\n    petal width (cm)     0\n    species              0\n    dtype: int64\n\n\n## Customizing LightGBM Parameters\n\nParameters can be passed directly to lightgbm in several different ways.\nParameters you wish to apply globally to every model can simply be\npassed as kwargs to `mice`:\n\n\n```python\n# Run the MICE algorithm for 1 more iteration on the kernel with new parameters\nkernel.mice(iterations=1, n_estimators=50)\n```\n\nYou can also pass pass variable-specific arguments to\n`variable_parameters` in mice. For instance, let\u2019s say you noticed the\nimputation of the `[species]` column was taking a little longer, because\nit is multiclass. You could decrease the n\\_estimators specifically for\nthat column with:\n\n\n```python\n# Run the MICE algorithm for 2 more iterations on the kernel \nkernel.mice(\n  iterations=1,\n  variable_parameters={'species': {'n_estimators': 25}},\n  n_estimators=50\n)\n\n# Let's get the actual models for these variables:\nspecies_model = kernel.get_model(dataset=0,variable=\"species\")\nsepalwidth_model = kernel.get_model(dataset=0,variable=\"sepal width (cm)\")\n\nprint(\nf\"\"\"Species used {str(species_model.params[\"num_iterations\"])} iterations\nSepal Width used {str(sepalwidth_model.params[\"num_iterations\"])} iterations\n\"\"\"\n)\n```\n\n    Species used 25 iterations\n    Sepal Width used 50 iterations\n    \n\n\nIn this scenario, any parameters specified in `variable_parameters`\ntakes presidence over the kwargs.\n\nSince we can pass any parameters we want to LightGBM, we can completely\ncustomize how our models are built. That includes how the data should be\nmodeled. If your data contains count data, or any other data which can\nbe parameterized by lightgbm, you can simply specify that variable to be\nmodeled with the corresponding objective function.\n\nFor example, let\u2019s pretend `sepal width (cm)` is a count field which can\nbe parameterized by a Poisson distribution. Let\u2019s also change our\nboosting method to gradient boosted trees:\n\n\n```python\n# Create kernel. \ncust_kernel = mf.ImputationKernel(\n  iris_amp,\n  num_datasets=1,\n  random_state=1\n)\n\ncust_kernel.mice(\n  iterations=1, \n  variable_parameters={'sepal width (cm)': {'objective': 'poisson'}},\n  boosting = 'gbdt',\n  min_sum_hessian_in_leaf=0.01\n)\n```\n\nOther nice parameters like `monotone_constraints` can also be passed.\nSetting the parameter `device: 'gpu'` will utilize GPU learning, if\nLightGBM is set up to do this on your machine.\n\n## Adjusting The Mean Matching Scheme\n\nNote: It is probably a good idea to read [this\nsection](https://github.com/AnotherSamWilson/miceforest?tab=readme-ov-file#predictive-mean-matching)\nfirst, to get some context on how mean matching works.\n\nThere are 4 imputation strategies employed by `miceforest`:\n- **Fast** Mean Matching: Available only on binary and categorical variables. Chooses a class randomly based on the predicted probabilities output by lightgbm.\n- **Normal** Mean Matching: Employs mean matching as described in the section below.\n- **Shap** Mean Matching: Runs a nearest neighbor search on the shap values of the bachelor predictions in the shap values of the candidate predictions. Finds the `mean_match_candidates` nearest neighbors, and chooses one randomly as the imputation value.\n- Value Imputation: Uses the value output by lightgbm as the imputation value. Skips mean matching entirely. To use, set `mean_match_candidates = 0`.\n\nHere is the code required to use each method:\n\n\n```python\n# Create kernel. \ncust_kernel = mf.ImputationKernel(\n  iris_amp,\n  num_datasets=1,\n  random_state=1,\n  mean_match_strategy={\n      'sepal length (cm)': 'normal',\n      'sepal width (cm)': 'shap',\n      'species': 'fast',\n  },\n  mean_match_candidates={\n      'petal length (cm)': 0,\n      }\n)\n\ncust_kernel.mice(\n  iterations=1, \n)\n```\n\n## Imputing New Data with Existing Models\n\nMultiple Imputation can take a long time. If you wish to impute a\ndataset using the MICE algorithm, but don\u2019t have time to train new\nmodels, it is possible to impute new datasets using a `ImputationKernel`\nobject. The `impute_new_data()` function uses the models collected by\n`ImputationKernel` to perform multiple imputation without updating the\nmodels at each iteration:\n\n\n```python\n# Our 'new data' is just the first 15 rows of iris_amp\nfrom datetime import datetime\n\n# Define our new data as the first 15 rows\nnew_data = iris_amp.iloc[range(15)].reset_index(drop=True)\n\nstart_t = datetime.now()\nnew_data_imputed = cust_kernel.impute_new_data(new_data=new_data)\nprint(f\"New Data imputed in {(datetime.now() - start_t).total_seconds()} seconds\")\n```\n\n    New Data imputed in 0.040396 seconds\n\n\n## Saving and Loading Kernels\n\nSaving `miceforest` kernels is efficient. During the pickling process, the following steps are taken:\n\n1.  Convert working data to parquet bytes.\n2.  Serialize the kernel.\n4.  Save to a file.\n\nYou can save and load the kernel like any other object using `pickle` or `dill`:\n\n\n\n```python\nfrom tempfile import mkstemp\nimport dill\nnew_file, filename = mkstemp()\n\nwith open(filename, \"wb\") as f:\n    dill.dump(kernel, f)\n\nwith open(filename, \"rb\") as f:\n    kernel_from_pickle = dill.load(f)\n```\n\n## Implementing sklearn Pipelines\n\n`miceforest` kernels can be fit into sklearn pipelines to impute training and scoring\ndatasets:\n\n\n```python\nimport numpy as np\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.datasets import make_classification\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.pipeline import Pipeline\nimport miceforest as mf\n\nkernel = mf.ImputationKernel(iris_amp, num_datasets=1, random_state=1)\n\npipe = Pipeline([\n    ('impute', kernel),\n    ('scaler', StandardScaler()),\n])\n\n# The pipeline can be used as any other estimator\n# and avoids leaking the test set into the train set\nX_train_t = pipe.fit_transform(\n    X=iris_amp,\n    y=None,\n    impute__iterations=2\n)\nX_test_t = pipe.transform(new_data)\n\n# Show that neither now have missing values.\nassert not np.any(np.isnan(X_train_t))\nassert not np.any(np.isnan(X_test_t))\n```\n\n# Advanced Features\n\n## Building Models on Nonmissing Data\n\nThe MICE process itself is used to impute missing data in a dataset.\nHowever, sometimes a variable can be fully recognized in the training\ndata, but needs to be imputed later on in a different dataset. It is\npossible to train models to impute variables even if they have no\nmissing values by specifying them in the `variable_schema` parameter. \nIn this case, `variable_schema` is treated as the list of variables \nto train models on.\n\n\n```python\n# Set petal length (cm) in our amputed data \n# to original values with no missing data.\niris_amp['sepal width (cm)'] = iris['sepal width (cm)'].copy()\niris_amp.isnull().sum()\n```\n\n\n\n\n    sepal length (cm)    37\n    sepal width (cm)      0\n    petal length (cm)    37\n    petal width (cm)     37\n    species              37\n    dtype: int64\n\n\n\n\n```python\nkernel = mf.ImputationKernel(\n    data=iris_amp, \n    variable_schema=iris_amp.columns.to_list(), \n    num_datasets=1,\n    random_state=1,\n)\nkernel.mice(1)\n```\n\n\n```python\n# Remember, the dataset we are imputing does have \n# missing values in the sepal width (cm) column\nnew_data.isnull().sum()\n```\n\n\n\n\n    sepal length (cm)    4\n    sepal width (cm)     3\n    petal length (cm)    1\n    petal width (cm)     3\n    species              3\n    dtype: int64\n\n\n\n\n```python\nnew_data_imp = kernel.impute_new_data(new_data)\nnew_data_imp = new_data_imp.complete_data()\n\n# All columns have been imputed.\nnew_data_imp.isnull().sum()\n```\n\n\n\n\n    sepal length (cm)    0\n    sepal width (cm)     0\n    petal length (cm)    0\n    petal width (cm)     0\n    species              0\n    dtype: int64\n\n\n\n## Tuning Parameters\n\n`miceforest` allows you to tune the parameters on a kernel dataset.\nThese parameters can then be used to build the models in future\niterations of mice. In its most simple invocation, you can just call the\nfunction with the desired optimization steps:\n\n\n```python\noptimal_params = kernel.tune_parameters(\n    dataset=0, \n    use_gbdt=True,\n    num_iterations=500,\n    random_state=1,\n)\nkernel.mice(1, variable_parameters=optimal_params)\npd.DataFrame(optimal_params)\n```\n\n\n\n\n<div>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sepal length (cm)</th>\n      <th>petal length (cm)</th>\n      <th>petal width (cm)</th>\n      <th>species</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>boosting</th>\n      <td>gbdt</td>\n      <td>gbdt</td>\n      <td>gbdt</td>\n      <td>gbdt</td>\n    </tr>\n    <tr>\n      <th>data_sample_strategy</th>\n      <td>bagging</td>\n      <td>bagging</td>\n      <td>bagging</td>\n      <td>bagging</td>\n    </tr>\n    <tr>\n      <th>num_iterations</th>\n      <td>142</td>\n      <td>248</td>\n      <td>262</td>\n      <td>172</td>\n    </tr>\n    <tr>\n      <th>max_depth</th>\n      <td>4</td>\n      <td>4</td>\n      <td>5</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>num_leaves</th>\n      <td>12</td>\n      <td>17</td>\n      <td>2</td>\n      <td>19</td>\n    </tr>\n    <tr>\n      <th>min_data_in_leaf</th>\n      <td>2</td>\n      <td>2</td>\n      <td>15</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>min_sum_hessian_in_leaf</th>\n      <td>0.1</td>\n      <td>0.1</td>\n      <td>0.1</td>\n      <td>0.1</td>\n    </tr>\n    <tr>\n      <th>min_gain_to_split</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>bagging_fraction</th>\n      <td>0.580973</td>\n      <td>0.501521</td>\n      <td>0.586709</td>\n      <td>0.795465</td>\n    </tr>\n    <tr>\n      <th>feature_fraction_bynode</th>\n      <td>0.922566</td>\n      <td>0.299912</td>\n      <td>0.503182</td>\n      <td>0.237637</td>\n    </tr>\n    <tr>\n      <th>bagging_freq</th>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>verbosity</th>\n      <td>-1</td>\n      <td>-1</td>\n      <td>-1</td>\n      <td>-1</td>\n    </tr>\n    <tr>\n      <th>learning_rate</th>\n      <td>0.02</td>\n      <td>0.02</td>\n      <td>0.02</td>\n      <td>0.02</td>\n    </tr>\n    <tr>\n      <th>objective</th>\n      <td>regression</td>\n      <td>regression</td>\n      <td>regression</td>\n      <td>multiclass</td>\n    </tr>\n    <tr>\n      <th>num_class</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n</div>\n\n\n\nThis will perform 10 fold cross validation on random samples of\nparameters. By default, all variables models are tuned.  \n\nThe parameter tuning is pretty flexible. If you wish to set some model\nparameters static, or to change the bounds that are searched in, you can\nsimply pass this information to either the `variable_parameters`\nparameter, `**kwbounds`, or both:\n\n\n```python\noptimal_params = kernel.tune_parameters(\n  dataset=0,\n  variables = ['sepal width (cm)','species','petal width (cm)'],\n  variable_parameters = {\n    'sepal width (cm)': {'bagging_fraction': 0.5},\n    'species': {'bagging_freq': (5,10)}\n  },\n  use_gbdt=True,\n  optimization_steps=5,\n  extra_trees = [True, False]\n)\n\nkernel.mice(1, variable_parameters=optimal_params)\n```\n\nIn this example, we did a few things - we specified that only `sepal\nwidth (cm)`, `species`, and `petal width (cm)` should be tuned. We also\nspecified some specific parameters in `variable_parameters`. Notice that\n`bagging_fraction` was passed as a scalar, `0.5`. This means that, for\nthe variable `sepal width (cm)`, the parameter `bagging_fraction` will\nbe set as that number and not be tuned. We did the opposite for\n`bagging_freq`. We specified bounds that the process should search in.\nWe also passed the argument `extra_trees` as a list. Since it was passed\nto \\*\\*kwbounds, this parameter will apply to all variables that are\nbeing tuned. Passing values as a list tells the process that it should\nrandomly sample values from the list, instead of treating them as set of\ncounts to search within.\n\nAdditionally, we set `use_gbdt=True`. This switches the process to use\ngradient boosted trees, instead of random forests. Typically, gradient\nboosted trees will perform better. The optimal `num_iterations` is also\ndetermined by early stopping in cross validation.\n\nThe tuning process follows these rules for different parameter values it\nfinds:\n\n  - Scalar: That value is used, and not tuned.  \n  - Tuple: Should be length 2. Treated as the lower and upper bound to\n    search in.  \n  - List: Treated as a distinct list of values to try randomly.\n\n\n## On Reproducibility\n\n`miceforest` allows for different \u201clevels\u201d of reproducibility, global\nand record-level.\n\n##### **Global Reproducibility**\n\nGlobal reproducibility ensures that the same values will be imputed if\nthe same code is run multiple times. To ensure global reproducibility,\nall the user needs to do is set a `random_state` when the kernel is\ninitialized.\n\n##### **Record-Level Reproducibility**\n\nSometimes we want to obtain reproducible imputations at the record\nlevel, without having to pass the same dataset. This is possible by\npassing a list of record-specific seeds to the `random_seed_array`\nparameter. This is useful if imputing new data multiple times, and you\nwould like imputations for each row to match each time it is imputed.\n\n\n\n```python\n# Define seeds for the data, and impute iris\nimport numpy as np\nrandom_seed_array = np.random.randint(0, 9999, size=iris_amp.shape[0], dtype='uint32')\niris_imputed = kernel.impute_new_data(\n    iris_amp,\n    random_state=4,\n    random_seed_array=random_seed_array\n)\n\n# Select a random sample\nnew_inds = np.random.choice(150, size=15)\nnew_data = iris_amp.loc[new_inds].reset_index(drop=True)\nnew_seeds = random_seed_array[new_inds]\nnew_imputed = kernel.impute_new_data(\n    new_data,\n    random_state=4,\n    random_seed_array=new_seeds\n)\n\n# We imputed the same values for the 15 values each time,\n# because each record was associated with the same seed.\nassert new_imputed.complete_data(0).equals(\n    iris_imputed.complete_data(0).loc[new_inds].reset_index(drop=True)\n)\n```\n\n## How to Make the Process Faster\n\nMultiple Imputation is one of the most robust ways to handle missing\ndata - but it can take a long time. There are several strategies you can\nuse to decrease the time a process takes to run:\n\n  - Decrease `data_subset`. By default all non-missing datapoints for\n    each variable are used to train the model and perform mean matching.\n    This can cause the model training nearest-neighbors search to take a\n    long time for large data. A subset of these points can be searched\n    instead by using `data_subset`.  \n  - If categorical columns are taking a long time, you can set \n    `mean_match_strategy=\"fast\"`. You can also set different parameters\n    specifically for categorical columns, like smaller `bagging_fraction` \n    or `num_iterations`, or try grouping the categories before they are\n    imputed. Model training time for categorical variables is linear with\n    the number of distinct categories. \n  - Decrease `mean_match_candidates`. The maximum number of neighbors\n    that are considered with the default parameters is 10. However, for\n    large datasets, this can still be an expensive operation. Consider\n    explicitly setting `mean_match_candidates` lower. Setting \n    `mean_match_candidates=0` will skip mean matching entirely, and \n    just use the lightgbm predictions as the imputation values.\n  - Use different lightgbm parameters. lightgbm is usually not the\n    problem, however if a certain variable has a large number of\n    classes, then the max number of trees actually grown is (\\# classes)\n    \\* (n\\_estimators). You can specifically decrease the bagging\n    fraction or n\\_estimators for large multi-class variables, or grow\n    less trees in general.  \n\n## Imputing Data In Place\n\nIt is possible to run the entire process without copying the dataset. If\n`copy_data=False`, then the data is referenced directly:\n\n\n\n```python\nkernel_inplace = mf.ImputationKernel(\n  iris_amp,\n  num_datasets=1,\n  copy_data=False,\n  random_state=1,\n)\nkernel_inplace.mice(2)\n```\n\nNote, that this probably won\u2019t (but could) change the original dataset\nin undesirable ways. Throughout the `mice` procedure, imputed values are\nstored directly in the original data. At the end, the missing values are\nput back as `np.NaN`.\n\nWe can also complete our original data in place. This is useful if the dataset is large, and copies can\u2019t be made in\nmemory:\n\n\n```python\nkernel_inplace.complete_data(dataset=0, inplace=True)\nprint(iris_amp.isnull().sum(0))\n```\n\n    sepal length (cm)    0\n    sepal width (cm)     0\n    petal length (cm)    0\n    petal width (cm)     0\n    species              0\n    dtype: int64\n\n\n# Diagnostic Plotting\n\nAs of now, there are 2 diagnostic plot available. More coming soon!\n\n### Feature Importance\n\n\n```python\nkernel.plot_feature_importance(dataset=0)\n```\n\n\n    \n![png](README_files/README_48_0.png)\n    \n\n\n### Plot Imputed Distributions\n\n\n```python\nkernel.plot_imputed_distributions()\n```\n\n\n    \n![png](README_files/README_50_0.png)\n    \n\n\n## Using the Imputed Data\n\nTo return the imputed data simply use the `complete_data` method:\n\n\n```python\ndataset_1 = kernel.complete_data(0)\n```\n\nThis will return a single specified dataset. Multiple datasets are\ntypically created so that some measure of confidence around each\nprediction can be created.\n\nSince we know what the original data looked like, we can cheat and see\nhow well the imputations compare to the original data:\n\n\n```python\nacclist = []\niterations = kernel.iteration_count()+1\nfor iteration in range(iterations):\n    species_na_count = kernel.na_counts['species']\n    compdat = kernel.complete_data(dataset=0,iteration=iteration)\n    \n    # Record the accuract of the imputations of species.\n    acclist.append(\n      round(1-sum(compdat['species'] != iris['species'])/species_na_count,2)\n    )\n\n# acclist shows the accuracy of the imputations over the iterations.\nacclist = pd.Series(acclist).rename(\"Species Imputation Accuracy\")\nacclist.index = range(iterations)\nacclist.index.name = \"Iteration\"\nacclist\n```\n\n\n\n\n    Iteration\n    0    0.35\n    1    0.81\n    2    0.81\n    3    0.78\n    Name: Species Imputation Accuracy, dtype: float64\n\n\n\nIn this instance, we went from a low accuracy (what is expected with\nrandom sampling) to a much higher accuracy.\n\n## The MICE Algorithm\n\nMultiple Imputation by Chained Equations \u2018fills in\u2019 (imputes) missing\ndata in a dataset through an iterative series of predictive models. In\neach iteration, each specified variable in the dataset is imputed using\nthe other variables in the dataset. These iterations should be run until\nit appears that convergence has been met.\n\n<img src=\"https://i.imgur.com/2L403kU.png\" style=\"display: block; margin: auto;\" />\n\nThis process is continued until all specified variables have been\nimputed. Additional iterations can be run if it appears that the average\nimputed values have not converged, although no more than 5 iterations\nare usually necessary.\n\n### Common Use Cases\n\n##### **Data Leakage:**\n\nMICE is particularly useful if missing values are associated with the\ntarget variable in a way that introduces leakage. For instance, let\u2019s\nsay you wanted to model customer retention at the time of sign up. A\ncertain variable is collected at sign up or 1 month after sign up. The\nabsence of that variable is a data leak, since it tells you that the\ncustomer did not retain for 1 month.\n\n##### **Funnel Analysis:**\n\nInformation is often collected at different stages of a \u2018funnel\u2019. MICE\ncan be used to make educated guesses about the characteristics of\nentities at different points in a funnel.\n\n##### **Confidence Intervals:**\n\nMICE can be used to impute missing values, however it is important to\nkeep in mind that these imputed values are a prediction. Creating\nmultiple datasets with different imputed values allows you to do two\ntypes of inference:\n\n  - Imputed Value Distribution: A profile can be built for each imputed\n    value, allowing you to make statements about the likely distribution\n    of that value.  \n  - Model Prediction Distribution: With multiple datasets, you can build\n    multiple models and create a distribution of predictions for each\n    sample. Those samples with imputed values which were not able to be\n    imputed with much confidence would have a larger variance in their\n    predictions.\n\n\n## Predictive Mean Matching\n\n`miceforest` can make use of a procedure called predictive mean matching\n(PMM) to select which values are imputed. PMM involves selecting a\ndatapoint from the original, nonmissing data (candidates) which has a\npredicted value close to the predicted value of the missing sample\n(bachelors). The closest N (`mean_match_candidates` parameter) values\nare selected, from which a value is chosen at random. This can be\nspecified on a column-by-column basis. Going into more detail from our\nexample above, we see how this works in practice:\n\n<img src=\"https://i.imgur.com/3DBCXnL.png\" style=\"display: block; margin: auto;\" />\n\nThis method is very useful if you have a variable which needs imputing\nwhich has any of the following characteristics:\n\n  - Multimodal  \n  - Integer  \n  - Skewed\n\n\n### Effects of Mean Matching\n\nAs an example, let\u2019s construct a dataset with some of the above\ncharacteristics:\n\n\n```python\nrandst = np.random.RandomState(1991)\n# random uniform variable\nnrws = 1000\nuniform_vec = randst.uniform(size=nrws)\n\ndef make_bimodal(mean1,mean2,size):\n    bimodal_1 = randst.normal(size=nrws, loc=mean1)\n    bimodal_2 = randst.normal(size=nrws, loc=mean2)\n    bimdvec = []\n    for i in range(size):\n        bimdvec.append(randst.choice([bimodal_1[i], bimodal_2[i]]))\n    return np.array(bimdvec)\n\n# Make 2 Bimodal Variables\nclose_bimodal_vec = make_bimodal(2,-2,nrws)\nfar_bimodal_vec = make_bimodal(3,-3,nrws)\n\n\n# Highly skewed variable correlated with Uniform_Variable\nskewed_vec = np.exp(uniform_vec*randst.uniform(size=nrws)*3) + randst.uniform(size=nrws)*3\n\n# Integer variable correlated with Close_Bimodal_Variable and Uniform_Variable\ninteger_vec = np.round(uniform_vec + close_bimodal_vec/3 + randst.uniform(size=nrws)*2)\n\n# Make a DataFrame\ndat = pd.DataFrame(\n    {\n    'uniform_var':uniform_vec,\n    'close_bimodal_var':close_bimodal_vec,\n    'far_bimodal_var':far_bimodal_vec,\n    'skewed_var':skewed_vec,\n    'integer_var':integer_vec\n    }\n)\n\n# Ampute the data.\nampdat = mf.ampute_data(dat,perc=0.25,random_state=randst)\n```\n\n\n```python\nimport plotnine as p9\nimport itertools\n\ndef plot_matrix(df, columns):\n    pdf = []\n    for a1, b1 in itertools.combinations(columns, 2):\n        for (a,b) in ((a1, b1), (b1, a1)):\n            sub = df[[a, b]].rename(columns={a: \"x\", b: \"y\"}).assign(a=a, b=b)\n            pdf.append(sub)\n\n    g = (\n        p9.ggplot(pd.concat(pdf))\n            + p9.geom_point(p9.aes('x','y'))\n            + p9.facet_grid('b~a', scales='free')\n            + p9.theme(figure_size=(7, 7))\n            + p9.xlab(\"\") + p9.ylab(\"\")\n    )\n    return g\n\nplot_matrix(dat, dat.columns)\n```\n\n\n    \n![png](README_files/README_60_0.png)\n    \n\n\nWe can see how our variables are distributed and correlated in the graph\nabove. Now let\u2019s run our imputation process twice, once using mean\nmatching, and once using the model prediction.\n\n\n```python\nkernel_mean_match = mf.ImputationKernel(\n    data=ampdat,\n    num_datasets=3,\n    mean_match_candidates=5,\n    random_state=1\n)\nkernel_mean_match.mice(2)\nkernel_no_mean_match = mf.ImputationKernel(\n    data=ampdat,\n    num_datasets=3,\n    mean_match_candidates=0,\n    random_state=1\n)\nkernel_no_mean_match.mice(2)\n```\n\n\n```python\nkernel_mean_match.plot_imputed_distributions()\n```\n\n\n    \n![png](README_files/README_63_0.png)\n    \n\n\n\n```python\nkernel_no_mean_match.plot_imputed_distributions()\n```\n\n\n    \n![png](README_files/README_64_0.png)\n    \n\n\nYou can see the effects that mean matching has, depending on the\ndistribution of the data. Simply returning the value from the model\nprediction, while it may provide a better \u2018fit\u2019, will not provide\nimputations with a similair distribution to the original. This may be\nbeneficial, depending on your goal.\n",
    "bugtrack_url": null,
    "license": "The MIT License (MIT)  Copyright (c) 2020 Samuel Wilson  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Multiple Imputation by Chained Equations with LightGBM",
    "version": "6.0.3",
    "project_urls": {
        "Homepage": "https://github.com/AnotherSamWilson/miceforest",
        "Issues": "https://github.com/AnotherSamWilson/miceforest/issues",
        "changelog": "https://github.com/AnotherSamWilson/miceforest/releases"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "45a2ea9ff5dcabcb6fef555174761c4acb320af4d9cba9f1ef212a3fc75c39a7",
                "md5": "93087c74f0646a52982f4dcdbd36fc93",
                "sha256": "a5a176391bda6348f4ebafeb99071989918ddd4a6dff47378aac5e5c36127b9c"
            },
            "downloads": -1,
            "filename": "miceforest-6.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "93087c74f0646a52982f4dcdbd36fc93",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 40317,
            "upload_time": "2024-08-02T00:43:48",
            "upload_time_iso_8601": "2024-08-02T00:43:48.933592Z",
            "url": "https://files.pythonhosted.org/packages/45/a2/ea9ff5dcabcb6fef555174761c4acb320af4d9cba9f1ef212a3fc75c39a7/miceforest-6.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f53cdfd74af1aa573a56fbe54a6a4c524586747ef636ef501295bb1d5bc9935b",
                "md5": "66f6c575053905491f0cfa97b53c07a9",
                "sha256": "e1f585f8b45332bc6d04ee35a157a3b1b64563e0391171e97fd6a8081968063c"
            },
            "downloads": -1,
            "filename": "miceforest-6.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "66f6c575053905491f0cfa97b53c07a9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 63048,
            "upload_time": "2024-08-02T00:43:50",
            "upload_time_iso_8601": "2024-08-02T00:43:50.999215Z",
            "url": "https://files.pythonhosted.org/packages/f5/3c/dfd74af1aa573a56fbe54a6a4c524586747ef636ef501295bb1d5bc9935b/miceforest-6.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-02 00:43:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AnotherSamWilson",
    "github_project": "miceforest",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "miceforest"
}
        
Elapsed time: 0.31061s