0

I have a 2D NumPy ndarray which consists of densities in a sparse matrix. I would like to plot it as a heatmap while also plotting ellipsoids derived from a couple of Gaussian mixture models fitted to my data. How can I accomplish this in Python?

The array looks something like this:

a = np.array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    1., 0., 0., 0., 0., 0., 1., 1., 2., 1., 2., 1., 1., 1., 0., 0.,
    0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 0., 0., 1., 0., 0.,
    1., 0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0.,
    0., 0., 0., 0., 0.],
   [0., 0., 2., 1., 2., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 1.,
    0., 0., 0., 0., 0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1.,
    1., 1., 1., 0., 0.],
   [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
    0., 0., 0., 0., 0.]])

And to fit the data I use sklearn.mixture, but first I convert the 2D array to a density array:

def convert_to_density_array(array):
    """
    Convert an array to a density array
    """
    density_list = []
    # iterate over each i,j coordinate in the array
    for (i, j), value in np.ndenumerate(array):
        for x in range(int(value)):
            density_list.append((i, j))
    return np.array(density_list)

# Create density array
density_array = convert_to_density_array(a)
gaussian_mix_4_components = mixture.GaussianMixture(n_components=4).fit(density_array)
pietro_molina
  • 97
  • 1
  • 9

0 Answers0