def eval(model, data, set_name, denorm_predictions=True): # predictions predictions = model.predict(data.dataset(set_name)) labels = data.raw_data(set_name)["labels"][:len(predictions)] predictions = pd.DataFrame(data=predictions, index=labels.index, columns=labels.columns) if denorm_predictions: predictions = data.denormalize_labels(predictions) # results results = { "general": { "mae": mae(labels, predictions), "mape": mape(labels, predictions), "mse": mse(labels, predictions) } } for col in labels.columns: results[col] = { "mae": mae(labels[col], predictions[col]), "mape": mape(labels[col], predictions[col]), "mse": mse(labels[col], predictions[col]), "tend_acc": tendency_accuracy(labels[col], predictions[col]) } return predictions, results
def linear_model(data): """Build, train, and test sklearn linear model.""" print('\nBuilding sklearn linear model with only numeric data ...') print('#' * 65) m = LinearRegression() m.fit(data['x_train_numeric'], data['y_train']) print('r^2 score:', m.score(data['x_test_numeric'], data['y_test'])) y_pred = m.predict(data['x_test_numeric']) print('MSE:', mse(data['y_test'], y_pred)) print('MAE:', mae(data['y_test'], y_pred)) print('MAPE:', 100 * mape(data['y_test'], y_pred))
def evaluate(ticker): '''Evaluate model performance''' print('Retrieving data') predictions = pd.concat([pd.read_csv(f, parse_dates=['ds']) for f in os.listdir() if f.endswith('.csv')]) df = yf.Ticker(ticker)\ .history(start=predictions.ds.min().to_pydatetime(), end=predictions.ds.max().to_pydatetime(), interval='1m') df.index = df.index.floor('1min') df.index = df.index.tz_convert('UTC') df = df.loc[predictions.ds.min():predictions.ds.max()] df = df.reset_index() df = df.merge(predictions, left_on='Datetime', right_on='ds', how='inner')\ .rename(columns={'Close': 'Actuals', 'yhat': 'Predicted'})\ .set_index('Datetime') df = df[['Actuals', 'Predicted']] print('Metric calculation') mape_value = mape(df.Actuals, df.Predicted) * 100 print('Plotting') df.plot(title=f'Actuals vs. Predicted MAPE={round(mape_value, 2)}%') plt.savefig('forecast_plot.png', dpi=400)
def lgb_mape(pred, real): '''sklearn.metrics.mean_absolute_percentage_error wrapper for LGB''' is_higher_better = False score = mape(real.label, pred) return 'lgb_mape', score, is_higher_better