missingpy


Namemissingpy JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/epsilon-machine/missingpy
SummaryMissing Data Imputation for Python
upload_time2018-12-10 04:09:45
maintainer
docs_urlNone
authorAshim Bhattarai
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## missingpy

`missingpy` is a library for missing data imputation in Python. It has an 
API consistent with [scikit-learn](http://scikit-learn.org/stable/), so users 
already comfortable with that interface will find themselves in familiar 
terrain. Currently, the library supports k-Nearest Neighbors based 
imputation and Random Forest based imputation (MissForest) but we plan to add 
other imputation tools in the future so please stay tuned!

## Installation

`pip install missingpy`

## k-Nearest Neighbors (kNN) Imputation

### Example
```
# Let X be an array containing missing values
from missingpy import KNNImputer
imputer = KNNImputer()
X_imputed = imputer.fit_transform(X)
```

### Description
The `KNNImputer` class provides imputation for completing missing
values using the k-Nearest Neighbors approach. Each sample's missing values
are imputed using values from `n_neighbors` nearest neighbors found in the
training set. Note that if a sample has more than one feature missing, then
the sample can potentially have multiple sets of `n_neighbors`
donors depending on the particular feature being imputed.

Each missing feature is then imputed as the average, either weighted or
unweighted, of these neighbors. Where the number of donor neighbors is less
than `n_neighbors`, the training set average for that feature is used
for imputation. The total number of samples in the training set is, of course,
always greater than or equal to the number of nearest neighbors available for
imputation, depending on both the overall sample size as well as the number of
samples excluded from nearest neighbor calculation because of too many missing
features (as controlled by `row_max_missing`).
For more information on the methodology, see [1].

The following snippet demonstrates how to replace missing values,
encoded as `np.nan`, using the mean feature value of the two nearest
neighbors of the rows that contain the missing values::

    >>> import numpy as np
    >>> from missingpy import KNNImputer
    >>> nan = np.nan
    >>> X = [[1, 2, nan], [3, 4, 3], [nan, 6, 5], [8, 8, 7]]
    >>> imputer = KNNImputer(n_neighbors=2, weights="uniform")
    >>> imputer.fit_transform(X)
    array([[1. , 2. , 4. ],
           [3. , 4. , 3. ],
           [5.5, 6. , 5. ],
           [8. , 8. , 7. ]])

### API
    KNNImputer(missing_values="NaN", n_neighbors=5, weights="uniform", 
                     metric="masked_euclidean", row_max_missing=0.5, 
                     col_max_missing=0.8, copy=True)

    Parameters
    ----------
    missing_values : integer or "NaN", optional (default = "NaN")
        The placeholder for the missing values. All occurrences of
        `missing_values` will be imputed. For missing values encoded as
        ``np.nan``, use the string value "NaN".

    n_neighbors : int, optional (default = 5)
        Number of neighboring samples to use for imputation.

    weights : str or callable, optional (default = "uniform")
        Weight function used in prediction.  Possible values:

        - 'uniform' : uniform weights.  All points in each neighborhood
          are weighted equally.
        - 'distance' : weight 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.

    metric : str or callable, optional (default = "masked_euclidean")
        Distance metric for searching neighbors. Possible values:
        - 'masked_euclidean'
        - [callable] : a user-defined function which conforms to the
        definition of _pairwise_callable(X, Y, metric, **kwds). In other
        words, the function accepts two arrays, X and Y, and a
        ``missing_values`` keyword in **kwds and returns a scalar distance
        value.

    row_max_missing : float, optional (default = 0.5)
        The maximum fraction of columns (i.e. features) that can be missing
        before the sample is excluded from nearest neighbor imputation. It
        means that such rows will not be considered a potential donor in
        ``fit()``, and in ``transform()`` their missing feature values will be
        imputed to be the column mean for the entire dataset.

    col_max_missing : float, optional (default = 0.8)
        The maximum fraction of rows (or samples) that can be missing
        for any feature beyond which an error is raised.

    copy : boolean, optional (default = True)
        If True, a copy of X will be created. If False, imputation will
        be done in-place whenever possible. Note that, if metric is
        "masked_euclidean" and copy=False then missing_values in the
        input matrix X will be overwritten with zeros.

    Attributes
    ----------
    statistics_ : 1-D array of length {n_features}
        The 1-D array contains the mean of each feature calculated using
        observed (i.e. non-missing) values. This is used for imputing
        missing values in samples that are either excluded from nearest
        neighbors search because they have too many ( > row_max_missing)
        missing features or because all of the sample's k-nearest neighbors
        (i.e., the potential donors) also have the relevant feature value
        missing.

    Methods
    -------
    fit(X, y=None):
        Fit the imputer on X.

        Parameters
        ----------
        X : {array-like}, shape (n_samples, n_features)
            Input data, where ``n_samples`` is the number of samples and
            ``n_features`` is the number of features.

        Returns
        -------
        self : object
            Returns self.


    transform(X):
        Impute all missing values in X.

        Parameters
        ----------
        X : {array-like}, shape = [n_samples, n_features]
            The input data to complete.

        Returns
        -------
        X : {array-like}, shape = [n_samples, n_features]
            The imputed dataset.


    fit_transform(X, y=None, **fit_params):
        Fit KNNImputer and impute all missing values in X.

        Parameters
        ----------
        X : {array-like}, shape (n_samples, n_features)
            Input data, where ``n_samples`` is the number of samples and
            ``n_features`` is the number of features.

        Returns
        -------
        X : {array-like}, shape (n_samples, n_features)
            Returns imputed dataset.       

### References
1. Olga Troyanskaya, Michael Cantor, Gavin Sherlock, Pat Brown, Trevor
    Hastie, Robert Tibshirani, David Botstein and Russ B. Altman, Missing value
    estimation methods for DNA microarrays, BIOINFORMATICS Vol. 17 no. 6, 2001
    Pages 520-525.

## Random Forest Imputation (MissForest)

### Example
```
# Let X be an array containing missing values
from missingpy import MissForest
imputer = MissForest()
X_imputed = imputer.fit_transform(X)
```

### Description
MissForest imputes missing values using Random Forests in an iterative
fashion [1]. By default, the imputer begins imputing missing values of the
column (which is expected to be a variable) with the smallest number of
missing values -- let's call this the candidate column.
The first step involves filling any missing values of the remaining,
non-candidate, columns with an initial guess, which is the column mean for
columns representing numerical variables and the column mode for columns
representing categorical variables. Note that the categorical variables 
need to be explicitly identified during the imputer's `fit()` method call 
(see API for more information). After that, the imputer fits a random
forest model with the candidate column as the outcome variable and the
remaining columns as the predictors over all rows where the candidate
column values are not missing.
After the fit, the missing rows of the candidate column are
imputed using the prediction from the fitted Random Forest. The
rows of the non-candidate columns act as the input data for the fitted
model.
Following this, the imputer moves on to the next candidate column with the
second smallest number of missing values from among the non-candidate
columns in the first round. The process repeats itself for each column
with a missing value, possibly over multiple iterations or epochs for
each column, until the stopping criterion is met.
The stopping criterion is governed by the "difference" between the imputed
arrays over successive iterations. For numerical variables (`num_vars_`),
the difference is defined as follows:

     sum((X_new[:, num_vars_] - X_old[:, num_vars_]) ** 2) /
     sum((X_new[:, num_vars_]) ** 2)

For categorical variables(`cat_vars_`), the difference is defined as follows:

    sum(X_new[:, cat_vars_] != X_old[:, cat_vars_])) / n_cat_missing

where `X_new` is the newly imputed array, `X_old` is the array imputed in the
previous round, `n_cat_missing` is the total number of categorical
values that are missing, and the `sum()` is performed both across rows
and columns. Following [1], the stopping criterion is considered to have
been met when difference between `X_new` and `X_old` increases for the first
time for both types of variables (if available).


    >>> from missingpy import MissForest
    >>> nan = float("NaN")
    >>> X = [[1, 2, nan], [3, 4, 3], [nan, 6, 5], [8, 8, 7]]
    >>> imputer = MissForest()
    >>> imputer.fit_transform(X)
    Iteration: 0
    Iteration: 1
    Iteration: 2
    Iteration: 3
    array([[1.  , 2. , 4. ],
           [3.  , 4. , 3. ],
           [3.16, 6. , 5. ],
           [8.  , 8. , 7. ]])

### API
    MissForest(max_iter=10, decreasing=False, missing_values=np.nan,
                 copy=True, n_estimators=100, criterion=('mse', 'gini'),
                 max_depth=None, min_samples_split=2, min_samples_leaf=1,
                 min_weight_fraction_leaf=0.0, max_features='auto',
                 max_leaf_nodes=None, min_impurity_decrease=0.0,
                 bootstrap=True, oob_score=False, n_jobs=-1, random_state=None,
                 verbose=0, warm_start=False, class_weight=None)

    Parameters
    ----------
    NOTE: Most parameter definitions below are taken verbatim from the
    Scikit-Learn documentation at [2] and [3].

    max_iter : int, optional (default = 10)
        The maximum iterations of the imputation process. Each column with a
        missing value is imputed exactly once in a given iteration.

    decreasing : boolean, optional (default = False)
        If set to True, columns are sorted according to decreasing number of
        missing values. In other words, imputation will move from imputing
        columns with the largest number of missing values to columns with
        fewest number of missing values.

    missing_values : np.nan, integer, optional (default = np.nan)
        The placeholder for the missing values. All occurrences of
        `missing_values` will be imputed.

    copy : boolean, optional (default = True)
        If True, a copy of X will be created. If False, imputation will
        be done in-place whenever possible.

    criterion : tuple, optional (default = ('mse', 'gini'))
        The function to measure the quality of a split.The first element of
        the tuple is for the Random Forest Regressor (for imputing numerical
        variables) while the second element is for the Random Forest
        Classifier (for imputing categorical variables).

    n_estimators : integer, optional (default=100)
        The number of trees in the forest.

    max_depth : integer or None, optional (default=None)
        The maximum depth of the tree. If None, then nodes are expanded until
        all leaves are pure or until all leaves contain less than
        min_samples_split samples.

    min_samples_split : int, float, optional (default=2)
        The minimum number of samples required to split an internal node:
        - If int, then consider `min_samples_split` as the minimum number.
        - If float, then `min_samples_split` is a fraction and
          `ceil(min_samples_split * n_samples)` are the minimum
          number of samples for each split.

    min_samples_leaf : int, float, optional (default=1)
        The minimum number of samples required to be at a leaf node.
        A split point at any depth will only be considered if it leaves at
        least ``min_samples_leaf`` training samples in each of the left and
        right branches.  This may have the effect of smoothing the model,
        especially in regression.
        - If int, then consider `min_samples_leaf` as the minimum number.
        - If float, then `min_samples_leaf` is a fraction and
          `ceil(min_samples_leaf * n_samples)` are the minimum
          number of samples for each node.

    min_weight_fraction_leaf : float, optional (default=0.)
        The minimum weighted fraction of the sum total of weights (of all
        the input samples) required to be at a leaf node. Samples have
        equal weight when sample_weight is not provided.

    max_features : int, float, string or None, optional (default="auto")
        The number of features to consider when looking for the best split:
        - If int, then consider `max_features` features at each split.
        - If float, then `max_features` is a fraction and
          `int(max_features * n_features)` features are considered at each
          split.
        - If "auto", then `max_features=sqrt(n_features)`.
        - If "sqrt", then `max_features=sqrt(n_features)` (same as "auto").
        - If "log2", then `max_features=log2(n_features)`.
        - If None, then `max_features=n_features`.
        Note: the search for a split does not stop until at least one
        valid partition of the node samples is found, even if it requires to
        effectively inspect more than ``max_features`` features.

    max_leaf_nodes : int or None, optional (default=None)
        Grow trees with ``max_leaf_nodes`` in best-first fashion.
        Best nodes are defined as relative reduction in impurity.
        If None then unlimited number of leaf nodes.

    min_impurity_decrease : float, optional (default=0.)
        A node will be split if this split induces a decrease of the impurity
        greater than or equal to this value.
        The weighted impurity decrease equation is the following::
            N_t / N * (impurity - N_t_R / N_t * right_impurity
                                - N_t_L / N_t * left_impurity)
        where ``N`` is the total number of samples, ``N_t`` is the number of
        samples at the current node, ``N_t_L`` is the number of samples in the
        left child, and ``N_t_R`` is the number of samples in the right child.
        ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum,
        if ``sample_weight`` is passed.

    bootstrap : boolean, optional (default=True)
        Whether bootstrap samples are used when building trees.

    oob_score : bool (default=False)
        Whether to use out-of-bag samples to estimate
        the generalization accuracy.

    n_jobs : int or None, optional (default=None)
        The number of jobs to run in parallel for both `fit` and `predict`.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    verbose : int, optional (default=0)
        Controls the verbosity when fitting and predicting.

    warm_start : bool, optional (default=False)
        When set to ``True``, reuse the solution of the previous call to fit
        and add more estimators to the ensemble, otherwise, just fit a whole
        new forest. See :term:`the Glossary <warm_start>`.

    class_weight : dict, list of dicts, "balanced", "balanced_subsample" or \
    None, optional (default=None)
        Weights associated with classes in the form ``{class_label: weight}``.
        If not given, all classes are supposed to have weight one. For
        multi-output problems, a list of dicts can be provided in the same
        order as the columns of y.
        Note that for multioutput (including multilabel) weights should be
        defined for each class of every column in its own dict. For example,
        for four-class multilabel classification weights should be
        [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of
        [{1:1}, {2:5}, {3:1}, {4:1}].
        The "balanced" mode uses the values of y to automatically adjust
        weights inversely proportional to class frequencies in the input data
        as ``n_samples / (n_classes * np.bincount(y))``
        The "balanced_subsample" mode is the same as "balanced" except that
        weights are computed based on the bootstrap sample for every tree
        grown.
        For multi-output, the weights of each column of y will be multiplied.
        Note that these weights will be multiplied with sample_weight (passed
        through the fit method) if sample_weight is specified.
        NOTE: This parameter is only applicable for Random Forest Classifier
        objects (i.e., for categorical variables).

    Attributes
    ----------
    statistics_ : Dictionary of length two
        The first element is an array with the mean of each numerical feature
        being imputed while the second element is an array of modes of
        categorical features being imputed (if available, otherwise it
        will be None).

    Methods
    -------
    fit(self, X, y=None, cat_vars=None):
        Fit the imputer on X.

        Parameters
        ----------
        X : {array-like}, shape (n_samples, n_features)
            Input data, where ``n_samples`` is the number of samples and
            ``n_features`` is the number of features.

        cat_vars : int or array of ints, optional (default = None)
            An int or an array containing column indices of categorical
            variable(s)/feature(s) present in the dataset X.
            ``None`` if there are no categorical variables in the dataset.

        Returns
        -------
        self : object
            Returns self.


    transform(X):
        Impute all missing values in X.

        Parameters
        ----------
        X : {array-like}, shape = [n_samples, n_features]
            The input data to complete.

        Returns
        -------
        X : {array-like}, shape = [n_samples, n_features]
            The imputed dataset.


    fit_transform(X, y=None, **fit_params):
        Fit MissForest and impute all missing values in X.

        Parameters
        ----------
        X : {array-like}, shape (n_samples, n_features)
            Input data, where ``n_samples`` is the number of samples and
            ``n_features`` is the number of features.

        Returns
        -------
        X : {array-like}, shape (n_samples, n_features)
            Returns imputed dataset.

### References

* [1] Stekhoven, Daniel J., and Peter Bühlmann. "MissForest—non-parametric
  missing value imputation for mixed-type data." Bioinformatics 28.1
  (2011): 112-118.
* [2] https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor
* [3] https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/epsilon-machine/missingpy",
    "name": "missingpy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Ashim Bhattarai",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/20/ef/2c8b77dc55f0e1af9eb9b01ed220abf3957ae205c7b355951a02783416a0/missingpy-0.2.0.tar.gz",
    "platform": "",
    "description": "## missingpy\n\n`missingpy` is a library for missing data imputation in Python. It has an \nAPI consistent with [scikit-learn](http://scikit-learn.org/stable/), so users \nalready comfortable with that interface will find themselves in familiar \nterrain. Currently, the library supports k-Nearest Neighbors based \nimputation and Random Forest based imputation (MissForest) but we plan to add \nother imputation tools in the future so please stay tuned!\n\n## Installation\n\n`pip install missingpy`\n\n## k-Nearest Neighbors (kNN) Imputation\n\n### Example\n```\n# Let X be an array containing missing values\nfrom missingpy import KNNImputer\nimputer = KNNImputer()\nX_imputed = imputer.fit_transform(X)\n```\n\n### Description\nThe `KNNImputer` class provides imputation for completing missing\nvalues using the k-Nearest Neighbors approach. Each sample's missing values\nare imputed using values from `n_neighbors` nearest neighbors found in the\ntraining set. Note that if a sample has more than one feature missing, then\nthe sample can potentially have multiple sets of `n_neighbors`\ndonors depending on the particular feature being imputed.\n\nEach missing feature is then imputed as the average, either weighted or\nunweighted, of these neighbors. Where the number of donor neighbors is less\nthan `n_neighbors`, the training set average for that feature is used\nfor imputation. The total number of samples in the training set is, of course,\nalways greater than or equal to the number of nearest neighbors available for\nimputation, depending on both the overall sample size as well as the number of\nsamples excluded from nearest neighbor calculation because of too many missing\nfeatures (as controlled by `row_max_missing`).\nFor more information on the methodology, see [1].\n\nThe following snippet demonstrates how to replace missing values,\nencoded as `np.nan`, using the mean feature value of the two nearest\nneighbors of the rows that contain the missing values::\n\n    >>> import numpy as np\n    >>> from missingpy import KNNImputer\n    >>> nan = np.nan\n    >>> X = [[1, 2, nan], [3, 4, 3], [nan, 6, 5], [8, 8, 7]]\n    >>> imputer = KNNImputer(n_neighbors=2, weights=\"uniform\")\n    >>> imputer.fit_transform(X)\n    array([[1. , 2. , 4. ],\n           [3. , 4. , 3. ],\n           [5.5, 6. , 5. ],\n           [8. , 8. , 7. ]])\n\n### API\n    KNNImputer(missing_values=\"NaN\", n_neighbors=5, weights=\"uniform\", \n                     metric=\"masked_euclidean\", row_max_missing=0.5, \n                     col_max_missing=0.8, copy=True)\n\n    Parameters\n    ----------\n    missing_values : integer or \"NaN\", optional (default = \"NaN\")\n        The placeholder for the missing values. All occurrences of\n        `missing_values` will be imputed. For missing values encoded as\n        ``np.nan``, use the string value \"NaN\".\n\n    n_neighbors : int, optional (default = 5)\n        Number of neighboring samples to use for imputation.\n\n    weights : str or callable, optional (default = \"uniform\")\n        Weight function used in prediction.  Possible values:\n\n        - 'uniform' : uniform weights.  All points in each neighborhood\n          are weighted equally.\n        - 'distance' : weight points by the inverse of their distance.\n          in this case, closer neighbors of a query point will have a\n          greater influence than neighbors which are further away.\n        - [callable] : a user-defined function which accepts an\n          array of distances, and returns an array of the same shape\n          containing the weights.\n\n    metric : str or callable, optional (default = \"masked_euclidean\")\n        Distance metric for searching neighbors. Possible values:\n        - 'masked_euclidean'\n        - [callable] : a user-defined function which conforms to the\n        definition of _pairwise_callable(X, Y, metric, **kwds). In other\n        words, the function accepts two arrays, X and Y, and a\n        ``missing_values`` keyword in **kwds and returns a scalar distance\n        value.\n\n    row_max_missing : float, optional (default = 0.5)\n        The maximum fraction of columns (i.e. features) that can be missing\n        before the sample is excluded from nearest neighbor imputation. It\n        means that such rows will not be considered a potential donor in\n        ``fit()``, and in ``transform()`` their missing feature values will be\n        imputed to be the column mean for the entire dataset.\n\n    col_max_missing : float, optional (default = 0.8)\n        The maximum fraction of rows (or samples) that can be missing\n        for any feature beyond which an error is raised.\n\n    copy : boolean, optional (default = True)\n        If True, a copy of X will be created. If False, imputation will\n        be done in-place whenever possible. Note that, if metric is\n        \"masked_euclidean\" and copy=False then missing_values in the\n        input matrix X will be overwritten with zeros.\n\n    Attributes\n    ----------\n    statistics_ : 1-D array of length {n_features}\n        The 1-D array contains the mean of each feature calculated using\n        observed (i.e. non-missing) values. This is used for imputing\n        missing values in samples that are either excluded from nearest\n        neighbors search because they have too many ( > row_max_missing)\n        missing features or because all of the sample's k-nearest neighbors\n        (i.e., the potential donors) also have the relevant feature value\n        missing.\n\n    Methods\n    -------\n    fit(X, y=None):\n        Fit the imputer on X.\n\n        Parameters\n        ----------\n        X : {array-like}, shape (n_samples, n_features)\n            Input data, where ``n_samples`` is the number of samples and\n            ``n_features`` is the number of features.\n\n        Returns\n        -------\n        self : object\n            Returns self.\n\n\n    transform(X):\n        Impute all missing values in X.\n\n        Parameters\n        ----------\n        X : {array-like}, shape = [n_samples, n_features]\n            The input data to complete.\n\n        Returns\n        -------\n        X : {array-like}, shape = [n_samples, n_features]\n            The imputed dataset.\n\n\n    fit_transform(X, y=None, **fit_params):\n        Fit KNNImputer and impute all missing values in X.\n\n        Parameters\n        ----------\n        X : {array-like}, shape (n_samples, n_features)\n            Input data, where ``n_samples`` is the number of samples and\n            ``n_features`` is the number of features.\n\n        Returns\n        -------\n        X : {array-like}, shape (n_samples, n_features)\n            Returns imputed dataset.       \n\n### References\n1. Olga Troyanskaya, Michael Cantor, Gavin Sherlock, Pat Brown, Trevor\n    Hastie, Robert Tibshirani, David Botstein and Russ B. Altman, Missing value\n    estimation methods for DNA microarrays, BIOINFORMATICS Vol. 17 no. 6, 2001\n    Pages 520-525.\n\n## Random Forest Imputation (MissForest)\n\n### Example\n```\n# Let X be an array containing missing values\nfrom missingpy import MissForest\nimputer = MissForest()\nX_imputed = imputer.fit_transform(X)\n```\n\n### Description\nMissForest imputes missing values using Random Forests in an iterative\nfashion [1]. By default, the imputer begins imputing missing values of the\ncolumn (which is expected to be a variable) with the smallest number of\nmissing values -- let's call this the candidate column.\nThe first step involves filling any missing values of the remaining,\nnon-candidate, columns with an initial guess, which is the column mean for\ncolumns representing numerical variables and the column mode for columns\nrepresenting categorical variables. Note that the categorical variables \nneed to be explicitly identified during the imputer's `fit()` method call \n(see API for more information). After that, the imputer fits a random\nforest model with the candidate column as the outcome variable and the\nremaining columns as the predictors over all rows where the candidate\ncolumn values are not missing.\nAfter the fit, the missing rows of the candidate column are\nimputed using the prediction from the fitted Random Forest. The\nrows of the non-candidate columns act as the input data for the fitted\nmodel.\nFollowing this, the imputer moves on to the next candidate column with the\nsecond smallest number of missing values from among the non-candidate\ncolumns in the first round. The process repeats itself for each column\nwith a missing value, possibly over multiple iterations or epochs for\neach column, until the stopping criterion is met.\nThe stopping criterion is governed by the \"difference\" between the imputed\narrays over successive iterations. For numerical variables (`num_vars_`),\nthe difference is defined as follows:\n\n     sum((X_new[:, num_vars_] - X_old[:, num_vars_]) ** 2) /\n     sum((X_new[:, num_vars_]) ** 2)\n\nFor categorical variables(`cat_vars_`), the difference is defined as follows:\n\n    sum(X_new[:, cat_vars_] != X_old[:, cat_vars_])) / n_cat_missing\n\nwhere `X_new` is the newly imputed array, `X_old` is the array imputed in the\nprevious round, `n_cat_missing` is the total number of categorical\nvalues that are missing, and the `sum()` is performed both across rows\nand columns. Following [1], the stopping criterion is considered to have\nbeen met when difference between `X_new` and `X_old` increases for the first\ntime for both types of variables (if available).\n\n\n    >>> from missingpy import MissForest\n    >>> nan = float(\"NaN\")\n    >>> X = [[1, 2, nan], [3, 4, 3], [nan, 6, 5], [8, 8, 7]]\n    >>> imputer = MissForest()\n    >>> imputer.fit_transform(X)\n    Iteration: 0\n    Iteration: 1\n    Iteration: 2\n    Iteration: 3\n    array([[1.  , 2. , 4. ],\n           [3.  , 4. , 3. ],\n           [3.16, 6. , 5. ],\n           [8.  , 8. , 7. ]])\n\n### API\n    MissForest(max_iter=10, decreasing=False, missing_values=np.nan,\n                 copy=True, n_estimators=100, criterion=('mse', 'gini'),\n                 max_depth=None, min_samples_split=2, min_samples_leaf=1,\n                 min_weight_fraction_leaf=0.0, max_features='auto',\n                 max_leaf_nodes=None, min_impurity_decrease=0.0,\n                 bootstrap=True, oob_score=False, n_jobs=-1, random_state=None,\n                 verbose=0, warm_start=False, class_weight=None)\n\n    Parameters\n    ----------\n    NOTE: Most parameter definitions below are taken verbatim from the\n    Scikit-Learn documentation at [2] and [3].\n\n    max_iter : int, optional (default = 10)\n        The maximum iterations of the imputation process. Each column with a\n        missing value is imputed exactly once in a given iteration.\n\n    decreasing : boolean, optional (default = False)\n        If set to True, columns are sorted according to decreasing number of\n        missing values. In other words, imputation will move from imputing\n        columns with the largest number of missing values to columns with\n        fewest number of missing values.\n\n    missing_values : np.nan, integer, optional (default = np.nan)\n        The placeholder for the missing values. All occurrences of\n        `missing_values` will be imputed.\n\n    copy : boolean, optional (default = True)\n        If True, a copy of X will be created. If False, imputation will\n        be done in-place whenever possible.\n\n    criterion : tuple, optional (default = ('mse', 'gini'))\n        The function to measure the quality of a split.The first element of\n        the tuple is for the Random Forest Regressor (for imputing numerical\n        variables) while the second element is for the Random Forest\n        Classifier (for imputing categorical variables).\n\n    n_estimators : integer, optional (default=100)\n        The number of trees in the forest.\n\n    max_depth : integer or None, optional (default=None)\n        The maximum depth of the tree. If None, then nodes are expanded until\n        all leaves are pure or until all leaves contain less than\n        min_samples_split samples.\n\n    min_samples_split : int, float, optional (default=2)\n        The minimum number of samples required to split an internal node:\n        - If int, then consider `min_samples_split` as the minimum number.\n        - If float, then `min_samples_split` is a fraction and\n          `ceil(min_samples_split * n_samples)` are the minimum\n          number of samples for each split.\n\n    min_samples_leaf : int, float, optional (default=1)\n        The minimum number of samples required to be at a leaf node.\n        A split point at any depth will only be considered if it leaves at\n        least ``min_samples_leaf`` training samples in each of the left and\n        right branches.  This may have the effect of smoothing the model,\n        especially in regression.\n        - If int, then consider `min_samples_leaf` as the minimum number.\n        - If float, then `min_samples_leaf` is a fraction and\n          `ceil(min_samples_leaf * n_samples)` are the minimum\n          number of samples for each node.\n\n    min_weight_fraction_leaf : float, optional (default=0.)\n        The minimum weighted fraction of the sum total of weights (of all\n        the input samples) required to be at a leaf node. Samples have\n        equal weight when sample_weight is not provided.\n\n    max_features : int, float, string or None, optional (default=\"auto\")\n        The number of features to consider when looking for the best split:\n        - If int, then consider `max_features` features at each split.\n        - If float, then `max_features` is a fraction and\n          `int(max_features * n_features)` features are considered at each\n          split.\n        - If \"auto\", then `max_features=sqrt(n_features)`.\n        - If \"sqrt\", then `max_features=sqrt(n_features)` (same as \"auto\").\n        - If \"log2\", then `max_features=log2(n_features)`.\n        - If None, then `max_features=n_features`.\n        Note: the search for a split does not stop until at least one\n        valid partition of the node samples is found, even if it requires to\n        effectively inspect more than ``max_features`` features.\n\n    max_leaf_nodes : int or None, optional (default=None)\n        Grow trees with ``max_leaf_nodes`` in best-first fashion.\n        Best nodes are defined as relative reduction in impurity.\n        If None then unlimited number of leaf nodes.\n\n    min_impurity_decrease : float, optional (default=0.)\n        A node will be split if this split induces a decrease of the impurity\n        greater than or equal to this value.\n        The weighted impurity decrease equation is the following::\n            N_t / N * (impurity - N_t_R / N_t * right_impurity\n                                - N_t_L / N_t * left_impurity)\n        where ``N`` is the total number of samples, ``N_t`` is the number of\n        samples at the current node, ``N_t_L`` is the number of samples in the\n        left child, and ``N_t_R`` is the number of samples in the right child.\n        ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum,\n        if ``sample_weight`` is passed.\n\n    bootstrap : boolean, optional (default=True)\n        Whether bootstrap samples are used when building trees.\n\n    oob_score : bool (default=False)\n        Whether to use out-of-bag samples to estimate\n        the generalization accuracy.\n\n    n_jobs : int or None, optional (default=None)\n        The number of jobs to run in parallel for both `fit` and `predict`.\n        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.\n        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`\n        for more details.\n\n    random_state : int, RandomState instance or None, optional (default=None)\n        If int, random_state is the seed used by the random number generator;\n        If RandomState instance, random_state is the random number generator;\n        If None, the random number generator is the RandomState instance used\n        by `np.random`.\n\n    verbose : int, optional (default=0)\n        Controls the verbosity when fitting and predicting.\n\n    warm_start : bool, optional (default=False)\n        When set to ``True``, reuse the solution of the previous call to fit\n        and add more estimators to the ensemble, otherwise, just fit a whole\n        new forest. See :term:`the Glossary <warm_start>`.\n\n    class_weight : dict, list of dicts, \"balanced\", \"balanced_subsample\" or \\\n    None, optional (default=None)\n        Weights associated with classes in the form ``{class_label: weight}``.\n        If not given, all classes are supposed to have weight one. For\n        multi-output problems, a list of dicts can be provided in the same\n        order as the columns of y.\n        Note that for multioutput (including multilabel) weights should be\n        defined for each class of every column in its own dict. For example,\n        for four-class multilabel classification weights should be\n        [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of\n        [{1:1}, {2:5}, {3:1}, {4:1}].\n        The \"balanced\" mode uses the values of y to automatically adjust\n        weights inversely proportional to class frequencies in the input data\n        as ``n_samples / (n_classes * np.bincount(y))``\n        The \"balanced_subsample\" mode is the same as \"balanced\" except that\n        weights are computed based on the bootstrap sample for every tree\n        grown.\n        For multi-output, the weights of each column of y will be multiplied.\n        Note that these weights will be multiplied with sample_weight (passed\n        through the fit method) if sample_weight is specified.\n        NOTE: This parameter is only applicable for Random Forest Classifier\n        objects (i.e., for categorical variables).\n\n    Attributes\n    ----------\n    statistics_ : Dictionary of length two\n        The first element is an array with the mean of each numerical feature\n        being imputed while the second element is an array of modes of\n        categorical features being imputed (if available, otherwise it\n        will be None).\n\n    Methods\n    -------\n    fit(self, X, y=None, cat_vars=None):\n        Fit the imputer on X.\n\n        Parameters\n        ----------\n        X : {array-like}, shape (n_samples, n_features)\n            Input data, where ``n_samples`` is the number of samples and\n            ``n_features`` is the number of features.\n\n        cat_vars : int or array of ints, optional (default = None)\n            An int or an array containing column indices of categorical\n            variable(s)/feature(s) present in the dataset X.\n            ``None`` if there are no categorical variables in the dataset.\n\n        Returns\n        -------\n        self : object\n            Returns self.\n\n\n    transform(X):\n        Impute all missing values in X.\n\n        Parameters\n        ----------\n        X : {array-like}, shape = [n_samples, n_features]\n            The input data to complete.\n\n        Returns\n        -------\n        X : {array-like}, shape = [n_samples, n_features]\n            The imputed dataset.\n\n\n    fit_transform(X, y=None, **fit_params):\n        Fit MissForest and impute all missing values in X.\n\n        Parameters\n        ----------\n        X : {array-like}, shape (n_samples, n_features)\n            Input data, where ``n_samples`` is the number of samples and\n            ``n_features`` is the number of features.\n\n        Returns\n        -------\n        X : {array-like}, shape (n_samples, n_features)\n            Returns imputed dataset.\n\n### References\n\n* [1] Stekhoven, Daniel J., and Peter B\u00fchlmann. \"MissForest\u2014non-parametric\n  missing value imputation for mixed-type data.\" Bioinformatics 28.1\n  (2011): 112-118.\n* [2] https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor\n* [3] https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Missing Data Imputation for Python",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/epsilon-machine/missingpy"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b5be998d04d27054b58f0974b5f09f8457778a0a72d4355e0b7ae877b6cfb850",
                "md5": "37c96c62c75c102839c18c4fc7723e29",
                "sha256": "d48fabaaf07284aed704c4e78872a58fdd7f161dd6f71de4cb73c1fe5ecca322"
            },
            "downloads": -1,
            "filename": "missingpy-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "37c96c62c75c102839c18c4fc7723e29",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 49090,
            "upload_time": "2018-12-10T04:09:43",
            "upload_time_iso_8601": "2018-12-10T04:09:43.339274Z",
            "url": "https://files.pythonhosted.org/packages/b5/be/998d04d27054b58f0974b5f09f8457778a0a72d4355e0b7ae877b6cfb850/missingpy-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "20ef2c8b77dc55f0e1af9eb9b01ed220abf3957ae205c7b355951a02783416a0",
                "md5": "8ef4daf2e3626f80121308619cb05277",
                "sha256": "5aacf5b440e443a1cd1eb04ba168402a7a08c3ec5869d2b0130d8d235d8611c1"
            },
            "downloads": -1,
            "filename": "missingpy-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8ef4daf2e3626f80121308619cb05277",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 34236,
            "upload_time": "2018-12-10T04:09:45",
            "upload_time_iso_8601": "2018-12-10T04:09:45.128774Z",
            "url": "https://files.pythonhosted.org/packages/20/ef/2c8b77dc55f0e1af9eb9b01ed220abf3957ae205c7b355951a02783416a0/missingpy-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2018-12-10 04:09:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "epsilon-machine",
    "github_project": "missingpy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "missingpy"
}
        
Elapsed time: 0.58927s