Skip to content

linearGAM with sklearn gridsearchCV #247

@hongkahjun

Description

@hongkahjun

Hi

I tried implementing LinearGAM with sklearn's GridsearchCV and got an error when gridsearchCV tried to clone the estimator. The code is below:

def gam(x, y):
    lams = np.random.rand(10, x.shape[1])
    lams = np.exp(lams)
    linear_gam = LinearGAM(n_splines=10, max_iter=1000)
    parameters = {
        'lam': [x for x in lams]
    }
    gam_cv = GridSearchCV(linear_gam, parameters, cv=5, iid=False, return_train_score=True, 
 refit=True, scoring='neg_mean_squared_error')
    gam_cv.fit(x, y)
    cv_results_df = pd.DataFrame(gam_cv.cv_results_).sort_values(by='mean_test_score', ascending=False)
    return gam_cv, cv_results_df

gam_rank, gam_cv_results = gam(x_all, y_all)

I get the error


RuntimeError Traceback (most recent call last)
in
----> 1 gam_rank, gam_cv_results = gam(x_all, y_all)

in gam(x, y)
7 }
8 gam_cv = GridSearchCV(linear_gam, parameters, cv=5, iid=False, return_train_score=True, >refit=True, scoring='neg_mean_squared_error')
----> 9 gam_cv.fit(x, y)
10 cv_results_df = pd.DataFrame(gam_cv.cv_results_).sort_values(by='mean_test_score', >ascending=False)
11 return gam_cv, cv_results_df

C:\Anaconda3\lib\site-packages\sklearn\model_selection_search.py in fit(self, X, y, groups, **fit_params)
630 n_splits = cv.get_n_splits(X, y, groups)
631
--> 632 base_estimator = clone(self.estimator)
633
634 parallel = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,

C:\Anaconda3\lib\site-packages\sklearn\base.py in clone(estimator, safe)
73 raise RuntimeError('Cannot clone object %s, as the constructor '
74 'either does not set or modifies parameter %s' %
---> 75 (estimator, name))
76 return new_object
77

RuntimeError: Cannot clone object LinearGAM(callbacks=['deviance', 'diffs'], fit_intercept=True,
max_iter=1000, n_splines=10, scale=None, terms='auto', tol=0.0001,
verbose=False), as the constructor either does not set or modifies parameter callbacks

The dataset I used was sklearn's california housing dataset.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions