def cost(self, **kwargs): (destination_hat, time_hat) = self.predict(**kwargs) destination = tensor.concatenate((kwargs['destination_latitude'][:, None], kwargs['destination_longitude'][:, None]), axis=1) time = kwargs['travel_time'] destination_cost = error.erdist(destination_hat, destination).mean() time_cost = error.rmsle(time_hat.flatten(), time.flatten()) self.add_auxiliary_variable(destination_cost, [roles.COST], 'destination_cost') self.add_auxiliary_variable(time_cost, [roles.COST], 'time_cost') return destination_cost + self.config.time_cost_factor * time_cost
def cross_validate(X,y,model,folds=5,random_seed=42,test_size=.2,model_name='',parameters={},plot=False): """ ----------------------- cross validate models ----------------------- """ print ' ------------------ Cross Validation using %s model -------------------- '% model_name mses=[];rmses=[];rmsles=[] for fold in range(folds): #! create a test and train cv set train_cv, test_cv, y_target, y_true = cross_validation.train_test_split(X, y, test_size=test_size, random_state=fold*random_seed) #! train model and make predictions model.fit(train_cv, y_target) preds = model.predict(test_cv) #! measure the error (difference between the predictions and the actual targets) mse = error.mse(y_true, preds) rmse = error.rmse(y_true, preds) rmsle = error.rmsle(y_true, preds) print "(fold %d of %d) MSE : %f | RMSE: %f | RMSLE: %f %s" % (fold + 1, folds, mse, rmse, rmsle, '') mses.append(mse); rmses.append(rmse); rmsles.append(rmsle) print ">>> Mean MSE: %f | Mean RMSE: %f | Mean RMSLE: %f <<<" % (np.mean(mses), np.mean(rmses), np.mean(rmsles)) print "______________________________________________________________" if plot: plot_error(range(folds), rmsles,"Fold", "RMSLE", "Cross Validation using %s model" % model_name, ["RMSLE"]) # ---------------------- Scrap Notes --------------------------------- # models = [(linear_model.SGDRegressor, ), (linear_model.Ridge, ), ()] # param_grid = {'alpha': [0.001, 0.01, 0.5]} #,1,5, 10, 100, 1000] } #clf = linear_model.Ridge(alpha=a) #clf = linear_model.SGDRegressor(alpha=0.2,n_iter=1000,shuffle=True) #clf = linear_model.LassoCV(cv=3) #clf = linear_model.ElasticNet() #clf = linear_model.BayesianRidge() #clf = ensemble.RandomForestRegressor(n_estimators=100,random_state=42*idx*10,max_depth=4) #clf = ensemble.ExtraTreesRegressor(n_estimators=100,random_state=42*idx*10,max_depth=4) #clf = ensemble.GradientBoostingRegressor(alpha=a,n_estimators=100,random_state=42,max_depth=4)
def cost(self, **kwargs): (destination_hat, time_hat) = self.predict(**kwargs) destination = tensor.concatenate( (kwargs['destination_latitude'][:, None], kwargs['destination_longitude'][:, None]), axis=1) time = kwargs['travel_time'] destination_cost = error.erdist(destination_hat, destination).mean() time_cost = error.rmsle(time_hat.flatten(), time.flatten()) self.add_auxiliary_variable(destination_cost, [roles.COST], 'destination_cost') self.add_auxiliary_variable(time_cost, [roles.COST], 'time_cost') return destination_cost + self.config.time_cost_factor * time_cost
def cost(self, **kwargs): y_hat = self.predict(**kwargs) y = kwargs['travel_time'] return error.rmsle(y_hat, y)