Example #1
0
    def initialisation(self):
        """
                        This is the initialisation of the parameters of the gaussians
                        On the choice of using the Pycluster library see http://stackoverflow.com/questions/1545606/python-k-means-algorithm
        """
        labels, errors, found = knn(self.data, self.number_of_gaussians)
        # grouped data example
        # 0th element of grouped data represents the 0th cluster and consists
        # of the frames belonging to that cluster
        group_data = [[] for i in xrange(self.number_of_gaussians)]
        for index, label in enumerate(labels):
            group_data[label].append(self.data[index].tolist())

        total_weight = sum(map(lambda x: len(x), group_data))

        dimension = (self.data.shape)[1]
        for everyMatrix in group_data:
            everyMatrix = np.array(everyMatrix)
            mean_vector = mean(everyMatrix)
            covariance_matrix = covariance(everyMatrix)
            gaussian = MultiVariateGaussian(mean_vector, covariance_matrix,
                                            dimension)
            weight = float(len(everyMatrix)) / total_weight
            self.gaussians.append({'gaussian': gaussian, 'weight': weight})
Example #2
0
    def initialisation(self):
        """
                        This is the initialisation of the parameters of the gaussians
                        On the choice of using the Pycluster library see http://stackoverflow.com/questions/1545606/python-k-means-algorithm
        """
        labels, errors, found = knn(self.data, self.number_of_gaussians)
        # grouped data example
        # 0th element of grouped data represents the 0th cluster and consists
        # of the frames belonging to that cluster
        group_data = [[] for i in xrange(self.number_of_gaussians)]
        for index, label in enumerate(labels):
            group_data[label].append(self.data[index].tolist())

        total_weight = sum(map(lambda x: len(x), group_data))

        dimension = (self.data.shape)[1]
        for everyMatrix in group_data:
            everyMatrix = np.array(everyMatrix)
            mean_vector = mean(everyMatrix)
            covariance_matrix = covariance(everyMatrix)
            gaussian = MultiVariateGaussian(
                mean_vector, covariance_matrix, dimension)
            weight = float(len(everyMatrix)) / total_weight
            self.gaussians.append({'gaussian': gaussian, 'weight': weight})
Example #3
0
    def getGeneralisedSampleVariance(self):
        return self.generalisedSampleVariance

    def setGeneralisedSampleVariance(self):
        self.generalisedSampleVariance = np.linalg.det(
            self.getCovarianceMatrix())

    def getPrecisionMatrix(self):
        return self.precision_matrix

    def setPrecisionMatrix(self):
        self.precision_matrix = np.fabs(
            np.linalg.inv(self.getCovarianceMatrix()))

    def probabilityDensityFunction(self, data):
        variation_from_mean = data - self.mean_vector
        self.MahalanobisDistance = (
            (variation_from_mean) * self.precision_matrix) * (variation_from_mean.T)
        self.denominator = (
            (2 * np.pi) ** (self.number_of_random_variables / 2)) * (self.generalisedSampleVariance ** 0.5)
        return (np.exp((self.MahalanobisDistance * (-0.5))) / self.denominator).item(0)

if __name__ == '__main__':
    data = test.get_test_data()
    mean = mean(data)
    covariance = covariance(data)
    print "covariance_matrix", covariance
    gmm_cluster = MultiVariateGaussian(mean, covariance, 2)
    print gmm_cluster.probabilityDensityFunction(np.matrix([3.0, 4.0]))