Search code examples
pythonnumpyscipykernel-densityargmax

Python - Get the coordinates of densest point


I'm using numpy and scipy to generate a density plot from 3D coordinate information. I can generate a density plot of the data successfully by generating a KDE with the following code

xyz = np.vstack([x,y,z])
kde = stats.gaussian_kde(xyz)
density = kde(xyz)

But how can I use this information to find the coordinates that associate with the 3D point of greatest density?

I've tried

max(density)

which returns a value that I can then find the index of with

density.argmax(axis=0)

but then I hit a blank as I can't seem to use that index to grab the associated coordinates from xyz and I'm unsure if this is the right approach.


Solution

  • From here, I can use

    xyz.T[np.argmax(density)]
    

    to return the 3D coordinates of the densest point in my data