From 3c2668abd9ca81dcc013f9d5e9e5bb5390d34f91 Mon Sep 17 00:00:00 2001 From: Anders Aagaard Date: Thu, 27 Aug 2015 13:23:38 +0200 Subject: [PATCH] use get_feature_names from last step in pipeline if it is available --- sklearn/pipeline.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sklearn/pipeline.py b/sklearn/pipeline.py index 85fedd926e947..96ff12ed7addf 100644 --- a/sklearn/pipeline.py +++ b/sklearn/pipeline.py @@ -131,6 +131,22 @@ def named_steps(self): def _final_estimator(self): return self.steps[-1][1] + def get_feature_names(self): + """Get feature names from last step in pipeline + + Returns + ------- + feature_names : list of strings + Names of the features produced by last step in transform. + """ + last_step = self.steps[-1] + transformer = last_step[1] + name = last_step[0] + if not hasattr(transformer, 'get_feature_names'): + raise AttributeError("Transformer %s does not provide" + " get_feature_names." % str(name)) + return transformer.get_feature_names() + # Estimator interface def _pre_transform(self, X, y=None, **fit_params):