human
I am working on the file "02_preprocessing/exploration01-linear_regression.ipynb".
The first task is:
Linear Regression with L2 regularization (Ridge Regression)
Can you write Python program for it?
assistant
#Here is the Python program you requested.

def getModel(alpha):
    return Ridge(alpha=alpha, fit_intercept=True, normalize=False, copy_X=True, random_state=random_state)

model = getModel(alpha=0.01)

cvs = cross_val_score(estimator=model, X=XX, y=yy, cv=10)
cvs

cv_score = np.mean(cvs)
cv_score

def gpOptimization(n_jobs=n_jobs, cv=10, verbose=True):
    
    def objective(params): # Here we define the metric we want to minimise
        (alpha) = params
        if verbose:
            print "current L2: {}".format(alpha)
        
        start = time()
        
        model = getModel(alpha=alpha)

        cv_results = cross_val_score(estimator=model, X=XX, y=yy, cv=cv)

        cv_score = np.mean(cv_results)
        
        if verbose:
            print "current CV score: {}".format(cv_score)
            print "time elapsed: {}".format(time() - start)
            print

        # We want to maximise accuracy score, i.e. minimise minus average accuracy
        return -cv_score
    
    alpha_space = Real(1e-4, 1e1, prior='log-uniform')  #uniform or log-uniform
    
    return gp_minimize(
        func=objective, # function that we wish to minimise
        dimensions=[alpha_space], #the search space for the hyper-parameters
        #x0=x0, #inital values for the hyper-parameters
        n_calls=30, #number of times the function will be evaluated
        random_state=random_state, #random seed
        n_random_starts=5,  #before we start modelling the optimised function with a GP Regression
                            #model, we want to try a few random choices for the hyper-parameters.
        kappa=1.9, #trade-off between exploration vs. exploitation.
        n_jobs=n_jobs
    )

res_gp = gpOptimization()

fig = plt.figure(figsize=(12,6))
plot_convergence(res_gp)
plt.grid()
plt.show()

plt.figure(figsize=(12,6))
plt.plot(res_gp.func_vals)
plt.scatter(range(len(res_gp.func_vals)), res_gp.func_vals)
plt.ylabel(r'$f(x)$')
plt.xlabel('Number of calls $n$')
plt.xlim([0, len(res_gp.func_vals)])
plt.show()

best_alpha = res_gp.x[0]
best_alpha

model =...