Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map categorical data to category_encoders.OrdinalEncoder in python pandas dataframe

I'm trying to use category_encoders.OrdinalEncoder to map categories to integers in a pandas dataframe. But I'm getting the following error without any other helpful hints.

TypeError: 'NoneType' object is not iterable

Code runs fine without the attempted mapping, but I'd like the mapping.

Code:

import category_encoders as ce

ordinal_cols = [
    "ExterQual",
]

ordinal_cols_mapping = [{
    "ExterQual": {
        'Ex': 5,
        'Gd': 4,
        'TA': 3, 
        'Fa': 2,
        'Po': 1,
        'NA': NaN
    }},
]

encoder = ce.OrdinalEncoder(  mapping = ordinal_cols_mapping, return_df = True, cols = ordinal_cols,)  

df_train = encoder.fit_transform(train_data)
print(df_train)

What am I dong wrong?

mapping: list of dict a mapping of class to label to use for the encoding, optional.

http://contrib.scikit-learn.org/categorical-encoding/ordinal.html

Full Stack Trace:

---------------------------------------------------------------------------
TypeError                                 
Traceback (most recent call last)
<ipython-input-56-4944c8d41d07> in <module>()
    150 # use the Ordinal Encoder to map the ordinal data to interval and then fit transform
    151 encoder = ce.OrdinalEncoder( return_df = True, cols = ordinal_cols, mapping = ordinal_cols_mapping)  #NaNs get -1, mapping = ordinal_cols_mapping removed due to error
--> 152 X = encoder.fit_transform(X)

/opt/conda/lib/python3.6/site-packages/sklearn/base.py in fit_transform(self, X, y, **fit_params)
    515         if y is None:
    516             # fit method of arity 1 (unsupervised transformation)
--> 517             return self.fit(X, **fit_params).transform(X)
    518         else:
    519             # fit method of arity 2 (supervised transformation)

/opt/conda/lib/python3.6/site-packages/category_encoders/ordinal.py in fit(self, X, y, **kwargs)
    130             cols=self.cols,
    131             impute_missing=self.impute_missing,
--> 132             handle_unknown=self.handle_unknown
    133         )
    134         self.mapping = categories

/opt/conda/lib/python3.6/site-packages/category_encoders/ordinal.py in ordinal_encoding(X_in, mapping, cols, impute_missing, handle_unknown)
    249             for switch in mapping:
    250                 X[str(switch.get('col')) + '_tmp'] = np.nan
--> 251                 for category in switch.get('mapping'):
    252                     X.loc[X[switch.get('col')] == category[0], str(switch.get('col')) + '_tmp'] = str(category[1])
    253                 del X[switch.get('col')]

TypeError: 'NoneType' object is not iterable

Example data:

0    0
1    1
2    0
3    1
4    0
Name: ExterQual, dtype: int64
like image 255
jeffhale Avatar asked Apr 30 '18 01:04

jeffhale


1 Answers

You are using the 'mapping' param wrong.

The format should be:

'mapping' param should be a list of dicts where internal dicts should contain the keys 'col' and 'mapping' and in that the 'mapping' key should have a list of tuples of format (original_label, encoded_label) as value.

Something like this:

ordinal_cols_mapping = [{
    "col":"ExterQual",    
    "mapping": [
        ('Ex',5), 
        ('Gd',4), 
        ('TA',3), 
        ('Fa',2), 
        ('Po',1), 
        ('NA',np.nan)
    ]},
]

Then no need to set the 'cols' param separately. Column names will be used from the 'mapping' param.

Just do this:

encoder = OrdinalEncoder(mapping = ordinal_cols_mapping, 
                         return_df = True)  
df_train = encoder.fit_transform(train_data)

Hope that this makes it clear.

like image 80
Vivek Kumar Avatar answered Sep 28 '22 07:09

Vivek Kumar