コード例 #1
0
def test_gaussian_kde():

    pdf = MoG(a=[0.3, 0.7], ms=[[-1.0, 0.0], [1.0, 0.0]], Ss=[np.diag([0.1, 1.1]), np.diag([1.1, 0.1])])
    xs = pdf.gen(5000)
    kde = gaussian_kde(xs)

    import util.plot

    lims = [-4.0, 4.0]
    util.plot.plot_pdf_marginals(pdf, lims=lims).suptitle('original')
    util.plot.plot_hist_marginals(xs, lims=lims).suptitle('samples')
    util.plot.plot_pdf_marginals(kde, lims=lims).suptitle('KDE')
    util.plot.plt.show()
コード例 #2
0
    def compute_stat(self, img_array, data_type, file_type, k):
        img_array = np.array(img_array)
        pca = PCA(n_components=self.reduced_dimensions,
                  svd_solver='randomized').fit(img_array)
        pca_array = pca.transform(img_array)
        if data_type == 'positive':
            model = MoG(pca_array, k)
            model.exp_max(tolerance=1e-18)
            for i in range(0, k):
                mu = pca.inverse_transform(model.mu[i])
                if (file_type == "rgb"):
                    mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
                elif file_type == "hsv":
                    mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
                    mu = cv2.cvtColor(mu, cv2.COLOR_HSV2BGR)
                else:
                    mu = np.round(mu.reshape((100, 100, 1))).astype(np.uint8)


#                print(np.max(mu))
                print(model.theta[i])
                cv2.imshow('mean', mu)
                cv2.waitKey(0)
                cv2.destroyAllWindows()
                #                    print(self.output_image_dir+file_type+"+"_mog_"+repr(i)
                cv2.imwrite(
                    self.output_image_dir + "/" + file_type + "_mog_" +
                    repr(i + 1) + ".jpg", mu)
            np.save(self.output_numpy_dir + "/" + file_type + "_mu", model.mu)
            np.save(self.output_numpy_dir + "/" + file_type + "_cov",
                    model.cov)
            np.save(self.output_numpy_dir + "/" + file_type + "_theta",
                    model.theta)
        else:
            mu = np.mean(img_array, axis=0)
            if (file_type == "rgb"):
                mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
            elif file_type == "hsv":
                mu = np.round(mu.reshape((100, 100, 3))).astype(np.uint8)
                mu = cv2.cvtColor(mu, cv2.COLOR_HSV2BGR)
            else:
                mu = np.round(mu.reshape((100, 100, 1))).astype(np.uint8)
            cv2.imwrite(
                self.output_image_dir + "/" + file_type + "_mog_neg.jpg", mu)
            pca_mu = np.mean(pca_array, axis=0)
            cov = np.cov(pca_array.T)
            np.save(self.output_numpy_dir + "/" + file_type + "_mu_neg",
                    pca_mu)
            np.save(self.output_numpy_dir + "/" + file_type + "_cov_neg", cov)
コード例 #3
0
def gaussian_kde(xs, ws=None, std=None):
    """
    Returns a mixture of gaussians representing a kernel density estimate.
    :param xs: rows are datapoints
    :param ws: weights, optional
    :param std: the std of the kernel, if None then a default is used
    :return: a MoG object
    """

    xs = np.array(xs)
    assert xs.ndim == 2, 'wrong shape'

    n_data, n_dims = xs.shape
    ws = np.full(n_data, 1.0 / n_data) if ws is None else np.asarray(ws)
    var = n_data ** (-2.0 / (n_dims + 4)) if std is None else std ** 2

    return MoG(a=ws, ms=xs, Ss=[var * np.eye(n_dims) for _ in xrange(n_data)])