Я хочу отслеживать индексы категориальных объектов в конвейере sklearn, чтобы передать их в CatBoostClassifier.
Я начинаю с набора категориальных функций перед соответствием () конвейера.Сам конвейер изменяет структуру данных и удаляет объекты на шаге выбора.
Как я могу заранее знать, какие категориальные объекты будут удалены или добавлены в конвейер?Мне нужно знать обновленные индексы списка, когда я вызываю метод fit ().Проблема в том, что мой набор данных может измениться после преобразований.
Вот пример моего фрейма данных:
data = pd.DataFrame({'pet': ['cat', 'dog', 'dog', 'fish', np.nan, 'dog', 'cat', 'fish'],
'children': [4., 6, 3, np.nan, 2, 3, 5, 4],
'salary': [90., 24, np.nan, 27, 32, 59, 36, 27],
'gender': ['male', 'male', 'male', 'male', 'male', 'male', 'male', 'male'],
'happy': [0, 1, 1, 0, 1, 1, 0, 0]})
categorical_features = ['pet', 'gender']
numerical_features = ['children', 'salary']
target = 'happy'
print(data)
pet children salary gender happy
0 cat 4.0 90.0 male 0
1 dog 6.0 24.0 male 1
2 dog 3.0 NaN male 1
3 fish NaN 27.0 male 0
4 NaN 2.0 32.0 male 1
5 dog 3.0 59.0 male 1
6 cat 5.0 36.0 male 0
7 fish 4.0 27.0 male 0
Теперь я хочу запустить конвейер с несколькими шагами.Одним из этих шагов является VarianceThreshold (), который в моем случае приведет к удалению «пола» из фрейма данных.
X, y = data.drop(columns=[target]), data[target]
pipeline = Pipeline(steps=[
(
'preprocessing',
ColumnTransformer(transformers=[
(
'categoricals',
Pipeline(steps=[
('fillna_with_frequent', SimpleImputer(strategy='most_frequent')),
('ordinal_encoder', OrdinalEncoder())
]),
categorical_features
),
(
'numericals',
Pipeline(steps=[
('fillna_with_mean', SimpleImputer(strategy='mean'))
]),
numerical_features
)
])
),
(
'feature_selection',
VarianceThreshold()
),
(
'estimator',
CatBoostClassifier()
)
])
Теперь, когда я пытаюсь получить список индексов категориальных функций для CatBoostЯ не могу сказать, что «пол» больше не является частью моего фрейма данных.
cat_features = [data.columns.get_loc(col) for col in categorical_features]
print(cat_features)
[0, 3]
Индексы 0, 3 неверны, потому что после VarianceThreshold функция 3 (пол) будет удалена.
pipeline.fit(X, y, estimator__cat_features=cat_features)
---------------------------------------------------------------------------
CatBoostError Traceback (most recent call last)
<ipython-input-230-527766a70b4d> in <module>
----> 1 pipeline.fit(X, y, estimator__cat_features=cat_features)
~/anaconda3/lib/python3.7/site-packages/sklearn/pipeline.py in fit(self, X, y, **fit_params)
265 Xt, fit_params = self._fit(X, y, **fit_params)
266 if self._final_estimator is not None:
--> 267 self._final_estimator.fit(Xt, y, **fit_params)
268 return self
269
~/anaconda3/lib/python3.7/site-packages/catboost/core.py in fit(self, X, y, cat_features, sample_weight, baseline, use_best_model, eval_set, verbose, logging_level, plot, column_description, verbose_eval, metric_period, silent, early_stopping_rounds, save_snapshot, snapshot_file, snapshot_interval, init_model)
2801 self._fit(X, y, cat_features, None, sample_weight, None, None, None, None, baseline, use_best_model,
2802 eval_set, verbose, logging_level, plot, column_description, verbose_eval, metric_period,
-> 2803 silent, early_stopping_rounds, save_snapshot, snapshot_file, snapshot_interval, init_model)
2804 return self
2805
~/anaconda3/lib/python3.7/site-packages/catboost/core.py in _fit(self, X, y, cat_features, pairs, sample_weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, use_best_model, eval_set, verbose, logging_level, plot, column_description, verbose_eval, metric_period, silent, early_stopping_rounds, save_snapshot, snapshot_file, snapshot_interval, init_model)
1231 _check_train_params(params)
1232
-> 1233 train_pool = _build_train_pool(X, y, cat_features, pairs, sample_weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, column_description)
1234 if train_pool.is_empty_:
1235 raise CatBoostError("X is empty.")
~/anaconda3/lib/python3.7/site-packages/catboost/core.py in _build_train_pool(X, y, cat_features, pairs, sample_weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, column_description)
689 raise CatBoostError("y has not initialized in fit(): X is not catboost.Pool object, y must be not None in fit().")
690 train_pool = Pool(X, y, cat_features=cat_features, pairs=pairs, weight=sample_weight, group_id=group_id,
--> 691 group_weight=group_weight, subgroup_id=subgroup_id, pairs_weight=pairs_weight, baseline=baseline)
692 return train_pool
693
~/anaconda3/lib/python3.7/site-packages/catboost/core.py in __init__(self, data, label, cat_features, column_description, pairs, delimiter, has_header, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names, thread_count)
318 )
319
--> 320 self._init(data, label, cat_features, pairs, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names)
321 super(Pool, self).__init__()
322
~/anaconda3/lib/python3.7/site-packages/catboost/core.py in _init(self, data, label, cat_features, pairs, weight, group_id, group_weight, subgroup_id, pairs_weight, baseline, feature_names)
638 cat_features = _get_cat_features_indices(cat_features, feature_names)
639 self._check_cf_type(cat_features)
--> 640 self._check_cf_value(cat_features, features_count)
641 if pairs is not None:
642 self._check_pairs_type(pairs)
~/anaconda3/lib/python3.7/site-packages/catboost/core.py in _check_cf_value(self, cat_features, features_count)
360 raise CatBoostError("Invalid cat_features[{}] = {} value type={}: must be int().".format(indx, feature, type(feature)))
361 if feature >= features_count:
--> 362 raise CatBoostError("Invalid cat_features[{}] = {} value: must be < {}.".format(indx, feature, features_count))
363
364 def _check_pairs_type(self, pairs):
CatBoostError: Invalid cat_features[1] = 3 value: must be < 3.
Я ожидаю, что cat_features будет [0], но фактический результат равен [0, 3].