|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | +from sklearn.base import BaseEstimator, TransformerMixin |
| 5 | +from sklearn.utils.validation import check_is_fitted |
| 6 | + |
| 7 | + |
| 8 | +def _get_mask(X, value): |
| 9 | + """ |
| 10 | + Compute the boolean mask X == missing_values. |
| 11 | + """ |
| 12 | + if value == "NaN" or \ |
| 13 | + value is None or \ |
| 14 | + (isinstance(value, float) and np.isnan(value)): |
| 15 | + return pd.isnull(X) |
| 16 | + else: |
| 17 | + return X == value |
| 18 | + |
| 19 | + |
| 20 | +class CategoricalImputer(BaseEstimator, TransformerMixin): |
| 21 | + """ |
| 22 | + Impute missing values from a categorical/string np.ndarray or pd.Series |
| 23 | + with the most frequent value on the training data. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + missing_values : string or "NaN", optional (default="NaN") |
| 28 | + The placeholder for the missing values. All occurrences of |
| 29 | + `missing_values` will be imputed. None and np.nan are treated |
| 30 | + as being the same, use the string value "NaN" for them. |
| 31 | +
|
| 32 | + copy : boolean, optional (default=True) |
| 33 | + If True, a copy of X will be created. |
| 34 | +
|
| 35 | + strategy : string, optional (default = 'most_frequent') |
| 36 | + The imputation strategy. |
| 37 | +
|
| 38 | + - If "most_frequent", then replace missing using the most frequent |
| 39 | + value along each column. Can be used with strings or numeric data. |
| 40 | + - If "constant", then replace missing values with fill_value. Can be |
| 41 | + used with strings or numeric data. |
| 42 | +
|
| 43 | + fill_value : string, optional (default='?') |
| 44 | + The value that all instances of `missing_values` are replaced |
| 45 | + with if `strategy` is set to `constant`. This is useful if |
| 46 | + you don't want to impute with the mode, or if there are multiple |
| 47 | + modes in your data and you want to choose a particular one. If |
| 48 | + `strategy` is not set to `constant`, this parameter is ignored. |
| 49 | +
|
| 50 | + Attributes |
| 51 | + ---------- |
| 52 | + fill_ : str |
| 53 | + The imputation fill value |
| 54 | +
|
| 55 | + """ |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + missing_values='NaN', |
| 60 | + strategy='most_frequent', |
| 61 | + fill_value='?', |
| 62 | + copy=True |
| 63 | + ): |
| 64 | + self.missing_values = missing_values |
| 65 | + self.copy = copy |
| 66 | + self.fill_value = fill_value |
| 67 | + self.strategy = strategy |
| 68 | + |
| 69 | + strategies = ['constant', 'most_frequent'] |
| 70 | + if self.strategy not in strategies: |
| 71 | + raise ValueError( |
| 72 | + 'Strategy {0} not in {1}'.format(self.strategy, strategies) |
| 73 | + ) |
| 74 | + |
| 75 | + def fit(self, X, y=None): |
| 76 | + """ |
| 77 | +
|
| 78 | + Get the most frequent value. |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | + X : np.ndarray or pd.Series |
| 83 | + Training data. |
| 84 | +
|
| 85 | + y : Passthrough for ``Pipeline`` compatibility. |
| 86 | +
|
| 87 | + Returns |
| 88 | + ------- |
| 89 | + self: CategoricalImputer |
| 90 | + """ |
| 91 | + |
| 92 | + mask = _get_mask(X, self.missing_values) |
| 93 | + X = X[~mask] |
| 94 | + if self.strategy == 'most_frequent': |
| 95 | + modes = pd.Series(X).mode() |
| 96 | + elif self.strategy == 'constant': |
| 97 | + modes = np.array([self.fill_value]) |
| 98 | + if modes.shape[0] == 0: |
| 99 | + raise ValueError('Data is empty or all values are null') |
| 100 | + elif modes.shape[0] > 1: |
| 101 | + raise ValueError('No value is repeated more than ' |
| 102 | + 'once in the column') |
| 103 | + else: |
| 104 | + self.fill_ = modes[0] |
| 105 | + |
| 106 | + return self |
| 107 | + |
| 108 | + def transform(self, X): |
| 109 | + """ |
| 110 | +
|
| 111 | + Replaces missing values in the input data with the most frequent value |
| 112 | + of the training data. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ---------- |
| 116 | + X : np.ndarray or pd.Series |
| 117 | + Data with values to be imputed. |
| 118 | +
|
| 119 | + Returns |
| 120 | + ------- |
| 121 | + np.ndarray |
| 122 | + Data with imputed values. |
| 123 | + """ |
| 124 | + |
| 125 | + check_is_fitted(self, 'fill_') |
| 126 | + |
| 127 | + if self.copy: |
| 128 | + X = X.copy() |
| 129 | + |
| 130 | + mask = _get_mask(X, self.missing_values) |
| 131 | + X[mask] = self.fill_ |
| 132 | + |
| 133 | + return np.asarray(X) |
| 134 | + |
| 135 | + |
| 136 | +class FunctionTransformer(BaseEstimator, TransformerMixin): |
| 137 | + """ |
| 138 | + Use this class to convert a random function into a |
| 139 | + transformer. |
| 140 | + """ |
| 141 | + |
| 142 | + def __init__(self, func): |
| 143 | + self.__func = func |
| 144 | + |
| 145 | + def fit(self, x, y=None): |
| 146 | + return self |
| 147 | + |
| 148 | + def transform(self, x): |
| 149 | + return np.vectorize(self.__func)(x) |
| 150 | + |
| 151 | + def __call__(self, *args, **kwargs): |
| 152 | + return self.__func(*args, **kwargs) |
0 commit comments