Skip to content

Commit

Permalink
Fixes #36, documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Feb 25, 2019
1 parent cdcab21 commit 1ac8b0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions _unittests/ut_mlmodel/test_extended_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_polynomial_features_nobias(self):
self.assertEqual(P_test, P[:, 1:])
names = poly.get_feature_names()

ext = ExtendedFeatures(poly_degree=deg, include_bias=False)
ext = ExtendedFeatures(poly_degree=deg, poly_include_bias=False)
e_test = ext.fit_transform(X)
self.assertEqual(P_test, P[:, 1:])
e_names = ext.get_feature_names()
Expand Down Expand Up @@ -177,7 +177,8 @@ def test_polynomial_features_bigger_transpose_nobias(self):
X_sk = poly.fit_transform(X)
names_sk = poly.get_feature_names()

ext = ExtendedFeatures(poly_degree=deg, poly_transpose=True, include_bias=False)
ext = ExtendedFeatures(
poly_degree=deg, poly_transpose=True, poly_include_bias=False)
X_ext = ext.fit_transform(X)

inames = ["x%d" % i for i in range(0, X.shape[1])]
Expand Down
24 changes: 19 additions & 5 deletions src/mlinsights/mlmodel/extended_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class ExtendedFeatures(BaseEstimator, TransformerMixin):
poly_transpose: boolean
Transpose the matrix before doing the computation. Default is False.
poly_interaction_only: boolean
If true, only interaction features are produced: features that
are products of at most degree distinct input features
(so not ``x[1] ** 2, x[0] * x[2] ** 3``, etc.).
poly_include_bias: boolean
If True (default), then include a bias column, the feature in
which all polynomial powers are zero (i.e. a column of ones -
acts as an intercept term in a linear model).
Attributes
----------
poly_powers_ : array, shape (n_output_features, n_input_features)
Expand All @@ -38,13 +48,15 @@ class ExtendedFeatures(BaseEstimator, TransformerMixin):
of input features.
"""

def __init__(self, kind='poly', poly_degree=2, poly_transpose=False, include_bias=True):
def __init__(self, kind='poly', poly_degree=2, poly_transpose=False,
poly_interaction_only=False, poly_include_bias=True):
BaseEstimator.__init__(self)
TransformerMixin.__init__(self)
self.kind = kind
self.poly_degree = poly_degree
self.poly_transpose = poly_transpose
self.include_bias = include_bias
self.poly_include_bias = poly_include_bias
self.poly_interaction_only = poly_interaction_only

def get_feature_names(self, input_features=None):
"""
Expand All @@ -70,6 +82,8 @@ def _get_feature_names_poly(self, input_features=None):
Returns feature names for output features for
the polynomial features.
"""
if self.poly_interaction_only:
raise NotImplementedError()
check_is_fitted(self, ['n_input_features_'])
if input_features is None:
input_features = ["x%d" %
Expand All @@ -78,7 +92,7 @@ def _get_feature_names_poly(self, input_features=None):
raise ValueError("input_features should contain {} strings.".format(
self.n_input_features_))

names = ["1"] if self.include_bias else []
names = ["1"] if self.poly_include_bias else []
n = self.n_input_features_
for d in range(0, self.poly_degree):
if d == 0:
Expand Down Expand Up @@ -196,7 +210,7 @@ def final(X):
return X

if self.poly_transpose:
if self.include_bias:
if self.poly_include_bias:
XP[0, :] = 1
pos = 1
else:
Expand Down Expand Up @@ -226,7 +240,7 @@ def final(X):
XP = final(XP)
return XP.T
else:
if self.include_bias:
if self.poly_include_bias:
XP[:, 0] = 1
pos = 1
else:
Expand Down

0 comments on commit 1ac8b0c

Please sign in to comment.