Пример #1
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."
Пример #3
0
    def test_calculate_degree_matrix_2(self):
        """
        Test provided by Nancy-Sarah Yacovzada.
        """
        
#         0--3
#         |\  \ 
#         5 4  2
#         
#         X = np.array(
#              [[0.0, 0.0, 1.0, 1.0, 1.0],
#               [0.0, 0.0, 1.0, 0.0, 0.0],
#               [1.0, 1.0, 0.0, 0.0, 0.0],
#               [1.0, 0.0, 0.0, 0.0, 0.0],
#               [1.0, 0.0, 0.0, 0.0, 0.0]]
#               )
        data = [0.0, 1.0, 1.0, 1.0,
                     1.0, 0.0, 0.0,
                          0.0, 0.0,
                               0.0]
        
        W = CondensedMatrix(data)
        
        self.assertListEqual([ 3., 1., 2., 1., 1.], SpectralTools.calculate_degree_matrix(W))
Пример #4
0
 def test_calculate_degree_matrix(self):
     W_data = numpy.array([4., 6.,
                               7.])
     expected_D = [10., 11., 13.]
     D = SpectralTools.calculate_degree_matrix(CondensedMatrix(W_data))
     numpy.testing.assert_almost_equal(D, expected_D, 8)