def run_lasso_model(X,y): X_train, X_test, y_train, y_test = train_test_split(X, y) standardizer = utils.XyScaler() standardizer.fit(X_train,y_train) X_train_std, y_train_std = standardizer.transform(X_train, y_train) X_test_std, y_test_std = standardizer.transform(X_test, y_test) lasso = LassoCV(alphas = np.logspace(-2,4,num=250),cv=10) lasso.fit(X_train_std,y_train_std) y_hats_std = lasso.predict(X_test_std) X_test, y_hats = standardizer.inverse_transform(X_test_std,y_hats_std) lasso_score = r2_score(y_test_std,y_hats_std) return lasso, lasso_score, y_hats, y_test, X_test
def run_ridge_model(X,y): X_train, X_test, y_train, y_test = train_test_split(X, y) standardizer = utils.XyScaler() standardizer.fit(X_train,y_train) X_train_std, y_train_std = standardizer.transform(X_train, y_train) X_test_std, y_test_std = standardizer.transform(X_test, y_test) ridge = RidgeCV(alphas = np.logspace(-2,4,num=250),cv=10) ridge.fit(X_train_std,y_train_std) y_hats_std = ridge.predict(X_test_std) X_test, y_hats = standardizer.inverse_transform(X_test_std,y_hats_std) ridge_score = r2_score(y_test_std,y_hats_std) return ridge, ridge_score, y_hats, y_test, X_test
def make_qqplot_route(): boulders, routes = grade_model.import_data() route = grade_model.drop_columns_grades(routes) X_train1, y_train1, X_hold, y_hold = grade_model.split_data_grades( route, 'usa_routes') X_train2, X_test, y_train2, y_test = train_test_split(X_train1, y_train1) standardizer = utils.XyScaler() standardizer.fit(X_train2, y_train2) X_train_std, y_train_std = standardizer.transform(X_train2, y_train2) X_test_std, y_test_std = standardizer.transform(X_test, y_test) X_hold_std, y_hold_std = standardizer.transform(X_hold, y_hold) ridge = RidgeCV(alphas=np.logspace(-2, 4, num=250), cv=10) ridge.fit(X_train_std, y_train_std) residuals = y_hold_std - ridge.predict(X_hold_std) sm.graphics.qqplot(residuals) plt.savefig('images/routes_qqplot.png', fit=True, line='45') plt.show()
def test_model_on_hold(X_train,y_train,X_hold,y_hold,model,alpha,title,filename): standardizer = utils.XyScaler() standardizer.fit(X_train,y_train) X_train_std, y_train_std = standardizer.transform(X_train, y_train) X_hold_std, y_hold_std = standardizer.transform(X_hold, y_hold) final_model = model(alpha) final_model.fit(X_train_std,y_train_std) y_pred_std = final_model.predict(X_hold_std) X_hold, y_pred = standardizer.inverse_transform(X_hold_std,y_pred_std) plot_model_predictions(y_hold, y_pred, title, filename) final_score = final_model.score(X_hold_std,y_hold_std) final_mse_std = mse(y_hold_std,y_pred_std) final_mse = mse(y_hold,y_pred) print('Final R2 score: {}'.format(final_score)) print('Final standardized RMSE: {}'.format(np.sqrt(final_mse_std))) print('Final RMSE: {}'.format(np.sqrt(final_mse))) return final_score, final_mse
def __init__(self, X, y): self.X = X self.y = y self.scaler = ut.XyScaler()