Skip to content

Instantly share code, notes, and snippets.

@davidlandry93
Last active June 3, 2025 09:48
Show Gist options
  • Save davidlandry93/d52cbf3e0c30018e2fba8b0c5ebbdea3 to your computer and use it in GitHub Desktop.
Save davidlandry93/d52cbf3e0c30018e2fba8b0c5ebbdea3 to your computer and use it in GitHub Desktop.
import numpy as np
import tqdm
from scipy.sparse.csgraph import minimum_spanning_tree
def prerank_minimum_spanning_tree(ensemble):
n_samples = ensemble.shape[0]
n_members = ensemble.shape[1]
preranks = np.zeros((n_samples, n_members))
for i in tqdm.tqdm(list(range(n_samples))):
for j in range(n_members):
ensemble_omit_j = np.delete(ensemble[i], j, axis=0)
ensemble_omit_j_t1 = ensemble_omit_j[:, np.newaxis, :]
ensemble_omit_j_t2 = ensemble_omit_j[np.newaxis, :, :]
distances = np.sqrt(
np.square(ensemble_omit_j_t1 - ensemble_omit_j_t2).sum(axis=-1)
)
mst = minimum_spanning_tree(distances).toarray()
preranks[i, j] = mst.sum()
return preranks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment