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?

5
  • 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? Commented Nov 28, 2015 at 4: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. Commented Nov 28, 2015 at 9:14
  • Possible duplicate of stackoverflow.com/q/14070565/1461210 Commented Nov 28, 2015 at 18:24
  • Also: stackoverflow.com/q/28572731/1461210 Commented Nov 28, 2015 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. Commented Nov 28, 2015 at 21:38

1 Answer 1

5

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.