Skip to content

Commit

Permalink
add accuracy score
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jun 15, 2024
1 parent 916a057 commit b431704
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions matsim/calibration/run_simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def process_results(runs):
"""Process results of multiple simulations"""
from sklearn.metrics import log_loss
from sklearn.metrics import log_loss, accuracy_score
from sklearn.preprocessing import LabelEncoder

print("Processing results in %s" % runs)
Expand All @@ -26,7 +26,6 @@ def process_results(runs):
for run in os.listdir(runs):
if not os.path.isdir(os.path.join(runs, run)):
continue
print("Processing run %s" % run)

df = pd.read_csv(os.path.join(runs, run, "analysis", "population", "mode_choices.csv"))
if dfs is None:
Expand Down Expand Up @@ -54,11 +53,16 @@ def process_results(runs):

y_pred[p.Index, j] = c / len(pred_cols)

accs = [accuracy_score(dfs.true_mode, dfs[col], sample_weight=dfs.weight) for col in pred_cols]
accs_d = [accuracy_score(dfs.true_mode, dfs[col], sample_weight=dfs.weight * dists) for col in pred_cols]

result = [
("Log likelihood", -log_loss(y_true, y_pred, sample_weight=dfs.weight, normalize=False), -log_loss(y_true, y_pred, sample_weight=dfs.weight * dists, normalize=False)),
("Log likelihood (normalized)", -log_loss(y_true, y_pred, sample_weight=dfs.weight, normalize=True), -log_loss(y_true, y_pred, sample_weight=dfs.weight * dists, normalize=True)),
("Mean Accuracy", np.mean(accs), np.mean(accs_d)),
("Log likelihood (null)", -log_loss(y_true, y_null, sample_weight=dfs.weight, normalize=False), -log_loss(y_true, y_null, sample_weight=dfs.weight * dists, normalize=False)),
("Samples", len(dfs), sum(dists)),
("Runs", len(pred_cols), len(pred_cols))
]

df = pd.DataFrame(result, columns=["Metric", "Value", "Distance weighted"]).set_index("Metric")
Expand Down

0 comments on commit b431704

Please sign in to comment.