Preprocess data¶
LabelEncoder
¶
Bases: object
Label Encode categorical values for multiple columns at once
NOTE:
Shamlessly copied from https://github.com/jrzaurin/pytorch-widedeep
NOTE:
LabelEncoder reserves 0 for
unseen
new categories. This is convenient
when defining the embedding layers, since we can just set padding idx to 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns_to_encode
|
list, Optional, default = None
|
List of strings containing
the names of the columns to encode. If |
None
|
Attributes:
Name | Type | Description |
---|---|---|
encoding_dict |
Dict
|
Dictionary containing the encoding mappings in the format,
e.g. : |
inverse_encoding_dict(Dict) |
Dict
|
Dictionary containing the inverse encoding mappings
in the format, e.g. : |
Source code in inference_model/preprocessing/label_encoder.py
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
fit(df)
¶
Creates encoding attributes
Returns:
Name | Type | Description |
---|---|---|
LabelEncoder |
LabelEncoder
|
|
Source code in inference_model/preprocessing/label_encoder.py
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 66 67 68 69 70 71 72 73 74 75 |
|
fit_transform(df)
¶
Combines fit
and transform
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: label-encoded dataframe |
Examples:
>>> import pandas as pd
>>> from data_preparation.label_encoder import LabelEncoder
>>> df = pd.DataFrame({'col1': [1,2,3], 'col2': ['me', 'you', 'him']})
>>> columns_to_encode = ['col2']
>>> encoder = LabelEncoder(columns_to_encode)
>>> encoder.fit_transform(df)
col1 col2
0 1 1
1 2 2
2 3 3
>>> encoder.encoding_dict
{'col2': {'me': 1, 'you': 2, 'him': 3}}
Source code in inference_model/preprocessing/label_encoder.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
inverse_transform(df)
¶
Returns the original categories
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: label-encoded dataframe |
Examples:
>>> import pandas as pd
>>> from data_preparation.label_encoder import LabelEncoder
>>> df = pd.DataFrame({'col1': [1,2,3], 'col2': ['me', 'you', 'him']})
>>> columns_to_encode = ['col2']
>>> encoder = LabelEncoder(columns_to_encode)
>>> df_enc = encoder.fit_transform(df)
>>> encoder.inverse_transform(df_enc)
col1 col2
0 1 me
1 2 you
2 3 him
Source code in inference_model/preprocessing/label_encoder.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
transform(df)
¶
Label Encoded the categories in columns_to_encode
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: label-encoded dataframe |
Source code in inference_model/preprocessing/label_encoder.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
drop_constant_cols(df, verbose=False)
¶
Returns dataframe without constant columns, i.e. those with just 1 unique value for all rows.
Source code in inference_model/preprocessing/preprocess_data.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
drop_high_nan_cols(df, threshold=0.8, verbose=False)
¶
Returns dataframe without columns that have ratio of missingness above threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
input dataframe |
required |
threshold
|
float = 0.8
|
ratio of missingness applied per column |
0.8
|
verbose
|
bool
|
whether the output should be verbose |
False
|
Source code in inference_model/preprocessing/preprocess_data.py
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 |
|
drop_high_uq_cat_cols(df, cat_cols, uq_val_count, verbose=False)
¶
Returns dataframe without categorical columns that have too many unique values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
input dataframe |
required |
cat_cols
|
list
|
list of categorical columns |
required |
uq_val_count
|
int
|
unique value count |
required |
verbose
|
bool
|
whether the output should be verbose |
False
|
Source code in inference_model/preprocessing/preprocess_data.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
drop_highly_correlated_columns(df, cont_cols, crosscorr_val=0.95, verbose=False)
¶
Returns dataframe without highly correlated columns, cross correlation is evaluated with crosscorr_val.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
input dataframe |
required |
cont_cols
|
list
|
list of columns to evaluate correlation for |
required |
crosscorr_val
|
float = 0.95
|
threshold value of correlation |
0.95
|
verbose
|
bool
|
whether the output should be verbose |
False
|
Source code in inference_model/preprocessing/preprocess_data.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
most_frequent_in_list_col(dfs)
¶
Returns pd.Series with the most frequent values in the lists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dfs
|
Series
|
input pandas series containing string list values |
required |
Source code in inference_model/preprocessing/preprocess_data.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
nan_with_number_imputer(df, columns, fill_number=-1.0, verbose=False)
¶
Fills NAs with surrogate float, -1 is default value used, it can be customized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
input dataframe |
required |
columns
|
List[str]
|
list of columns that will be filled |
required |
fill_number
|
float = -1
|
number used to replace NAs. Defaults to |
-1.0
|
verbose
|
bool
|
whether the output should be verbose |
False
|
Source code in inference_model/preprocessing/preprocess_data.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
nan_with_unknown_imputer(df, columns, fill_token='unknown', verbose=False)
¶
Fills NAs with surrogate string, 'unknown' is default value used, it can be customized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
input dataframe |
required |
columns
|
List[str]
|
ist of columns that will be filled |
required |
fillna_token
|
str = "unknown"
|
string used to replace NAs |
required |
verbose
|
bool
|
whether the output should be verbose |
False
|
Source code in inference_model/preprocessing/preprocess_data.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
nuq_in_list_col(dfs)
¶
Returns pd.Series with the number of unique values in the lists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dfs
|
Series
|
input pandas series containing string list values |
required |
Source code in inference_model/preprocessing/preprocess_data.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
replace_rare_categories_with_str_other(df, categorical_cols, quantile=0.05, surrogate_value='other', verbose=False)
¶
Replaces rare category value with surrogate string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
input dataframe |
required |
categorical_cols
|
List[str]
|
list of columns in dataframe to process. |
required |
quantile
|
float = 0.05
|
determines what values are considered as rare |
0.05
|
surrogate_value
|
str = "other"
|
string used to replace rare values |
'other'
|
Returns:
Type | Description |
---|---|
Tuple[DataFrame, Dict]
|
Tuple[pd.DataFrame, Dict]: New dataframe and a dict. with mapping between orig. and surrogate values. |
Source code in inference_model/preprocessing/preprocess_data.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
PreprocessData
¶
Object to preprocess the dataset. Args: target_col (str): target column name id_cols (List[str]): id columns cat_cols (Optional[List[str]]): list of categorical column names cont_cols (Optional[List[str]]): list of continuous column names
Source code in inference_model/preprocessing/preprocess.py
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
fit(df)
¶
Just to keep familiar naming convention with sklearn.
Source code in inference_model/preprocessing/preprocess.py
134 135 136 |
|
fit_transform(df)
¶
Fit peprocessor and transform dataset in training step.
Source code in inference_model/preprocessing/preprocess.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
transform(df)
¶
Transform dataset in inference step.
Source code in inference_model/preprocessing/preprocess.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
scaler_mapper(cont_cols, cat_cols, id_cols, scaler_mapper_def=None)
¶
Function that maps scaler functions to appropriate columns.
By default does not assign any scaler to continuous, categorical or identifier columns. The scalers must be set in scaler_mapper_def. Use sklearn scalers. Only columns defined in mapper object will be present in the transformed dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cont_cols
|
list
|
list of continuous feature columns in the dataset |
required |
cat_cols
|
list
|
list of categorical feature columns in the dataset |
required |
id_cols
|
list
|
identifier columns |
required |
scaler_mapper_def
|
dict
|
optional dictionary that contains keys ['cont_cols', 'cat_cols', 'id_cols'] with their corresponding scalers (defined by names, not instantiated) from sklearn library |
None
|
Source code in inference_model/preprocessing/scaler.py
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 |
|