2015-11-28 25 views
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 ]]) 

我想從此二維數組創建二維密度估計值,以使類似的值意味着較高的密度。有沒有辦法在numpy中做到這一點?在numpy中創建密度估計

+0

當你說二維密度估計,它似乎意味着你將工作在二維空間?從你的矩陣中,你會看到你想要一維密度估計,還是一維5維? –

+0

@DavidMaust它是一個2D矩陣。你什麼意思? User308827:「類似的值意味着更高的密度」---你的意思是,密度與矩陣中的值之間的差異成正比?現在你的問題很不明確。 – DilithiumMatrix

+0

可能的重複http://stackoverflow.com/q/14070565/1461210 –

回答

1

我同意,這確實不完全清楚你的意思。 numpy.histogram函數爲您提供數組的密度。

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

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

然後,您可以繪製密度,例如用Matplotlib。 這裏有一個很好的討論:How does numpy.histogram() work?