scikit-learn-contrib/sklearn-pandas

Preserving column names when transformer requires multiple columns as input

Aperta

#174 aperta il 2 ott 2018

 (4 commenti) (0 reazioni) (0 assegnatari)Python (420 fork)github user discovery
good first issue

Metriche repository

Star
 (2850 stelle)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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?

Guida contributor