Beispiel #1
0
class LassoLarsCVImpl():

    def __init__(self, fit_intercept=True, verbose=False, max_iter=500, normalize=True, precompute='auto', cv=3, max_n_alphas=1000, n_jobs=None, eps=2.220446049250313e-16, copy_X=True, positive=False):
        self._hyperparams = {
            'fit_intercept': fit_intercept,
            'verbose': verbose,
            'max_iter': max_iter,
            'normalize': normalize,
            'precompute': precompute,
            'cv': cv,
            'max_n_alphas': max_n_alphas,
            'n_jobs': n_jobs,
            'eps': eps,
            'copy_X': copy_X,
            'positive': positive}
        self._wrapped_model = SKLModel(**self._hyperparams)

    def fit(self, X, y=None):
        if (y is not None):
            self._wrapped_model.fit(X, y)
        else:
            self._wrapped_model.fit(X)
        return self

    def predict(self, X):
        return self._wrapped_model.predict(X)
# Transform into numpy object
x_train = poly_reg.fit_transform(X_train)
X_test = poly_reg.fit_transform(X_test)
y_test = np.array(y_test.ix[:, 0])
y_train = np.array(y_train.ix[:, 0])

# Build model with good params
model = LassoLarsCV(copy_X=True,
                    cv=None,
                    eps=2.2204460492503131e-16,
                    fit_intercept=True,
                    max_iter=500,
                    max_n_alphas=1000,
                    n_jobs=1,
                    normalize=True,
                    positive=False,
                    precompute='auto',
                    verbose=False)

# Fit the model
model.fit(x_train, y_train)

# Predict
y_pred = model.predict(X_test)

# Scoring
if regression:
    print('Score on test set:', mean_absolute_error(y_test, y_pred))
else:
    print('Score on test set:', accuracy_score(y_test, y_pred))