Example #1
0
    def test_eigencalculation(self):
        L = numpy.array([  [1., 0., 0.,  0.],
                          [ 0,  1.,  0., -4.],
                          [ 0,  0.,  1., 0.],
                          [ 0, -4.,  0.,  1.]])
        max_clusters = 2
        v  = SpectralTools.calculateUnnormalizedEigenvectors(L, max_clusters, False)

        L = numpy.array([  [1., 0., 0.,  0.],
                          [ 0,  1.,  0., -4.],
                          [ 0,  0.,  1., 0.],
                          [ 0, -4.,  0.,  1.]])
        v  = SpectralTools.calculateUnnormalizedEigenvectors(L, max_clusters, True)

        D = [1.,2.,3.,4.]

        L = numpy.array([  [1., 0., 0.,  0.],
                          [ 0,  1.,  0., -4.],
                          [ 0,  0.,  1., 0.],
                          [ 0, -4.,  0.,  1.]])
        v  = SpectralTools.calculateNormalizedEigenvectors(L, D, max_clusters, False)

        L = numpy.array([  [1., 0., 0.,  0.],
                          [ 0,  1.,  0., -4.],
                          [ 0,  0.,  1., 0.],
                          [ 0, -4.,  0.,  1.]])
        v  = SpectralTools.calculateNormalizedEigenvectors(L, D, max_clusters, True)
        self.fail("CHECK EIGENCALCULATIONS AGAIN, VALUES FOR THIS METHODS ARE DIFFERENT")
Example #2
0
    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."