def evaluate(X_train, X_test, y_train, y_test, alpha): # Implement your method here model = Lasso(alpha=alpha) model.fit(X_train,y_train) MSEsum = 0 y_pred = model.predict(X_test) for i in range(len(y_test)): MSEsum+=(y_pred[i]-y_test[i])**2 return MSEsum/len(y_test)
def deploy_lasso(sample_id): sample_data = sample_data_list[sample_id] sample_x, sample_y = sample_data.x, sample_data.y sample_phi = generate_polynomial_features(sample_x) title = 'LASSO' log = open(result_sub_path + title + '_' + str(sample_id) + '.txt', 'w') img_path = result_sub_path + title + '_' + str(sample_id) + '.png' candidate_pred_y, candidate_error, candidate_sample_error, candidate_lambda = [], [], [], [] _lambda = 0.000001 _lambda_step = 1.05 log.write("range of lambda: " + str(_lambda) + ' : ') for candidate_id in range(1000): model = Lasso() model.fit(sample_phi, sample_y, _lambda) pred_sample_y = model.predict(sample_phi) pred_y = model.predict(gt_phi) sample_error = compute_mean_squared_error(pred_sample_y, sample_y) error = compute_mean_squared_error(pred_y, gt_y) candidate_sample_error.append((sample_error, candidate_id)) candidate_error.append(error) candidate_lambda.append(_lambda) candidate_pred_y.append(pred_y) _lambda *= _lambda_step log.write(str(_lambda) + '\n') candidate_error.sort() (sample_error, best_id) = candidate_sample_error[0] error = candidate_error[best_id] pred_y = candidate_pred_y[best_id] _lambda = candidate_lambda[best_id] log.write("lambda: " + str(_lambda) + '\n') log.write("Training MSE: " + str(sample_error) + '\n') log.write("Testing MSE: " + str(error) + '\n') visualize(sample_x, sample_y, gt_x, gt_y, pred_y, title, img_path) return sample_error, error
def deploy_lasso(): title = 'LASSO' log = open(result_sub_path + title + '_2.txt', 'w') img_path = result_sub_path + title + '_2.png' candidate_pred_y, candidate_error, candidate_sample_error, candidate_lambda = [], [], [], [] _lambda = 1.0 _lambda_step = 1.05 log.write("range of lambda: " + str(_lambda) + ' : ') for candidate_id in range(10): model = Lasso() model.fit(sample_phi, sample_y, _lambda) pred_sample_y = model.predict(sample_phi) pred_y = model.predict(gt_phi) sample_error = compute_mean_squared_error(pred_sample_y, sample_y) error = compute_mean_squared_error(pred_y, gt_y) candidate_sample_error.append((sample_error, candidate_id)) candidate_error.append(error) candidate_lambda.append(_lambda) candidate_pred_y.append(pred_y) _lambda *= _lambda_step log.write(str(_lambda) + '\n') candidate_error.sort() (sample_error, best_id) = candidate_sample_error[0] error = candidate_error[best_id] pred_y = candidate_pred_y[best_id] _lambda = candidate_lambda[best_id] log.write("lambda: " + str(_lambda) + '\n') log.write("Training MSE: " + str(sample_error) + '\n') log.write("Testing MSE: " + str(error) + '\n') sample_error = compute_mean_absolute_error(pred_sample_y, sample_y) error = compute_mean_absolute_error(pred_y, gt_y) log.write("Training MAE: " + str(sample_error) + '\n') log.write("Testing MAE: " + str(error) + '\n') visualize_people_counting(gt_y, pred_y, title, img_path)
#! -*- coding: utf-8 -*- import pandas as pd import numpy as np from sklearn import cross_validation from lasso import Lasso df = pd.read_csv("Boston.csv", index_col=0) y = df.iloc[:, 13].values df = (df - df.mean()) / df.std() # 基準化 X = df.iloc[:, :13].values model = Lasso(alpha=1.0, max_iter=1000) model.fit(X, y) print(model.intercept_) print(model.coef_) scores = cross_validation.cross_val_score(model, X, y, cv=5, scoring='mean_squared_error') print(np.mean(scores))
#! -*- coding: utf-8 -*- import pandas as pd import numpy as np from sklearn import cross_validation from lasso import Lasso df = pd.read_csv("Boston.csv", index_col=0) y = df.iloc[:, 13].values df = (df - df.mean())/df.std() # 基準化 X = df.iloc[:, :13].values model = Lasso(alpha=1.0, max_iter=1000) model.fit(X, y) print model.intercept_ print model.coef_ scores = cross_validation.cross_val_score(model, X, y, cv=5, scoring='mean_squared_error') print np.mean(scores)