Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
33 changes: 19 additions & 14 deletions pygam/pygam.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ def _linear_predictor(self, X=None, modelmat=None, b=None, term=-1):
contains the spline coefficients
if None, will use current model coefficients

term : 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 @@ -485,12 +485,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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK i like this.

the point is that we only check the features that we need

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, conveniently, check_X already allowed for that. Changing arguments was enough to fix the original issue.

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 @@ -1432,6 +1442,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 @@ -1602,14 +1615,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 @@ -938,7 +938,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