Skip to content
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

Fixes #301 and some other issues #354

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions pygam/pygam.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def _linear_predictor(self, X=None, modelmat=None, b=None, term=-1):
---------
at least 1 of (X, modelmat)
and
at least 1 of (b, feature)
at least 1 of (b, term)

X : array-like of shape (n_samples, m_features) or None, optional
containing the input dataset
Expand All @@ -401,9 +401,9 @@ def _linear_predictor(self, X=None, modelmat=None, b=None, term=-1):
contains the spline coefficients
if None, will use current model coefficients

feature : int, optional
feature for which to compute the linear prediction
if -1, will compute for all features
term : int or list of int, default: -1
term(s) to use in calculation of linear predictor
if -1, all terms are used

Returns
-------
Expand Down Expand Up @@ -480,12 +480,22 @@ def _modelmat(self, X, term=-1):
modelmat : sparse matrix of len n_samples
containing model matrix of the spline basis for selected features
"""
# take features, dtypes and edge_knots based on supplied term values
if term != -1:
terms = list(np.atleast_1d(term))
else:
terms = range(len(self.feature))

features = [self.feature[i] for i in terms]
edge_knots = [self.edge_knots_[i] for i in terms]
dtypes = [self.dtype[i] for i in terms]

X = check_X(
X,
n_feats=self.statistics_['m_features'],
edge_knots=self.edge_knots_,
dtypes=self.dtype,
features=self.feature,
edge_knots=edge_knots,
dtypes=dtypes,
features=features,
verbose=self.verbose,
)

Expand Down Expand Up @@ -1434,6 +1444,9 @@ def _flatten_mesh(self, Xs, term):
X = np.zeros((n, self.statistics_['m_features']))
for term_, x in zip(terms, Xs):
X[:, term_.feature] = x.ravel()

if getattr(self.terms[term], 'by', None) is not None:
X[:, self.terms[term].by] = 1.0
return X

def generate_X_grid(self, term, n=100, meshgrid=False):
Expand Down Expand Up @@ -1607,14 +1620,6 @@ def partial_dependence(
shape = X[0].shape

X = self._flatten_mesh(X, term=term)
X = check_X(
X,
n_feats=self.statistics_['m_features'],
edge_knots=self.edge_knots_,
dtypes=self.dtype,
features=self.feature,
verbose=self.verbose,
)

modelmat = self._modelmat(X, term=term)
pdep = self._linear_predictor(modelmat=modelmat, term=term)
Expand Down
1 change: 0 additions & 1 deletion pygam/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,6 @@ def build_columns(self, X, verbose=False):
-------
scipy sparse array with n rows
"""
X[:, self.feature][:, np.newaxis]

splines = b_spline_basis(
X[:, self.feature],
Expand Down
Loading