def generateGraph2(self, alpha, dim):
        numVertices = self.graph.getNumVertices()
        self.X = numpy.random.rand(numVertices, dim)

        D = KernelUtils.computeDistanceMatrix(numpy.dot(self.X, self.X.T))
        #P = beta**(-alpha * D)
        """
        Normalise the distance matrix so that the max distance (corner to corner)
        is 1. The value of k is the cumulative probability of an edge for any node
        """

        P = (D / math.sqrt(dim)) ** -alpha
        diagIndices = numpy.array(list(range(0, numVertices)))
        P[(diagIndices, diagIndices)] = numpy.zeros(numVertices)
        P = P / numpy.array([numpy.sum(P, 1)]).T 
        B = numpy.random.rand(numVertices, numVertices) <= P

        #Note that B is symmetric - could just go through e.g. upper triangle
        for i in range(numpy.nonzero(B)[0].shape[0]):
            v1 = numpy.nonzero(B)[0][i]
            v2 = numpy.nonzero(B)[1][i]

            self.graph.addEdge(v1, v2)

        return self.graph
    def generateGraph(self, alpha, p, dim):
        Parameter.checkFloat(alpha, 0.0, float('inf'))
        Parameter.checkFloat(p, 0.0, 1.0)
        Parameter.checkInt(dim, 0, float('inf'))
        
        numVertices = self.graph.getNumVertices()
        self.X = numpy.random.rand(numVertices, dim)

        D = KernelUtils.computeDistanceMatrix(numpy.dot(self.X, self.X.T))
        P = numpy.exp(-alpha * D)
        diagIndices = numpy.array(list(range(0, numVertices)))
        P[(diagIndices, diagIndices)] = numpy.zeros(numVertices)

        B = numpy.random.rand(numVertices, numVertices) <= P 

        #Note that B is symmetric - could just go through e.g. upper triangle 
        for i in range(numpy.nonzero(B)[0].shape[0]):
            v1 = numpy.nonzero(B)[0][i]
            v2 = numpy.nonzero(B)[1][i]
            
            self.graph.addEdge(v1, v2)

        erdosRenyiGenerator = ErdosRenyiGenerator(p)
        self.graph = erdosRenyiGenerator.generate(self.graph, False)

        return self.graph
    def generateGraph2(self, alpha, dim):
        numVertices = self.graph.getNumVertices()
        self.X = numpy.random.rand(numVertices, dim)

        D = KernelUtils.computeDistanceMatrix(numpy.dot(self.X, self.X.T))
        #P = beta**(-alpha * D)
        """
        Normalise the distance matrix so that the max distance (corner to corner)
        is 1. The value of k is the cumulative probability of an edge for any node
        """

        P = (D / math.sqrt(dim))**-alpha
        diagIndices = numpy.array(list(range(0, numVertices)))
        P[(diagIndices, diagIndices)] = numpy.zeros(numVertices)
        P = P / numpy.array([numpy.sum(P, 1)]).T
        B = numpy.random.rand(numVertices, numVertices) <= P

        #Note that B is symmetric - could just go through e.g. upper triangle
        for i in range(numpy.nonzero(B)[0].shape[0]):
            v1 = numpy.nonzero(B)[0][i]
            v2 = numpy.nonzero(B)[1][i]

            self.graph.addEdge(v1, v2)

        return self.graph
    def generateGraph(self, alpha, p, dim):
        Parameter.checkFloat(alpha, 0.0, float('inf'))
        Parameter.checkFloat(p, 0.0, 1.0)
        Parameter.checkInt(dim, 0, float('inf'))

        numVertices = self.graph.getNumVertices()
        self.X = numpy.random.rand(numVertices, dim)

        D = KernelUtils.computeDistanceMatrix(numpy.dot(self.X, self.X.T))
        P = numpy.exp(-alpha * D)
        diagIndices = numpy.array(list(range(0, numVertices)))
        P[(diagIndices, diagIndices)] = numpy.zeros(numVertices)

        B = numpy.random.rand(numVertices, numVertices) <= P

        #Note that B is symmetric - could just go through e.g. upper triangle
        for i in range(numpy.nonzero(B)[0].shape[0]):
            v1 = numpy.nonzero(B)[0][i]
            v2 = numpy.nonzero(B)[1][i]

            self.graph.addEdge(v1, v2)

        erdosRenyiGenerator = ErdosRenyiGenerator(p)
        self.graph = erdosRenyiGenerator.generate(self.graph, False)

        return self.graph
示例#5
0
 def testComputeDistanceMatrix(self):
     numExamples = 100
     numFeatures = 10 
     numRepetitions = 10
     
     for i in range(numRepetitions): 
         X = numpy.random.randn(numExamples, numFeatures)
         K = numpy.dot(X, X.T)
 
         D1 = KernelUtils.computeDistanceMatrix(K)
         D2 = numpy.zeros((numExamples, numExamples))
 
         for i in range(0, numExamples):
             for j in range(0, numExamples):
                 D2[i, j] = numpy.linalg.norm(X[i, :] - X[j, :])
 
         self.assertTrue(numpy.linalg.norm(D1 - D2) < 10**-6)
示例#6
0
    def testComputeDistanceMatrix(self):
        numExamples = 100
        numFeatures = 10
        numRepetitions = 10

        for i in range(numRepetitions):
            X = numpy.random.randn(numExamples, numFeatures)
            K = numpy.dot(X, X.T)

            D1 = KernelUtils.computeDistanceMatrix(K)
            D2 = numpy.zeros((numExamples, numExamples))

            for i in range(0, numExamples):
                for j in range(0, numExamples):
                    D2[i, j] = numpy.linalg.norm(X[i, :] - X[j, :])

            self.assertTrue(numpy.linalg.norm(D1 - D2) < 10**-6)