Skip to content

Instantly share code, notes, and snippets.

@rrobby86
Created April 22, 2020 15:33
Show Gist options
  • Select an option

  • Save rrobby86/f2943b8a824250fda3c8f9f259b4565f to your computer and use it in GitHub Desktop.

Select an option

Save rrobby86/f2943b8a824250fda3c8f9f259b4565f to your computer and use it in GitHub Desktop.
Regressione non lineare (parte 2): soluzioni esercizi
# ESERCIZIO 1
# 1a
model_a = LinearRegression()
model_a.fit(X_train, y_train)
print_eval(X_val, y_val, model_a)
pd.Series(model_a.coef_, index=X.columns)
# 1b
model_b = Ridge(alpha=10)
model_b.fit(X_train, y_train)
print_eval(X_val, y_val, model_b)
pd.Series(model_b.coef_, index=X.columns)
# 1c
model_c = Pipeline([
("scale", StandardScaler()),
("lr", LinearRegression())
])
model_c.fit(X_train, y_train)
print_eval(X_val, y_val, model_c)
pd.Series(model_c.named_steps["lr"].coef_, index=X.columns)
# 1d
pd.DataFrame({
"linear": model_a.coef_,
"ridge": model_b.coef_,
"scaled": model_c.named_steps["lr"].coef_
}, index=X.columns)
# ESERCIZIO 2
# 2a
def elastic_net_with_alphas(alpha_l2, alpha_l1):
alpha = alpha_l1 + alpha_l2
l1_ratio = alpha_l1 / alpha
return ElasticNet(alpha=alpha, l1_ratio=l1_ratio)
# 2b
model = Pipeline([
("scale", StandardScaler()),
("regr", elastic_net_with_alphas(1, 0.1))
])
model.fit(X_train, y_train)
print_eval(X_val, y_val, model)
# ESERCIZIO 3
# 3a
model = Pipeline([
("scale", StandardScaler()),
("poly", PolynomialFeatures(degree=3, include_bias=False)),
("regr", ElasticNet(alpha=0.5, l1_ratio=0.2))
])
model.fit(X_train, y_train)
print_eval(X_val, y_val, model)
# 3b
model = Pipeline([
("scale", StandardScaler()),
("poly", PolynomialFeatures(degree=3, include_bias=False)),
("scale2", StandardScaler()),
("regr", ElasticNet(alpha=0.5, l1_ratio=0.2))
])
model.fit(X_train, y_train)
print_eval(X_val, y_val, model)
# ESERCIZIO 4
# 4a
model = Pipeline([
("scale", StandardScaler()),
("regr", KernelRidge(alpha=10, kernel="poly", degree=3))
])
# 4b
cv_results = cross_validate(model, X, y, cv=kf)
# 4c
cv_scores = cv_results["test_score"]
cv_scores.mean(), cv_scores.std()
# ESERCIZIO 5
# 5a
def grid_test(model, grid):
gs = GridSearchCV(model, grid, cv=kf)
gs.fit(X_train, y_train)
print(gs.best_params_)
print_eval(X_val, y_val, gs)
# 5b
model = Pipeline([
("poly", PolynomialFeatures(include_bias=False)),
("scale", StandardScaler()),
("regr", ElasticNet())
])
grid = {
"poly__degree": [2, 3],
"regr__alpha": [0.1, 1, 10],
"regr__l1_ratio": [0.1, 0.25, 0.5]
}
grid_test(model, grid)
# 5c
model = Pipeline([
("scale", StandardScaler()),
("regr", KernelRidge(kernel="poly"))
])
grid = {
"regr__degree": range(2, 11),
"regr__alpha": [0.01, 0.1, 1, 10],
}
grid_test(model, grid)
# ESERCIZIO 6
def nested_cv(model, grid):
results = []
for train_indices, val_indices in outer_cv.split(X, y):
gs = GridSearchCV(model, grid, cv=inner_cv)
gs.fit(X.iloc[train_indices], y.iloc[train_indices])
score = gs.score(X.iloc[val_indices], y.iloc[val_indices])
results.append(score)
return results
model = Pipeline([
("scale", StandardScaler()),
("regr", KernelRidge(kernel="poly"))
])
grid = {
"regr__degree": range(2, 11),
"regr__alpha": [0.01, 0.1, 1, 10],
}
nested_cv(model, grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment