-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsimple_imputer.py
65 lines (49 loc) · 1.75 KB
/
simple_imputer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from sklearn.impute import SimpleImputer as SI
from niaaml.preprocessing.imputation.imputer import Imputer
import numpy as np
import pandas as pd
__all__ = ["SimpleImputer"]
class SimpleImputer(Imputer):
r"""Implementation of simple imputer.
Date:
2020
Author:
Luka Pečnik
License:
MIT
Documentation:
https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html
See Also:
* :class:`niaaml.preprocessing.imputation.Imputer`
"""
Name = "Simple Imputer"
def __init__(self, **kwargs):
r"""Initialize imputer."""
self.__simple_imputer = SI(missing_values=np.nan)
def fit(self, feature):
r"""Fit imputer.
Arguments:
feature (pandas.core.frame.DataFrame): A column from DataFrame of features.
"""
if not pd.api.types.is_numeric_dtype(feature.iloc[:, 0]):
replacement_val = feature.mode().iloc[0, 0]
self.__simple_imputer.set_params(
**{"fill_value": replacement_val, "strategy": "constant"}
)
self.__simple_imputer.fit(feature)
else:
self.__simple_imputer.fit(feature)
def transform(self, feature):
r"""Transform feature's values.
Arguments:
feature (pandas.core.frame.DataFrame): A column from DataFrame of features.
Returns:
pandas.core.frame.DataFrame: A transformed column.
"""
return self.__simple_imputer.transform(feature)
def to_string(self):
r"""User friendly representation of the object.
Returns:
str: User friendly representation of the object.
"""
return Imputer.to_string(self).format(name=self.Name)