Skip to content

Commit

Permalink
Refactor GAMRegressor and GAMClassifier: improve code readability and…
Browse files Browse the repository at this point in the history
… remove main execution block
  • Loading branch information
nickcorona committed Dec 1, 2024
1 parent 0afb8f0 commit 7069f5a
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions pygam/sklearn_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from pygam.terms import te, TermList, Term # Import te for interactions
from pygam.terms import s, f, l, intercept # Import s, f, l for splines


def create_default_terms(X, categorical_features=None):
"""Generate default terms for each feature in X, handling categoricals."""
n_features = X.shape[1]
Expand Down Expand Up @@ -108,7 +109,10 @@ def fit(self, X, y):
self.terms_ = self.terms

if self.interactions is not None:
self.interactions_ = [te(*interaction) if isinstance(interaction, tuple) else interaction for interaction in self.interactions]
self.interactions_ = [
te(*interaction) if isinstance(interaction, tuple) else interaction
for interaction in self.interactions
]
else:
self.interactions_ = []

Expand Down Expand Up @@ -256,21 +260,3 @@ def predict_proba(self, X):

def score(self, X, y):
return accuracy_score(y, self.predict(X))


if __name__ == '__main__':

# Generate synthetic data
X, y = make_regression(n_samples=100, n_features=3, noise=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Initialize GAMRegressor with 'auto' terms
model = GAMRegressor(terms='auto', verbose=True)
model.fit(X_train, y_train)

# Inspect the generated terms
print(model.model_.terms)

# Predict and evaluate
y_pred = model.predict(X_test)
print(f"Test RMSE: {model.rmse(X_test, y_test):.4f}")

0 comments on commit 7069f5a

Please sign in to comment.