diff --git a/boom/data/prepare_splits_10k.py b/boom/data/prepare_splits_10k.py index 7d61acf..80fa013 100644 --- a/boom/data/prepare_splits_10k.py +++ b/boom/data/prepare_splits_10k.py @@ -66,10 +66,18 @@ def prepare_splits( # Extract the hof values hof_values = np.array(hof_values).reshape(-1, 1) - density_KDE = KernelDensity(kernel="gaussian", bandwidth="scott").fit( + # sklearn's "scott" assumes sigma=1, so we compute it manually: + # h = n^(-1/(d+4)) * sigma + + n_samples_density, n_features_density = density_values.shape + scott_bw_density = n_samples_density ** (-1.0 / (n_features_density + 4)) * np.std(density_values) + density_KDE = KernelDensity(kernel="gaussian", bandwidth=scott_bw_density).fit( density_values ) - hof_KDE = KernelDensity(kernel="gaussian", bandwidth="scott").fit(hof_values) + + n_samples_hof, n_features_hof = hof_values.shape + scott_bw_hof = n_samples_hof ** (-1.0 / (n_features_hof + 4)) * np.std(hof_values) + hof_KDE = KernelDensity(kernel="gaussian", bandwidth=scott_bw_hof).fit(hof_values) density_kde_scores = density_KDE.score_samples(density_values) hof_kde_scores = hof_KDE.score_samples(hof_values) diff --git a/boom/data/prepare_splits_qm9.py b/boom/data/prepare_splits_qm9.py index 8c1465f..e6c8a0a 100644 --- a/boom/data/prepare_splits_qm9.py +++ b/boom/data/prepare_splits_qm9.py @@ -168,7 +168,12 @@ def prepare_splits_qm9( property_values = np.array(property_values).reshape(-1, 1).astype(np.float64) print("Starting Kernel Density Estimation for " + property_name) - property_KDE = KernelDensity(kernel="gaussian", bandwidth="scott").fit( + # sklearn's "scott" assumes sigma=1, so we compute it manually: + # h = n^(-1/(d+4)) * sigma + + n_samples, n_features = property_values.shape + scott_bandwidth = n_samples ** (-1.0 / (n_features + 4)) * np.std(property_values) + property_KDE = KernelDensity(kernel="gaussian", bandwidth=scott_bandwidth).fit( property_values ) print("Kernel Density Estimation Done!")