-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path.Rhistory
32 lines (32 loc) · 1.21 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
reticulate::repl_python()
quit
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
reticulate::repl_python()
# Import required packages
import numpy as np
import asgl
from sklearn.datasets import make_regression
x, y = make_regression(n_samples=100, n_features=30, random_state=42)
group_index = np.random.randint(1, 6, 30)
# Define parameters grid
lambda1 np.array([1e-2, 1])
alpha = np.array([0, 0.5, 1])
# Define a cross validation object
cv_class = asgl.CV(model='lm', penalization='sgl', lambda1=lambda1, alpha=alpha,
nfolds=5, error_type='MSE', parallel=True, random_state=99)
# Compute error using k-fold cross validation
error = cv_class.cross_validation(x=x, y=y, group_index=group_index)
num_models, k_folds = error.shape
# error is a matrix of shape (number_of_models, k_folds)
print(f'We are considering a grid of {num_models} models, optimized based on {k_folds}-folds cross validation')
# Obtain the mean error across different folds
error = np.mean(error, axis=1)
# Select the minimum error
minimum_error_idx = np.argmin(error)
# Select the parameters associated to mininum error values
optimal_parameters = cv_class.retrieve_parameters_value(minimum_error_idx)