numpy 高階函數 —— np.histogram


  • np.diff(a, n=1, axis=-1):n 表示差分的階數;

    >> x = np.array([1, 2, 4, 7, 0])
    >> np.diff(x)
    array([ 1, 2, 3, -7]) >> np.diff(x, n=2) array([ 1, 1, -10])

1. np.histogram

官方文檔:numpy.histogram — NumPy v1.12 Manual

  • numpy.histogram(a, bins=10, range=None, normed=False, weights=None, density=None)
    • 返回值,有兩個,
      • hist : array
      • bin_edges : array of dtype float,bin edges 的長度要是 hist 的長度加1,bin edges (length(hist)+1),也即 (bin_edges[0], bin_edges[1]) ⇒ hist[0],….,(bin_edges[-2], bin_edges[-1]) ⇒ hist[-1],bin_edges 參數與輸入參數的 bins 保持一致;
>>> a = np.arange(5)
>>> hist, bin_edges = np.histogram(a, density=True)
>>> hist
array([ 0.5,  0. ,  0.5,  0. ,  0. ,  0.5,  0. ,  0.5,  0. ,  0.5])
>>> hist.sum()
2.4999999999999996

>>> np.sum(hist*np.diff(bin_edges))
1.0
                            # hist*np.diff(bin_edges) ⇒ 其實表示一種概率分布;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM