<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 to predict nutrition score
Step1:  whoa! linear regression rmse on 10-fold cross validation is terrible!! let's try something else
Ridge, Lasso, Elastic-Net Regression
Step2:  The best regression method was Lasso with an alpha value of 0.23
<ASSISTANT_TASK:>
Python Code:
x = data.as_matrix()
y = target.as_matrix()
x = np.array([np.concatenate((v,[1])) for v in x]) #add column of ones to the end of the data set
print x
linreg = LinearRegression()
linreg.fit(x,y)
p = linreg.predict(x)
p
err = abs(p-y)
err
total_error = np.dot(err,err)
rmse_train = np.sqrt(total_error/len(p))
rmse_train
linreg.coef_ #Regression Coefficients
pl.plot(p, y,'ro')
pl.plot([-25,50],[-25,50], 'g-')
pl.xlabel('predicted')
pl.ylabel('real')
pl.show()
# RMSE with 10-Fold Cross Validation
kf = KFold(len(x), n_folds=10)
xval_err = 0
for train,test in kf:
    linreg.fit(x[train],y[train])
    p = linreg.predict(x[test])
    e = p-y[test]
    print e
    xval_err += np.dot(e,e)
    
rmse_10cv = np.sqrt(xval_err/len(x))
print('Method: Linear Regression')
print('RMSE on training: %.4f' %rmse_train)
print('RMSE on 10-fold CV: %.4f' %rmse_10cv)
Explanation: Linear regression to predict nutrition score
End of explanation
print('alpha\t\tridge\t\tlasso\t\telastic-net\n')
alpha = np.linspace(0.01,0.5,50)
for a in alpha:
    results = []
    for name,met in [
            #('linear regression', LinearRegression()),
            ('ridge', Ridge(fit_intercept=True, alpha=a)),
            ('lasso', Lasso(fit_intercept=True, alpha=a)),
            ('elastic-net', ElasticNet(fit_intercept=True, alpha=a))
            ]:
        #met.fit(x,y)
        #p = met.predict(x)
        #e = p-y
        #total_error = np.dot(e,e)
        #rmse_train = np.sqrt(total_error/len(p))
        kf = KFold(len(x), n_folds=10)
        err = 0
        for train,test in kf:
            met.fit(x[train],y[train])
            p = met.predict(x[test])
            e = p-y[test]
            err += np.dot(e,e)
        rmse_10cv = np.sqrt(err/len(x))
        
        results.append(rmse_10cv)
        
    print('{:.3f}\t\t{:.4f}\t\t{:.4f}\t\t{:.4f}\n'.format(a,results[0],results[1],results[2]))
Explanation: whoa! linear regression rmse on 10-fold cross validation is terrible!! let's try something else
Ridge, Lasso, Elastic-Net Regression
End of explanation
print('Lasso Regression w/ alpha=0.23')
ridge = Lasso(fit_intercept=True, alpha=0.23)
# computing RMSE using 10-fold cross validation
kf = KFold(len(x), n_folds=10)
xval_err = 0
for train, test in kf:
    ridge.fit(x[train], y[train])
    p = ridge.predict(x[test])
    err = p - y[test]
    xval_err += np.dot(err,err)
    
    pl.plot(p, y[test],'ro')
    pl.plot([-25,50],[-25,50], 'g-')
    pl.xlabel('predicted')
    pl.ylabel('real')
    pl.show()
    
rmse_10cv = np.sqrt(xval_err/len(x))
print('rsme with 10-fold cross validation = {:.4f}'.format(rmse_10cv))
Explanation: The best regression method was Lasso with an alpha value of 0.23
End of explanation
<END_TASK>