Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spatial AUC #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion scripts/stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
from sklearn.metrics import roc_auc_score

from carbonplan_forest_risks import collect, fit, load, prepare, utils

Expand Down Expand Up @@ -31,6 +32,13 @@ def score(x, y, model, da, method):
b = b - b.mean()
spatial_r2 = 1 - np.sum((a - b) ** 2) / np.sum((a - np.mean(a)) ** 2)

a = da.mean("time").values.flatten()
b = prediction["prediction"].mean("time").values.flatten()
inds = ~np.isnan(a) & ~np.isnan(b)
a = a[inds]
b = b[inds]
spatial_auc = roc_auc_score(a > 0, b)

bias = (prediction['prediction'].mean().values - da.mean().values) / da.mean().values

return {
Expand All @@ -40,6 +48,7 @@ def score(x, y, model, da, method):
'annual_r2': annual_r2,
'seasonal_r2': seasonal_r2,
'spatial_r2': spatial_r2,
'spatial_auc': spatial_auc,
'bias': bias,
}

Expand Down Expand Up @@ -155,7 +164,7 @@ def append(df, results):
results = shuffle(x_z, y, mtbs['monthly'], f'shuffle_all_{index}')
df = append(df, results)

df = df[['method', 'roc', 'r2', 'annual_r2', 'seasonal_r2', 'spatial_r2', 'bias']]
df = df[['method', 'roc', 'r2', 'annual_r2', 'seasonal_r2', 'spatial_r2', 'spatial_auc', 'bias']]

print(df)

Expand Down