def two_blobs_clustering(): """ TO BE COMPLETED Clustering of two blobs. Used in questions 2.1 and 2.2 """ # Get data and compute number of classes X, Y = blobs(50, n_blobs=2, blob_var=0.15, surplus=0) num_classes = len(np.unique(Y)) """ Choose parameters """ k = 0 var = 1.0 # exponential_euclidean's sigma^2 laplacian_normalization = 'unn' chosen_eig_indices = [0, 1, 2] # indices of the ordered eigenvalues to pick if k == 0: # compute epsilon dists = sd.cdist( X, X, 'euclidean' ) # dists[i, j] = euclidean distance between x_i and x_j min_tree = min_span_tree(dists) l = [] n1, m1 = min_tree.shape for i in range(n1): for j in range(m1): if min_tree[i][j] == True: l.append(dists[i][j]) #distance_threshold = sorted(l)[-1] distance_threshold = sorted(l)[-2] eps = np.exp(-(distance_threshold)**2.0 / (2 * var)) ##### # build laplacian W = build_similarity_graph(X, var=var, eps=eps, k=k) plot_graph_matrix(X, Y, W) L = build_laplacian(W, laplacian_normalization) # run spectral clustering Y_rec = spectral_clustering(L, chosen_eig_indices, num_classes=num_classes) # Plot results plot_clustering_result(X, Y, L, Y_rec, KMeans(num_classes).fit_predict(X))
def plot_similarity_graph(X, Y, var=1.0, eps=0.0, k=5): """ Function to plot the similarity graph, given data and parameters. :param X: (n x m) matrix of m-dimensional samples :param Y: (n, ) vector with cluster assignments :param var: the sigma value for the exponential function, already squared :param eps: threshold eps for epsilon graphs :param k: the number of neighbours k for k-nn :return: """ # use the build_similarity_graph function to build the graph W # W: (n x n) dimensional matrix representing the adjacency matrix of the graph W = build_similarity_graph(X, var, eps, k) # Use auxiliary function to plot plot_graph_matrix(X, Y, W)
def how_to_choose_epsilon(): """ TO BE COMPLETED. Consider the distance matrix with entries dist(x_i, x_j) (the euclidean distance between x_i and x_j) representing a fully connected graph. One way to choose the parameter epsilon to build a graph is to choose the maximum value of dist(x_i, x_j) where (i,j) is an edge that is present in the minimal spanning tree of the fully connected graph. Then, the threshold epsilon can be chosen as exp(-dist(x_i, x_j)**2.0/(2*sigma^2)). """ # the number of samples to generate num_samples = 100 # the option necessary for worst_case_blob, try different values gen_pam = 2.0 # to understand the meaning of the parameter, read worst_case_blob in generate_data.py # get blob data X, Y = worst_case_blob(num_samples, gen_pam) # get two moons data # X, Y = two_moons(num_samples) n = X.shape[0] """ use the distance function and the min_span_tree function to build the minimal spanning tree min_tree - var: the exponential_euclidean's sigma2 parameter - dists: (n x n) matrix with euclidean distance between all possible couples of points - min_tree: (n x n) indicator matrix for the edges in the minimal spanning tree """ var = 1.0 dists = pairwise_distances(X).reshape( (n, n)) # dists[i, j] = euclidean distance between x_i and x_j min_tree = min_span_tree(dists) """ set threshold epsilon to the max weight in min_tree """ distance_threshold = np.max(dists[min_tree]) eps = np.exp(-distance_threshold**2 / (2 * var)) """ use the build_similarity_graph function to build the graph W W: (n x n) dimensional matrix representing the adjacency matrix of the graph use plot_graph_matrix to plot the graph """ W = build_similarity_graph(X, var=var, eps=eps, k=0) plot_graph_matrix(X, Y, W)
def how_to_choose_epsilon(gen_pam, k): """ TO BE COMPLETED. Consider the distance matrix with entries dist(x_i, x_j) (the euclidean distance between x_i and x_j) representing a fully connected graph. One way to choose the parameter epsilon to build a graph is to choose the maximum value of dist(x_i, x_j) where (i,j) is an edge that is present in the minimal spanning tree of the fully connected graph. Then, the threshold epsilon can be chosen as exp(-dist(x_i, x_j)**2.0/(2*sigma^2)). """ # the number of samples to generate num_samples = 100 # the option necessary for worst_case_blob, try different values #gen_pam = 10 # to understand the meaning of the parameter, read worst_case_blob in generate_data.py # get blob data # X, Y = worst_case_blob(num_samples, gen_pam) X, Y = two_moons(num_samples) """ use the distance function and the min_span_tree function to build the minimal spanning tree min_tree - var: the exponential_euclidean's sigma2 parameter - dists: (n x n) matrix with euclidean distance between all possible couples of points - min_tree: (n x n) indicator matrix for the edges in the minimal spanning tree """ var = 1.0 dists = sd.cdist( X, X, 'euclidean') # dists[i, j] = euclidean distance between x_i and x_j min_tree = min_span_tree(dists) l = [] n1, m1 = min_tree.shape for i in range(n1): for j in range(m1): if min_tree[i][j] == True: l.append([(i, j), dists[i][j]]) l = sorted(l, key=lambda x: x[1], reverse=True) #print(min_tree) """ set threshold epsilon to the max weight in min_tree """ distance_threshold = l[0][1] eps = np.exp(-distance_threshold**2.0 / (2 * var)) """ use the build_similarity_graph function to build the graph W W: (n x n) dimensional matrix representing the adjacency matrix of the graph use plot_graph_matrix to plot the graph """ W = build_similarity_graph(X, var=var, eps=eps, k=k) plot_graph_matrix(X, Y, W) return eps, X, Y, W #if __name__ == '__main__': # for gp in [0,1,10,100]: # print(gp) # how_to_choose_epsilon(gp,0) # for k in [0,1,2,5,10]: # how_to_choose_epsilon(0,k)
distance_threshold = np.max(dists[min_tree]) eps = np.exp(-distance_threshold**2 / (2 * var)) """ use the build_similarity_graph function to build the graph W W: (n x n) dimensional matrix representing the adjacency matrix of the graph use plot_graph_matrix to plot the graph """ W = build_similarity_graph(X, var=var, eps=eps, k=0) plot_graph_matrix(X, Y, W) if __name__ == '__main__': n = 300 blobs_data, blobs_clusters = blobs(n) moons_data, moons_clusters = two_moons(n) point_circle_data, point_circle_clusters = point_and_circle(n) worst_blobs_data, worst_blobs_clusters = worst_case_blob(n, 1.0) var = 1 X, Y = moons_data, moons_clusters n_samples = X.shape[0] dists = pairwise_distances(X).reshape((n_samples, n_samples)) min_tree = min_span_tree(dists) eps = np.exp(-np.max(dists[min_tree])**2 / (2 * var)) W_eps = build_similarity_graph(X, var=var, eps=0.6) W_knn = build_similarity_graph(X, k=15) plot_graph_matrix(X, Y, W_eps) plot_graph_matrix(X, Y, W_knn)