<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Linear Regression with L2 regularization (Ridge Regression)
Step1:  We see that by numbers we have a good coefficient correlation between the true values and the predicted ones
Step2:  Kind of gaussian distribution of the error but the test dataset is rather small
Which coefficient is stronger ?
Step3:  The conclusion is that even if the cpu power was independently correlated with price the overall effect on price seem to be played by the manufacturer, and cool features, such as iris scanner, or tango sensor which allows augmented reality to work better
Good Deal or not
We are determining if the mobile phone is a good deal or not based on how much its real price deviates from the predicted one
<ASSISTANT_TASK:>
Python Code:
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 = getModel(alpha=best_alpha)
model.fit(XX, yy)
model.score(XX, yy)
model.score(XX_test, yy_test)
#r2_score(yy_test, model.predict(XX_test)) #exactly same as above
Explanation: Linear Regression with L2 regularization (Ridge Regression)
End of explanation
def fit_scatter(y_true, y_pred, x_label = 'Measured', y_label = 'Predicted'):
    assert y_true.shape == y_pred.shape
    fig, ax = plt.subplots()
    ax.scatter(y_true, y_pred)
    ax.plot([y_true.min(), y_true.max()], [y_true.min(), y_true.max()], 'k--', lw=4)
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    return fig, ax
preds_test = model.predict(XX_test)
fig = plt.figure()
#plt.figure(figsize=(17,7))
fig, ax = fit_scatter(y_true=yy_test, y_pred=preds_test,
            x_label='Measured Price', y_label='Predicted Price')
#ax.set_figsize((17,7))
fig.set_figheight(12)
fig.set_figwidth(12)
plt.show()
#fig, ax = plt.subplots(figsize=(12,6))
#ax.set_xlabel('price error')
plt.figure(figsize=(12,6))
sns.distplot( yy_test - preds_test, rug=True, axlabel='Price Error')
plt.show()
Explanation: We see that by numbers we have a good coefficient correlation between the true values and the predicted ones
End of explanation
coefdic = dict(zip(XX.columns, np.absolute(model.coef_) ))
from collections import OrderedDict
#weights_sorted = sorted(coefdic, key=coefdic.get)[::-1]
weights_sorted = OrderedDict(sorted(coefdic.items(), key=lambda x: x[1]))
weights_sorted.keys()[::-1]
plt.rc('ytick', labelsize=20)
def weight_plot(weights_dic, step=1):
    fig = plt.figure(figsize=(14, 17))
    xvalues = np.arange(0, len(weights_dic)) + 1
    #plt.bar(left = weights_dic.keys(), height=weights_dic.keys()) #, align='center')
    #plt.xticks(xvalues[::step], weights_dic.keys()[::step])
    sns.barplot(weights_dic.values(), weights_dic.keys())    
    plt.show()
weight_plot(weights_dic=weights_sorted, step=15)
strong_weights = OrderedDict(weights_sorted.items()[::-1][:40])
weight_plot(weights_dic=strong_weights)
Explanation: Kind of gaussian distribution of the error but the test dataset is rather small
Which coefficient is stronger ?
End of explanation
df.shape
df_all = pd.concat((df, df_test), axis=0)
XX_all = df_all.drop(labels=SkroutzMobile.PRICE_COLS, axis=1)
yy_all = df_all[SkroutzMobile.TARGET_COL]
XX_all.shape, yy_all.shape
preds_all = model.predict(XX_all)
len(preds_all)
deal = preds_all - yy_all
deal.sample(5, random_state=random_state)
sorted_deal = deal.sort_values(ascending=False)
sorted_deal[:5]
orig_df = pd.read_csv('../mobiles_02_no_outliers.csv', index_col=0, encoding='utf-8', quoting=csv.QUOTE_ALL)
deals_display = pd.concat( (sorted_deal, orig_df['display_name']), axis=1).sort_values(
    by='price_min', ascending=False)  #concat here works as expected
#rename column
deals_display = deals_display.rename_axis({"price_min": "deal_metric"}, axis="columns")
deals_display
deals_display_to_render = deals_display[:10].append(deals_display[-10:])
fig = plt.figure(figsize=(14, 17))
sns.barplot(deals_display_to_render['deal_metric'], deals_display_to_render['display_name'])
plt.show()
#https://awwapp.com/b/uh8hfmo8g/
#deals_display.to_csv('../mobiles_04_deals_display.csv', encoding='utf-8', quoting=csv.QUOTE_ALL)
Explanation: The conclusion is that even if the cpu power was independently correlated with price the overall effect on price seem to be played by the manufacturer, and cool features, such as iris scanner, or tango sensor which allows augmented reality to work better
Good Deal or not
We are determining if the mobile phone is a good deal or not based on how much its real price deviates from the predicted one
End of explanation
<END_TASK>