1
import numpy as np
np.random.random((5,5))

array([[ 0.26045197,  0.66184973,  0.79957904,  0.82613958,  0.39644677],
       [ 0.09284838,  0.59098542,  0.13045167,  0.06170584,  0.01265676],
       [ 0.16456109,  0.87820099,  0.79891448,  0.02966868,  0.27810629],
       [ 0.03037986,  0.31481138,  0.06477025,  0.37205248,  0.59648463],
       [ 0.08084797,  0.10305354,  0.72488268,  0.30258304,  0.230913  ]])

I would like to create a 2D density estimate from this 2D array such that similar values imply higher density. Is there a way to do this in numpy?

user308827
  • 21,227
  • 87
  • 254
  • 417
  • When you say a 2D density estimate, it would seem to imply you would be working in a 2-dimensional space? From your matrix it would appear you either want a 1D density estimate, or one in 5 dimensions? – David Maust Nov 28 '15 at 04:20
  • @DavidMaust it's a 2D matrix. What do you mean? User308827: by "similar values imply higher density" --- do you mean, the density is proportional to the difference between values in the matrix? As it stands your question is very unclear. – DilithiumMatrix Nov 28 '15 at 09:14
  • Possible duplicate of http://stackoverflow.com/q/14070565/1461210 – ali_m Nov 28 '15 at 18:24
  • Also: http://stackoverflow.com/q/28572731/1461210 – ali_m Nov 28 '15 at 18:26
  • What I was looking for clarification on is that points in a 2D space have coordinate pairs. (x_1, y_1), (x_2, y_2)... From a 5x5 matrix, either each point should be treated individually as 25 points in a 1D space, or they should be seen as 5 points in a 5D space. I'm not seeing how these points exist in a 2D space. – David Maust Nov 28 '15 at 21:38

1 Answers1

4

I agree, it is indeed not entirely clear what you mean. The numpy.histogram function provides you with the density for an array.

import numpy as np
array = np.random.random((5,5))
print array

density = np.histogram(array, density=True)
print(density)

You can then plot the density, for example with Matplotlib. There is a great discussion on this here: How does numpy.histogram() work?

Community
  • 1
  • 1
Dominix
  • 433
  • 3
  • 9