Skip to content

Commit

Permalink
Merge branch 'master' into periodic
Browse files Browse the repository at this point in the history
  • Loading branch information
dswah authored Oct 31, 2018
2 parents 015eb6f + e6dc537 commit 95e6bc6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
28 changes: 27 additions & 1 deletion pygam/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ def build_columns(self, X, verbose=False):


class FactorTerm(SplineTerm):
_encodings = ['one-hot']
_encodings = ['one-hot', 'dummy']
def __init__(self, feature, lam=0.6, penalties='auto', coding='one-hot', verbose=False):
"""creates an instance of a FactorTerm
Expand Down Expand Up @@ -900,6 +900,32 @@ def compile(self, X, verbose=False):
verbose=verbose)
return self

def build_columns(self, X, verbose=False):
"""construct the model matrix columns for the term
Parameters
----------
X : array-like
Input dataset with n rows
verbose : bool
whether to show warnings
Returns
-------
scipy sparse array with n rows
"""
columns = super(FactorTerm, self).build_columns(X, verbose=verbose)
if self.coding == 'dummy':
columns = columns[:, 1:]

return columns

@property
def n_coefs(self):
"""Number of coefficients contributed by the term to the model
"""
return self.n_splines - 1 * (self.coding in ['dummy'])

class MetaTermMixin(object):
_plural = [
Expand Down
13 changes: 12 additions & 1 deletion pygam/tests/test_terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ def test_correct_smoothing_in_tensors(toy_interaction_X_y):
gam = LinearGAM(te(0, 1, lam=[10000, 0.6])).fit(X, y)
assert gam.statistics_['pseudo_r2']['explained_deviance'] < 0.1

def test_dummy_encoding(wage_X_y, wage_gam):
"""check that dummy encoding produces fewer coefficients than one-hot"""
X, y = wage_X_y

gam = LinearGAM(s(0) + s(1) + f(2, coding='dummy')).fit(X, y)

assert gam._modelmat(X=X, term=2).shape[1] == 4
assert gam.terms[2].n_coefs == 4

assert wage_gam._modelmat(X=X, term=2).shape[1] == 5
assert wage_gam.terms[2].n_coefs == 5

def test_build_cyclic_p_spline(hepatitis_X_y):
"""check the cyclic p spline builds
Expand Down Expand Up @@ -247,7 +259,6 @@ def test_cyclic_p_spline_custom_period():
assert np.allclose(gam.predict(X), 0.5)
assert np.allclose(gam.edge_knots_[0], [0, 0.5])


def test_tensor_terms_have_constraints(toy_interaction_X_y):
"""test that we can fit a gam with constrained tensor terms,
even if those constraints are 'none'
Expand Down

0 comments on commit 95e6bc6

Please sign in to comment.