-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG+2] add get_feature_names to PolynomialFeatures #6372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
from ..utils.extmath import row_norms | ||
from ..utils.extmath import _incremental_mean_and_var | ||
from ..utils.fixes import combinations_with_replacement as combinations_w_r | ||
from ..utils.fixes import bincount | ||
from ..utils.sparsefuncs_fast import (inplace_csr_row_normalize_l1, | ||
inplace_csr_row_normalize_l2) | ||
from ..utils.sparsefuncs import (inplace_column_scale, | ||
|
@@ -1140,7 +1141,7 @@ class PolynomialFeatures(BaseEstimator, TransformerMixin): | |
|
||
Attributes | ||
---------- | ||
powers_ : array, shape (n_input_features, n_output_features) | ||
powers_ : array, shape (n_output_features, n_input_features) | ||
powers_[i, j] is the exponent of the jth input in the ith output. | ||
|
||
n_input_features_ : int | ||
|
@@ -1179,9 +1180,39 @@ def powers_(self): | |
combinations = self._combinations(self.n_input_features_, self.degree, | ||
self.interaction_only, | ||
self.include_bias) | ||
return np.vstack(np.bincount(c, minlength=self.n_input_features_) | ||
return np.vstack(bincount(c, minlength=self.n_input_features_) | ||
for c in combinations) | ||
|
||
def get_feature_names(self, input_features=None): | ||
""" | ||
Return feature names for output features | ||
|
||
Parameters | ||
---------- | ||
input_features : list of string, length n_features, optional | ||
String names for input features if available. By default, | ||
"x0", "x1", ... "xn_features" is used. | ||
|
||
Returns | ||
------- | ||
output_feature_names : list of string, length n_output_features | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a nitpick here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. conventionally not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is description, not type. |
||
|
||
""" | ||
powers = self.powers_ | ||
if input_features is None: | ||
input_features = ['x%d' % i for i in range(powers.shape[1])] | ||
feature_names = [] | ||
for row in powers: | ||
inds = np.where(row)[0] | ||
if len(inds): | ||
name = " ".join("%s^%d" % (input_features[ind], exp) | ||
if exp != 1 else input_features[ind] | ||
for ind, exp in zip(inds, row[inds])) | ||
else: | ||
name = "1" | ||
feature_names.append(name) | ||
return feature_names | ||
|
||
def fit(self, X, y=None): | ||
""" | ||
Compute number of output features. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been suggesting for a while that this is an appropriate way to deal with feature names in extractor/transformer pipelines. I'm happy to see this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. Very nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been wanting to do it but didn't have time. I'm just writing the book chapter about preprocessing and I'm embarrassed not having that feature ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, who knew writing books would be good for something?