我想跟踪 sklearn 管道中的分类特征索引,以便将它们提供给 CatBoostClassifier。
我在管道的 fit() 之前从一组分类特征开始。管道本身会改变数据的结构并在特征选择步骤中删除特征。
我如何预先知道哪些分类特征将被删除或添加到管道中?当我调用 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 …Run Code Online (Sandbox Code Playgroud)