X_all = mm.get_all_features_except_response(response, index_big) num_features_all = X_all.shape[1] y_all = mm.get_data().ix[index_big, response] mean_y_all = np.mean(y_all) F, p_vals = f_regression(X_all, y_all) index_reg_reduced = p_vals <= p_val X_reg = X_all.ix[:, index_reg_reduced] num_features_reg = X_reg.shape[1] print("P-value reduced features: ", k_full.columns.values[index_reg_reduced]) for alpha in np.arange(0.1, 2.1, 0.1): # **************************** Full Data Set *************************** # grake_lasso = GraKeLasso(k_full.as_matrix(), alpha) glasso = GraKeLasso(dependency_graph_full.as_matrix(), alpha) lasso = Lasso(alpha=alpha, fit_intercept=True, normalize=True, precompute='auto', copy_X=True, max_iter=n_iter, tol=1e-4, positive=False) elasticNet = ElasticNet(alpha=alpha, l1_ratio=0.5, fit_intercept=True, normalize=True, precompute='auto', max_iter=n_iter, copy_X=True, tol=1e-4, positive=False) lambda_glasso = glasso.cross_val_lambda(X_all, y_all, n_fold, n_iter, lambda_range, model=None) lambda_grake_lasso = grake_lasso.cross_val_lambda(X_all, y_all, n_fold, n_iter, lambda_range, model=None) lambda_elastic_net = grake_lasso.cross_val_lambda(X_all, y_all, n_fold, n_iter, lambda_range, model=elasticNet) print("Full Data Set:") print("Evaluating Lasso:") lasso_full, lasso_full_red = grake_lasso.cross_val(X_all, y_all, n_fold, n_iter, 0, model=lasso) print("Evaluating Elastic Net:") eNet_full, eNet_full_red = grake_lasso.cross_val(X_all, y_all, n_fold, n_iter, lambda_elastic_net, model=elasticNet) print("Evaluating GraKeLasso:") grake_full, grake_full_red = grake_lasso.cross_val(X_all, y_all, n_fold, n_iter, lambda_grake_lasso, model=None)
__author__ = 'martin' from learning.grakelasso import GraKeLasso, ModelManager import numpy as np lambd = 0.1 alpha = 1 num_examples = 1000 response = "TestingProduct" # *************** Load Data ************** # mm = ModelManager() mm.load_data(["../data/test.txt"]) kernel_lap = mm.load_kernel_laplacian("../data/laplacian.csv") data = mm.get_data() index_sparse = np.ones(num_examples, dtype=bool) index_sparse = np.concatenate((index_sparse, np.zeros(mm.num_examples() - num_examples - 1, dtype=bool))) np.random.shuffle(index_sparse) X_sparse = mm.get_all_features_except_response(response, index_sparse) y_sparse = data.ix[index_sparse, response] # Evaluate GraKeLasso klasso = GraKeLasso(kernel_lap.as_matrix(), alpha) rmse, avg_theta = klasso.cross_val(X_sparse, y_sparse, 10, 10000, lambd) print("MSE and Coefficient Reduction ", rmse, avg_theta)