stratify=y)

        X = X_test
        y = y_test

    else:
        print("\nThe provided dataset does not exist!")
        sys.exit()

    if clustering_algo == 'k_means':

        #time-start
        start = timeit.default_timer()

        #object initialization
        km = my_kmeans.my_kmeans(X, y)

        if n_cluster == None:
            n_cluster = 3

        #fit
        km.fit(X, n_cluster)

        #predict
        predicted_y = km.predict(X)

        #elbow method
        km.elbow_method(X)

        #time-stop
        stop = timeit.default_timer()
Example #2
0
# display the vertices & normals
ax = plt.figure().gca(projection='3d')
ax.quiver(points[:, 0], points[:, 1], points[:, 2], normals[:, 0], normals[:, 1], normals[:, 2], length=0.01)
ax.set_title("Data")

# select the number of clusters
k = 5


print("Starting computation...")
# TODO:
# Use your k-means implementation to find 'k' cluster centers in
# the normal vectors.  The array 'map_to_centers' should contain the index of the
# center of each normal in 'VN'.
#
centers = np.random.standard_normal((k, 3))
centers=centers/np.linalg.norm(centers)
map_to_centers, center_colors = my_kmeans(points, k, centers)

# Cluster Visualization
colors=["r", "g", "b", "c", "m", "y", "k"]
ax = plt.figure().gca(projection='3d')
ax.set_title("Clustering")
# TODO:
# create a plot as above, where each cluster has a different color
ax.quiver(points[:, 0], points[:, 1], points[:, 2], normals[:, 0], normals[:, 1], normals[:, 2], length=0.01, colors=colors)
ax.set_title("Data")

print("done")
plt.show()


for i1 in range(len(ks)):
    # number of clusters
    k = ks[i1]
    print "Reiterate"
    # display image
    plot_image(i1, 0, colors)
    # plt.show()


    # <exercise>
    # Use your *own* k-means implementation to find 'k' cluster centers in
    # the colors 'colors'.  Assign the variable 'center_colors' with the
    # representative color of each center (dimension: k x 3).  The array
    # 'map_to_centers' should contain the center of each color in 'colors'.
    #
    # </exercise>
    map_to_centers, center_colors = my_kmeans(colors, k)
    print center_colors
    print map_to_centers
    

    plot_image(i1, 1, map_to_centers)
    print "Plotted"

    plot_image(i1, 2, center_colors[map_to_centers])
plt.show()

    data = data.reshape(img.shape[:2] + (-1,))
    if data.shape[2] == 1:
        data = data.reshape(img.shape[:2])
    axes[i1, i2].imshow(data.astype('uint8'))
    axes[i1, i2].set(xticks=[], yticks=[])

print("Starting computation...")

for i in range(len(clusterAmounts)):
    # TODO:
    # Use your k-means implementation to find 'k' cluster centers in
    # the colors 'colors'.  Assign the variable 'center_colors' with the
    # representative color of each center (dimension: k x 3).  The array
    # 'map_to_centers' should contain the center of each color in 'colors'.
    centers = np.random.random((clusterAmounts[i], 3)) * 255
    map_to_centers, center_colors = my_kmeans(colors,clusterAmounts[i], centers)

    # display image
    plot_image(i, 0, colors) # original image
    axes[i, 0].set_title("Original")
    
    plot_image(i, 1, map_to_centers) # clusters
    axes[i, 1].set_title("Clusters k={}".format(clusterAmounts[i]))
    
    # TODO:
    cluster_colors = np.empty_like(colors)
    # Plot a reconstruction of the image, using only the cluster centers as colors
    for k in range(clusterAmounts[i]):
        cluster_colors[map_to_centers == k] = center_colors[k]


# load data:
#   points:     (Nx3) vector of vertex coordinates
#   normals:     (Nx3) vector of vertex normals
data = scipy.io.loadmat(os.path.join(*'.. data.mat'.split()))
points, normals = data['points'], data['normals']

# display the vertices & normals
p_, n_ = points, normals
mlab.figure()
mlab.quiver3d(p_[:, 0], p_[:, 1], p_[:, 2], n_[:, 0], n_[:, 1], n_[:, 2])

# select the number of clusters
k = 5

# <exercise>
# Use your *own* k-means implementation to find 'k' cluster centers in
# the norms.  The array 'map_to_centers' should contain the index of the
# center of each normal in 'VN'.
#
# </exercise>
map_to_centers, _= my_kmeans(normals, k)
# display the clustering
mlab.figure()
n__ = normals * (map_to_centers + 1)[:, None]
mlab.quiver3d(p_[:, 0], p_[:, 1], p_[:, 2], n__[:, 0], n__[:, 1], n__[:, 2], scale_mode='none')
mlab.show()