def test_regression(w, test, ds): # get data data = fl.format_inputs(test, ds) # predict results m = rg.iter2matrix(data) x = m.T[:-1].T x = rg.append_ones(x) y = m.T[-1:].T # test error return rg.mse_error(x, y, w)
def train_grad_descent(training, params): data,ds = transform_train_set(training) # generate regression m = rg.iter2matrix(data) x = m.T[:-1].T x = rg.append_ones(x) y = m.T[-1:].T init_weight = np.ones((x.shape[1],1)) w = gd.grad_descent(x,y,init_weight,params["alpha"],params["iterations"]) # get training error terror = rg.mse_error(x,y,w) return (w, ds, terror)
def train_regression(training, params={}): # get data data,ds = transform_train_set(training) # generate regression m = rg.iter2matrix(data) x = m.T[:-1].T x = rg.append_ones(x) y = m.T[-1:].T w = rg.regression_weights(x,y) # get training error terror = rg.mse_error(x,y,w) return (w, ds, terror)
def train_lasso(training, params={}): # get data # t = fl.list2ordered_dict(training) # data,ds = fl.get_data2(t) data,ds = transform_train_set(training) # generate regression m = rg.iter2matrix(data) x = m.T[:-1].T y = m.T[-1:].T w = rl.lasso_reg(x,y) # get training error x = rg.append_ones(x) terror = rg.mse_error(x,y,w) return (w, ds, terror)
import unicodecsv as csv import regression as rg dataf = open('data/features/feature_data.csv', 'rt') datac = csv.reader(dataf) next(datac) m = rg.iter2matrix(datac) x = m.T[:-1].T x = rg.append_ones(x) y = m.T[-1:].T init_weight = np.ones((x.shape[1], 1)) final_weight = grad_descent(x, y, init_weight, 0.00004, 1000) print final_weight print rg.mse_error(x, y, final_weight)
import unicodecsv as csv import numpy as np import scipy as sp import math import regression as rg dataf = open('data/features/feature_data.csv', 'rt') datac = csv.reader(dataf) next(datac) m = rg.iter2matrix(datac) x1 = m.T[:-1].T x = rg.append_ones(x1) y = m.T[-1:].T w = rg.regression_weights(x,y) lse = rg.lse_error(x, y, w) mse = rg.mse_error(x, y, w) print "weights: " + str(w) print "lse: " + str(lse) print "mse: " + str(mse)
import unicodecsv as csv import regression as rg dataf = open("data/features/feature_data.csv", "rt") datac = csv.reader(dataf) next(datac) m = rg.iter2matrix(datac) x = m.T[:-1].T x = rg.append_ones(x) y = m.T[-1:].T init_weight = np.ones((x.shape[1], 1)) final_weight = grad_descent(x, y, init_weight, 0.00004, 1000) print final_weight print rg.mse_error(x, y, final_weight)