예제 #1
0
def k_means(points, k, iterations=10):
    """
    K Means Unsupervised ML Algorithm Implementation with Forgy Initialization
    Input:
        points (numpy array): a 2D (M,N) Array with the data to cluster.
        M = Instances, N = variables.
        k (int): The number of clusters to find
    """
    idx = np.random.randint(len(points),size=k)

    centroids = points[idx,:]
    clusters = get_clusters(points,centroids)

    for i in range(iterations):

        # Use get_clusters() and update_centroids() for each iteration.

    return clusters,centroids


if __name__ == "__main__":
    data = load_data('./data/iris.data')
    k = 3

    X = np.array([f[:-1] for f in data])
    y = np.array([f[-1] for f in data])

    clusters,centroids = k_means(X,3)

    show_clusters_centroids(clusters, centroids, "Result", keep=True)
예제 #2
0
def k_means(points, k, iterations=10):
    """
    K Means Unsupervised ML Algorithm Implementation with Forgy Initialization
    Input:
        points (numpy array): a 2D Array with the data to cluster.
        k (int): The number of clusters to find
    """
    idx = np.random.randint(len(points),size=k)

    centroids = points[idx,:]
    clusters = get_clusters(points,centroids)

    for i in range(iterations):

        if i % 1 == 0:
            if i == 0:
                title = "Initialization"
            else:
                title = "Iteration {}".format(i+1)

            show_clusters_centroids(
                clusters,
                centroids,
                title,
            )

        clusters = get_clusters(points,centroids)
        centroids = update_centroids(clusters)

    return clusters,centroids
예제 #3
0
def k_means(puntos):
    cantidad_de_centros = 0
    points = np.array(puntos)

    cantidad_de_centros = int(input("Elija la cantidad de centros: "))
    system('clear')
    iterationts = int(input("Elija la cantidad de iteraciones: "))

    idx = np.random.randint(len(points), size=cantidad_de_centros)
    k_lista = points[idx, :]
    clusters = cercanos(points, k_lista)

    # Redifine puntos and centers adjusting with the methods cercanos() and centros()
    for i in range(iterationts):
        if i % 1 == 0:
            if i == 0:
                title = "Initialization"
            else:
                title = "Iteration {}".format(i + 1)

        show_clusters_centroids(clusters, k_lista, title)
        clusters = cercanos(points, k_lista)
        k_lista = centros(clusters)

    # Return the new adjustes list
    return clusters, k_lista
예제 #4
0
def main():
    filename = './iris.data'
    data = load_data(filename)
    #consigue todas las columnas, menos la ultima
    X = np.array([f[:-1] for f in data])
    #consigue la ultima columna, de nombres
    y = np.array([f[-1] for f in data])
    #inicializa k_means
    clusters, centros = k_means(X)
    #imprime los valores de los centros, en 4 dimensiones
    print(centros)
    #genera una grafica con los valores finales
    show_clusters_centroids(clusters, centros, "Result", keep=True)
    #muestra la grafica
    plt.show()
예제 #5
0
def k_means(puntos, k, iterations=10):
    idx = np.random.randint(len(puntos), size=k)

    centros = puntos[idx, :]
    clusters = cercanos(puntos, centros)

    for i in range(iterations):

        show_clusters_centroids(
            clusters,
            centros,
            "Graph",
        )

        clusters = cercanos(puntos, centros)
        centros = find_centros(clusters)

    return clusters, centros
예제 #6
0
def k_means(puntos, k, iterations=10):
    idx = np.random.randint(len(puntos), size=k)

    centros = puntos[idx, :]
    clusters = cercanos(puntos, centros)

    for i in range(iterations):

        show_clusters_centroids(
            clusters,
            centros,
            "Graph",
        )

        clusters = cercanos(puntos, centros)
        centros = find_centros(clusters)

    return clusters, centros


if __name__ == "__main__":
    data = load_data('./data/iris.data')
    k = 4

    X = np.array([f[:-1] for f in data])
    y = np.array([f[-1] for f in data])

    clusters, centros = k_means(X, k)

    show_clusters_centroids(clusters, centros, "Result", keep=True)