Exemplo n.º 1
0
    for step in steps:
        fi_dict = model.extras.feature_importances[period][step]
        step_df = pd.DataFrame(fi.reorder_fi_dict(fi_dict))
        step_df = step_df.rename(columns={"importance": f"s={step}"})
        step_df.set_index("feature", inplace=True)
        df = df.join(step_df) if step > steps[0] else step_df.copy()

    df = df.sort_values(by=[f"s={sort_step}"], ascending=False)
    df = df[0:top + 1]
    return df


# Paramaters.
sort_step = 3
top = 30
period = "test"

for model in models:
    model.extras._get_feature_importances()
    fi_cm = featimp_by_steps(model=model,
                             period=period,
                             steps=model.steps,
                             sort_step=sort_step,
                             top=top,
                             cols=model.cols_features)
    fi.write_fi_tex(
        pd.DataFrame(fi_cm),
        os.path.join(
            out_paths["features"],
            f"impurity_imp_task{task}_{model.name}_{level}_dummy.tex"))
Exemplo n.º 2
0
        """
        tex = meta + tex
        path_out = os.path.join(out_paths["evaluation"],
                                f"{model.name}_{level}_{calib}_scores.tex")
        with open(path_out, "w") as f:
            f.write(tex)
        print(f"Wrote scores table to {path_out}.")

for model in models:
    fi_cm = featimp_by_steps(model=model,
                             steps=steps,
                             sort_step=sort_step,
                             top=top,
                             cols=model.cols_features)
    fi.write_fi_tex(
        pd.DataFrame(fi_cm),
        os.path.join(out_paths["features"],
                     f"impurity_imp_{model.name}_{level}.tex"))

sort_step = 3
top = 30

for model in models:
    for step in steps:
        pi_dict = model.extras.permutation_importances["test"][step]["test"]
        step_df = pd.DataFrame(fi.reorder_fi_dict(pi_dict))
        step_df = step_df.rename(columns={"importance": f"s={step}"})
        step_df.set_index("feature", inplace=True)
        pi_df = pi_df.join(step_df) if step > steps[0] else step_df.copy()

    pi_df = pi_df.sort_values(by=[f"s={sort_step}"], ascending=False)
    pi_df = pi_df[0:top + 1]
print(models[0].name)
# Period B step 1
models[0].scores["B"][1]

# Notice all features and predictions in the same dataframe, no more a/b/c
# Instead we subset by the periods when needed

cols_predict = [model.col_sc_calibrated for model in models] + [ensemble.col_sc for ensemble in ensembles]

# All calibrated predictions for period C
df.loc[period_c.times_predict, cols_predict]

sort_step = 3
top = 30

for model in models:
    for step in steps:
        pi_dict = model.extras.permutation_importances["test"][step]["test"]
        step_df = pd.DataFrame(fi.reorder_fi_dict(pi_dict))
        step_df = step_df.rename(columns={"importance": f"s={step}"})
        step_df.set_index("feature", inplace=True)
        pi_df = pi_df.join(step_df) if step > steps[0] else step_df.copy()

    pi_df = pi_df.sort_values(by=[f"s={sort_step}"], ascending=False)
    pi_df = pi_df[0:top + 1]

    fi.write_fi_tex(
        pi_df,
        os.path.join(out_paths["features"], f"permutation_imp_{model.name}.tex")
    )