def initialize(init, k, G, X): """Initializaton function to use in energy Lloyd and Hartigan.""" if init == "spectral": z0 = initialization.topeigen(k, G) Z0 = eclust.ztoZ(z0) elif init == "k-means++": z0 = initialization.kmeanspp(k, X) Z0 = eclust.ztoZ(z0) elif init == "random": z0 = np.random.randint(0, k, len(X)) Z0 = eclust.ztoZ(z0) else: raise ValueError("No initialization method provided") return Z0
def initialize(init, k, G, X): """Initializaton function to use in energy Lloyd and Hartigan.""" if init == "spectral": z0 = initialization.topeigen(k, G) Z0 = eclust.ztoZ(z0) elif init == "k-means++": z0 = initialization.kmeanspp(k, X) Z0 = eclust.ztoZ(z0) elif init == "random": z0 = np.random.randint(0, k, len(X)) Z0 = eclust.ztoZ(z0) else: raise ValueError("No initialization method provided") return Z0
def energy_hartigan(k, X, G, run_times=5, return_labels=False): best_score = -np.inf for rt in range(run_times): z0 = initialization.kmeanspp(k, X) Z0 = eclust.ztoZ(z0) zh = eclust.energy_hartigan(k, G, Z0, max_iter=300) Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_zh = zh if return_labels: return best_zh else: return best_score
def energy_hartigan(k, X, G, run_times=5, return_labels=False): best_score = -np.inf for rt in range(run_times): z0 = initialization.kmeanspp(k, X) Z0 = eclust.ztoZ(z0) zh = eclust.energy_hartigan(k, G, Z0, max_iter=300) Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_zh = zh if return_labels: return best_zh else: return best_score
def energy_hartigan(k, X, G, run_times=5, init="spectral"): """Run few times and pick the best objective function value.""" best_score = -np.inf for rt in range(run_times): Z0 = initialize(init, k, G, X) zh = eclust.energy_hartigan(k, G, Z0, max_iter=300) Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_z = zh return best_z
def energy_hartigan(k, X, G, run_times=10, init="spectral"): """Run few times and pick the best objective function value.""" best_score = -np.inf for rt in range(run_times): Z0 = initialize(init, k, G, X) zh = eclust.energy_hartigan(k, G, Z0, max_iter=300) Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_z = zh return best_z
def energy_spectral(k, X, G, run_times=5, init="random"): """Run few times and pick the best objective function value. Choose the initializatio for k-means, which can be k-means++ or random. """ best_score = -np.inf for rt in range(run_times): zh = initialization.topeigen(k, G, run_times=run_times, init="random") Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_z = zh return best_z
def energy_spectral(k, X, G, run_times=10, init="random"): """Run few times and pick the best objective function value. Choose the initializatio for k-means, which can be k-means++ or random. """ best_score = -np.inf for rt in range(run_times): zh = initialization.topeigen(k, G, run_times=run_times, init="random") Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_z = zh return best_z
def energy_hartigan(k, X, G, z, run_times=10, init="spectral"): """Run few times and pick the best objective function value.""" best_score = -np.inf for rt in range(run_times): Z0 = initialize(init, k, G, X) zh = eclust.energy_hartigan(k, G, Z0, max_iter=300) Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_z = zh a = metric.accuracy(z, best_z) v = metric.variation_information(z, best_z) return a, v
def energy_lloyd(k, X, G, z, run_times=10, init="spectral"): """Run few times and pick the best objective function value.""" best_score = -np.inf for rt in range(run_times): Z0 = initialize(init, k, G, X) zh = eclust.energy_lloyd(k, G, Z0, max_iter=300) Zh = eclust.ztoZ(zh) score = eclust.objective(Zh, G) if score > best_score: best_score = score best_z = zh a = metric.accuracy(z, best_z) v = metric.variation_information(z, best_z) return a, v