Numpy 的 bincount(~)
方法計算給定值數組的 bin 計數(即落在某個區間內的值的數量)。默認情況下,每個 bin 的間隔大小為 1,因此 bin 的數量取決於輸入值的範圍。
參數
1. a
| array-like
輸入數組。
2. weights
| array-like
| optional
包含每個輸入值的權重的數組。如果某個值落在特定的 bin 中,我們不會將計數增加 1,而是增加相應的權重。形狀必須與 a
相同。默認情況下,權重均為一。
3. minlength
| int
| optional
最小箱數。
返回值
包含 bin 計數的 Numpy 數組。
例子
基本用法
my_hist = np.bincount([1,3,6,6,10])
my_hist
array([0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 1])
指定權重
my_hist = np.bincount([1,3,6,6,10], weights=[2,3,4,5,6])
my_hist
array([0., 2., 0., 3., 0., 0., 9., 0., 0., 0., 6.])
我們得到 9 的原因是輸入數組中的兩個 6 顯然屬於同一個 bin,並且由於它們各自的權重是 4 和 5,所以我們最終得到的總 bin 計數為 4+5=9
。
指定最小長度
my_hist = np.bincount([1,3,6,6,10], minlength=20)
my_hist
array([0, 1, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
請注意,bin 大小仍為 1 - 最小長度隻是擴大了全局範圍。
相關用法
- Python binascii.crc32用法及代碼示例
- Python binary轉string用法及代碼示例
- Python binary轉ASCII用法及代碼示例
- Python bin用法及代碼示例
- Python binascii.b2a_hex用法及代碼示例
- Python bin()用法及代碼示例
- Python bin方法用法及代碼示例
- Python NumPy busdaycalendar對象用法及代碼示例
- Python bytes.isupper用法及代碼示例
- Python base64.b64decode()用法及代碼示例
- Python bokeh.plotting.figure.asterisk()用法及代碼示例
- Python bytes.zfill用法及代碼示例
- Python base64.b32decode()用法及代碼示例
- Python bytes.expandtabs用法及代碼示例
- Python bytearray()用法及代碼示例
- Python bokeh.plotting.figure.annular_wedge()用法及代碼示例
- Python NumPy base屬性用法及代碼示例
- Python Pandas bdate_range方法用法及代碼示例
- Python bytes.isalpha用法及代碼示例
- Python base64.b85encode()用法及代碼示例
- Python base64.b64encode()用法及代碼示例
- Python bokeh.plotting.figure.circle()用法及代碼示例
- Python bytes.hex用法及代碼示例
- Python bool()用法及代碼示例
- Python bz2.decompress(s)用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | bincount method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。