Beispiel #1
0
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_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