def __init__(self, condensed_matrix, **kwargs): """ Constructor. Calculates the eigenvectors given a dataset distance matrix. The eigenvector distances would be the common part for clusterings with different k. @param condensed_matrix: The distance matrix of the dataset. @param sigma_sq: The squared value of sigma for the adjacency matrix calculation. If None, the value will be automatically calculated. @param max_clusters: Maximum number of clusters we will try with this algorithm (for instance with max_clusters = 10 we can try with ks in range [1..10] @param laplacian_calculation_type: The type of calculation. @param store_W: If True the object stores the adjacency matrix. Useful for testing. @param verbose: If True some messages will be printed. """ self.handle_params(kwargs, max_clusters_default=condensed_matrix.row_length - 1) print "Initializing Spectral clustering. This may take some time ..." # self.verbose = True if self.sigma_sq is not None: if self.verbose: print "Calculating W with sigma = %f estimation..." % self.sigma_sq W = SpectralTools.calculate_fully_connected_adjacency_matrix( condensed_matrix, self.sigma_sq) else: if self.verbose: print "Calculating W with sigma estimation..." sigmas = SpectralTools.local_sigma_estimation(condensed_matrix) W = SpectralTools.calculate_fully_connected_adjacency_matrix_with_sigma_estimation( condensed_matrix, sigmas) self.sigma_sq = numpy.mean(sigmas)**2 if self.verbose: print "Sigma^2 estimation (mean of local sigmas): ", self.sigma_sq if self.force_sparse: SpectralTools.force_sparsity(W) if self.store_W: if self.verbose: print "Storing W ..." self.W = numpy.copy(W) if self.verbose: print "Calculating Degree Matrix ..." D = SpectralTools.calculate_degree_matrix(W) if self.verbose: print "Calculating Laplacian ..." L = SpectralTools.calculateUnnormalizedLaplacian(W, D) if self.verbose: print "Calculating Eigenvectors ..." if self.spectral_type == "UNNORMALIZED": v = SpectralTools.calculateUnnormalizedEigenvectors( L, self.max_clusters, self.force_sparse) elif self.spectral_type == "NORMALIZED": v = SpectralTools.calculateNormalizedEigenvectors( L, D, self.max_clusters, self.force_sparse) self.eigenvectors = v # eigenvectors in columns. We need the rows of this matrix for the clustering. if self.verbose: print "Spectral initialization finished."
def __init__(self, condensed_matrix, **kwargs): """ Constructor. Calculates the eigenvectors given a dataset distance matrix. The eigenvector distances would be the common part for clusterings with different k. @param condensed_matrix: The distance matrix of the dataset. @param sigma_sq: The squared value of sigma for the adjacency matrix calculation. If None, the value will be automatically calculated. @param max_clusters: Maximum number of clusters we will try with this algorithm (for instance with max_clusters = 10 we can try with ks in range [1..10] @param laplacian_calculation_type: The type of calculation. @param store_W: If True the object stores the adjacency matrix. Useful for testing. @param verbose: If True some messages will be printed. """ self.handle_params(kwargs, max_clusters_default = condensed_matrix.row_length-1) print "Initializing Spectral clustering. This may take some time ..." # self.verbose = True if self.sigma_sq is not None: if self.verbose: print "Calculating W with sigma = %f estimation..."%self.sigma_sq W = SpectralTools.calculate_fully_connected_adjacency_matrix(condensed_matrix, self.sigma_sq) else: if self.verbose: print "Calculating W with sigma estimation..." sigmas = SpectralTools.local_sigma_estimation(condensed_matrix) W = SpectralTools.calculate_fully_connected_adjacency_matrix_with_sigma_estimation(condensed_matrix, sigmas) self.sigma_sq = numpy.mean(sigmas)**2 if self.verbose: print "Sigma^2 estimation (mean of local sigmas): ", self.sigma_sq if self.force_sparse: SpectralTools.force_sparsity(W) if self.store_W: if self.verbose: print "Storing W ..." self.W = numpy.copy(W) if self.verbose: print "Calculating Degree Matrix ..." D = SpectralTools.calculate_degree_matrix(W) if self.verbose: print "Calculating Laplacian ..." L = SpectralTools.calculateUnnormalizedLaplacian(W, D) if self.verbose: print "Calculating Eigenvectors ..." if self.spectral_type == "UNNORMALIZED": v = SpectralTools.calculateUnnormalizedEigenvectors(L, self.max_clusters, self.force_sparse) elif self.spectral_type == "NORMALIZED": v = SpectralTools.calculateNormalizedEigenvectors(L, D, self.max_clusters, self.force_sparse) self.eigenvectors = v # eigenvectors in columns. We need the rows of this matrix for the clustering. if self.verbose: print "Spectral initialization finished."
def test_calculate_adjacency_matrix(self): data = numpy.array([1., 4., 6., 2., 5., 3., 9., 7., 2., 4., 1., 1., 9.,3., 8.]) matrix = CondensedMatrix(data) expected_W = numpy.array( [ 0.13537598, 0. , 0. , 0.00033545, 0. , 0. , 0. , 0. , 0.00033545, 0. , 0.13537598, 0.13537598, 0. , 0. , 0. ]) W = SpectralTools.calculate_fully_connected_adjacency_matrix(matrix, sigma_sq = 0.5) numpy.testing.assert_almost_equal(W.get_data(),expected_W,3)# float comparison