<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Demo for prox_elasticnet package
Below we import prox_elasticnet along with some other useful packages.
Step1:  Diabetes dataset
Import the diabetes dataset which is included in sklearn.
It consists of 10 physiological variables (age, sex, weight, blood pressure) measured on 442 patients, and an indication of disease progression after one year.
Step2:  Our goal is to fit a linear model using Elastic Net regularisation, which predicts the disease progression for a given patient's physiological variables.
We separate the data into training and test sets (80% train/20% test)
Step3:  First we run the basic ElasticNet model with the default parameters.
Step4:  The model coefficients are accessed as follows
Step5:  The package also provides ElasticNetCV which chooses the regularisation parameters (alpha and l1_ratio) which yield the best mean-squared error.
Step6:  We can see that there been a significant increase in the coefficient of determination ($R^2$) on the test set when compared to the previous model (although it is still rather poor). The alpha and l1_ratio values that have been selected through cross-validation are accessed as follows
Step7:  The mean-squared error is in fact available for all combinations of alpha, l1_ratio and each fold of cross-validation. As an example, we plot the mean-squared error for the optimal l1_ratio = 0.8 as a function of alpha. We average over the three folds of cross validation.
<ASSISTANT_TASK:>
Python Code:
from prox_elasticnet import ElasticNet, ElasticNetCV
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(319159)
Explanation: Demo for prox_elasticnet package
Below we import prox_elasticnet along with some other useful packages.
End of explanation
from sklearn.datasets import load_diabetes
diabetes = load_diabetes()
X = diabetes.data
y = diabetes.target
Explanation: Diabetes dataset
Import the diabetes dataset which is included in sklearn.
It consists of 10 physiological variables (age, sex, weight, blood pressure) measured on 442 patients, and an indication of disease progression after one year.
End of explanation
prop_train = 0.8
n_pts = len(y)
n_train = np.floor(n_pts * prop_train).astype(int)
n_test = n_pts - n_train
ix = np.arange(n_pts)
np.random.shuffle(ix)
train_ix = ix[0:n_train]
test_ix = ix[n_train:n_pts]
X_train = X[train_ix,:]
y_train = y[train_ix]
X_test = X[test_ix,:]
y_test = y[test_ix]
Explanation: Our goal is to fit a linear model using Elastic Net regularisation, which predicts the disease progression for a given patient's physiological variables.
We separate the data into training and test sets (80% train/20% test)
End of explanation
model = ElasticNet().fit(X_train, y_train)
y_pred = model.predict(X_test)
print("The coefficient of determination for this model is: {}".format(model.score(X_test,y_test)))
Explanation: First we run the basic ElasticNet model with the default parameters.
End of explanation
model.coef_
Explanation: The model coefficients are accessed as follows:
End of explanation
model_cv = ElasticNetCV(l1_ratio = np.arange(0.1,0.9,step=0.1)).fit(X_train, y_train)
y_pred_cv = model_cv.predict(X_test)
print("The coefficient of determination for this model is: {}".format(model_cv.score(X_test,y_test)))
Explanation: The package also provides ElasticNetCV which chooses the regularisation parameters (alpha and l1_ratio) which yield the best mean-squared error.
End of explanation
model_cv.alpha_
model_cv.l1_ratio_
Explanation: We can see that there been a significant increase in the coefficient of determination ($R^2$) on the test set when compared to the previous model (although it is still rather poor). The alpha and l1_ratio values that have been selected through cross-validation are accessed as follows:
End of explanation
plt.title("Cross-validation for l1_ratio = 0.8")
plt.plot(model_cv.alphas_[7],model_cv.mse_path_.mean(axis=2)[7])
plt.xlabel("alpha")
plt.ylabel("MSE")
plt.show()
Explanation: The mean-squared error is in fact available for all combinations of alpha, l1_ratio and each fold of cross-validation. As an example, we plot the mean-squared error for the optimal l1_ratio = 0.8 as a function of alpha. We average over the three folds of cross validation.
End of explanation
<END_TASK>