def linear_nn( experiment="one_month_forecast", include_pred_month=True, surrounding_pixels=None, explain=False, static="features", ignore_vars=None, num_epochs=50, early_stopping=5, layer_sizes=[100], predict_delta=False, spatial_mask=None, include_latlons=False, ): predictor = LinearNetwork( layer_sizes=layer_sizes, data_folder=get_data_path(), experiment=experiment, include_pred_month=include_pred_month, surrounding_pixels=surrounding_pixels, static=static, ignore_vars=ignore_vars, predict_delta=predict_delta, spatial_mask=spatial_mask, include_latlons=include_latlons, ) predictor.train(num_epochs=num_epochs, early_stopping=early_stopping) predictor.evaluate(save_preds=True) predictor.save_model() if explain: _ = predictor.explain(save_shap_values=True)
def linear_nn(experiment='one_month_forecast', include_pred_month=True, surrounding_pixels=1): # if the working directory is alread ml_drought don't need ../data if Path('.').absolute().as_posix().split('/')[-1] == 'ml_drought': data_path = Path('data') else: data_path = Path('../data') predictor = LinearNetwork(layer_sizes=[100], data_folder=data_path, experiment=experiment, include_pred_month=include_pred_month, surrounding_pixels=surrounding_pixels) predictor.train(num_epochs=50, early_stopping=5) predictor.evaluate(save_preds=True) predictor.save_model() _ = predictor.explain(save_shap_values=True)
def linear_nn( experiment="one_month_forecast", include_pred_month=True, surrounding_pixels=None, ignore_vars=None, pretrained=False, ): predictor = LinearNetwork( layer_sizes=[100], data_folder=get_data_path(), experiment=experiment, include_pred_month=include_pred_month, surrounding_pixels=surrounding_pixels, ignore_vars=ignore_vars, ) predictor.train(num_epochs=50, early_stopping=5) predictor.evaluate(save_preds=True) predictor.save_model() _ = predictor.explain(save_shap_values=True)
x_vars = np.repeat(val.x_vars, 11) df = pd.DataFrame( dict(variable=x_vars, month=np.tile(np.arange(1, 12), 5), value=values) ) fig, axs = plt.subplots(3, 2, sharex=True, sharey=True) for i, var in enumerate(val.x_vars): d = df.loc[df.variable == var] ax_ix = np.unravel_index(i, (3, 2)) ax = axs[ax_ix] d.plot(x="month", y="value", ax=ax, label=var, color=sns.color_palette()[i]) ax.axhline(0, "k--", alpha=0.7) fig.savefig("scripts/ts_variable_importance_linear_regression.png", dpi=300) # ------------------------------------------------------------------------------ # Explain the Linear Network # ------------------------------------------------------------------------------ test_arrays_loader = DataLoader( data_path=data_path, batch_file_size=1, shuffle_data=False, mode="test", to_tensor=True, ) key, val = list(next(iter(test_arrays_loader)).items())[0] explanations = ln.explain(val.x) #