scikit-learn-contrib/sklearn-pandas

Preserving column names when transformer requires multiple columns as input

オープン

#174 opened on 2018/10/02

 (4 件のコメント) (0 件のリアクション) (0 人の担当者)Python (420 件のフォーク)github user discovery
good first issue

Repository metrics

Stars
 (2,850 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

I was wondering whether it is possible to preserve column names when using a transformer that requires multiple columns of the dataframe. I'll try to illustrate what I mean with an example.

from sklearn.feature_selection import SelectKBest, chi2
​
data = pd.DataFrame({
    'pet':      ['cat', 'dog', 'dog', 'fish', 'cat', 'dog', 'cat', 'fish'],
    'children': [4., 6, 3, 3, 2, 3, 5, 4],
    'salary':   [90., 24, 44, 27, 32, 59, 36, 27]})
​
mapper_fs = DataFrameMapper([(['children','salary'], SelectKBest(chi2, k=2))])
mapper_fs.fit_transform(data[['children','salary']], data['pet'])
print(mapper_fs.transformed_names_)

Which outputs ['children_salary'], whereas I would expect just ['salary']. This makes it impossible to keep track of which columns were dropped by the SelectKBest transformer. Is there currently a way to solve this problem?

コントリビューターガイド