def train_test(phi_train, y_train, phi_test, y_test, lam_ridge=0.0, scale_price=1.0, dates=None, bool_draw=False): beta = np.linalg.inv(phi_train.T.dot(phi_train) + np.identity(phi_train.shape[1])*lam_ridge).dot(phi_train.T).dot(y_train) y_pred = phi_test.dot(beta) if bool_draw: dates = [datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%d %a') for date in dates] draw_compare(y_pred*scale, Y_test*scale, dates, node=NODE_NAME) draw_heat(beta, NODE_NAME) return cal_mse(phi_train.dot(beta), y_train)/(scale_price**2), cal_mse(y_pred, y_test)/(scale_price**2)
def train_test(phi, yy, idx_train, idx_test, lam_ridge=0.0, price_mu=0.0, price_std=1.0, verbose=False, case='rt'): phi_train = phi[idx_train, :] y_train = yy[idx_train, :] phi_test = phi[idx_test, :] y_test = yy[idx_test, :] beta = np.linalg.inv(phi_train.T.dot(phi_train) + np.identity(phi_train.shape[1])*lam_ridge).dot(phi_train.T).dot(y_train) y_pred = phi_test.dot(beta) if verbose: if case == 'da': y_pred = y_pred.reshape((y_pred.shape[0]*24, 1)) y_test = y_test.reshape((y_test.shape[0]*24, 1)) draw_compare(y_pred*price_std+price_mu, y_test*price_std+price_mu) draw_heat(beta, NODE_NAME) return cal_mse(phi_train.dot(beta), y_train), cal_mse(y_pred, y_test)