python - 无法使用category_encoders的fit_transform对我的数据进行序数编码

标签 python pandas machine-learning categorical-data ordinal

我正在尝试将 category_encodersOrdinalEncoder fit_transform 到我的 data 的列之一.
我尝试看到的documentation是:

    import category_encoders as ce

    # here i'm defining mapping for OrdinalEncoder
    property_ordinal_mapping_1 = [{"col":"property_type", "mapping": [('Apartment', 1),('House', 2),('Condominium', 3),
                                                                      ('Townhouse', 4),('Loft', 5),('Other', 6),
                                                                      ('Guesthouse', 7),('Bed & Breakfast', 8),
                                                                      ('Bungalow', 9),('Villa', 10),('Dorm', 11),
                                                                      ('Guest suite', 12),('Camper/RV', 13),
                                                                      ('Timeshare', 14),('Cabin', 15),('In-law', 16),
                                                                      ('Hostel', 17),('Boutique hotel', 18),('Boat', 19),
                                                                      ('Serviced apartment', 20),('Tent', 21),('Castle', 22),
                                                                      ('Vacation home', 23),('Yurt', 24),('Hut', 25),
                                                                      ('Treehouse', 26),('Chalet', 27),('Earth House', 28),
                                                                      ('Tipi', 29),('Train', 30),('Cave', 31),
                                                                      ('Casa particular', 32),('Parking Space', 33),
                                                                      ('Lighthouse', 34),('Island', 35)
                                                                     ]
                                },
                               ]
    # preparing the OrdinalEncoder for fitting and transforming
    property_encoder_1 = ce.OrdinalEncoder(mapping = property_ordinal_mapping_1, return_df = True, cols=["property_type"])

当我尝试fit_transfom时出现的问题是:
df_train = property_encoder_1.fit_transform(air_cat_2)
错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-96-9cea1713182c> in <module>()
----> 1 df_train = property_encoder_1.fit_transform(air_cat_2)
/usr/local/lib/python3.6/dist-packages/sklearn/base.py in fit_transform(self, X, y, **fit_params)
    551         if y is None:
    552             # fit method of arity 1 (unsupervised transformation)
--> 553             return self.fit(X, **fit_params).transform(X)
    554         else:
    555             # fit method of arity 2 (supervised transformation)

/usr/local/lib/python3.6/dist-packages/category_encoders/ordinal.py in fit(self, X, y, **kwargs)
    139             cols=self.cols,
    140             handle_unknown=self.handle_unknown,
--> 141             handle_missing=self.handle_missing
    142         )
    143         self.mapping = categories

/usr/local/lib/python3.6/dist-packages/category_encoders/ordinal.py in ordinal_encoding(X_in, mapping, cols, handle_unknown, handle_missing)
    288             for switch in mapping:
    289                 column = switch.get('col')
--> 290                 X[column] = X[column].map(switch['mapping'])
    291 
    292                 try:

/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in map(self, arg, na_action)
   3380         """
   3381         new_values = super(Series, self)._map_values(
-> 3382             arg, na_action=na_action)
   3383         return self._constructor(new_values,
   3384                                  index=self.index).__finalize__(self)

/usr/local/lib/python3.6/dist-packages/pandas/core/base.py in _map_values(self, mapper, na_action)
   1216 
   1217         # mapper is a function
-> 1218         new_values = map_f(values, mapper)
   1219 
   1220         return new_values

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

TypeError: 'list' object is not callable

sklearn.preprocessing.OrdinalEncoder 也有类似的错误。
我做错了什么以及如何解决这个问题。我已经仔细检查了我的专栏的类名并重写了整个代码,但似乎没有任何帮助;或者有什么替代方法可以做到这一点。
请不要将我的问题标记为重复。

最佳答案

文档不正确。 映射 的语法现在有所不同。参见这里:https://github.com/scikit-learn-contrib/categorical-encoding/issues/193

关于python - 无法使用category_encoders的fit_transform对我的数据进行序数编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56655197/

相关文章:

python - 避免在 for 循环中更新嵌套列表

python - 乘法递归的时间复杂度

python - 如何将第一列中的内容转移到 Pandas 中的列名称中

python - 如何使用 Groupby 函数查找数据框中的最高值

R - 混淆矩阵中缺失因子的条件替换

python - 如何使用 Websocket 向服务器发送消息

java - 整数的罗马表示

python - 用于分隔列的字典的 pandas 列表

c# - Accord.NET:如何训练 Boost 分类器

Tensorflow:批量大小 > 1 时无法过度拟合训练数据