numpy 版本 1.9.0 的更新。user545424 的回答在 1.9.0 中不起作用。这有效:
>>> import numpy as np
>>> arr = np.random.randint(0,10,100)
>>> hist, bin_edges = np.histogram(arr, density=True)
>>> hist = array([ 0.16666667, 0.15555556, 0.15555556, 0.05555556, 0.08888889,
0.08888889, 0.07777778, 0.04444444, 0.18888889, 0.08888889])
>>> hist
array([ 0.1 , 0.11111111, 0.11111111, 0.08888889, 0.08888889,
0.15555556, 0.11111111, 0.13333333, 0.1 , 0.11111111])
>>> bin_edges
array([ 0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ])
>>> np.diff(bin_edges)
array([ 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9])
>>> np.diff(bin_edges)*hist
array([ 0.09, 0.1 , 0.1 , 0.08, 0.08, 0.14, 0.1 , 0.12, 0.09, 0.1 ])
>>> cdf = np.cumsum(hist*np.diff(bin_edges))
>>> cdf
array([ 0.15, 0.29, 0.43, 0.48, 0.56, 0.64, 0.71, 0.75, 0.92, 1. ])
>>>