Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools Numpy usecase bincount #230

Open
junxnone opened this issue Feb 19, 2021 · 1 comment
Open

Tools Numpy usecase bincount #230

junxnone opened this issue Feb 19, 2021 · 1 comment

Comments

@junxnone
Copy link
Owner

junxnone commented Feb 19, 2021

numpy 统计数组数值

numpy.bincount(x, weights=None, minlength=0)
  • x 需要是一维数组 + 非负整数
  • weights 每个位置的权重,维度同 x
  • minlength 输出最小shape
  • Output 最大值为 80, 输出[0,1,2....,80] 的统计值

上例中 minlength 可以设置 大于 80 的值,小于无效

UseCase

import numpy as np
data = np.array([1,1,1,1,1,1,2,3,5,4,4,4,5,80])
np.bincount(data.astype(np.int8))
array([0, 6, 1, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  • minlength > max value
np.bincount(data.astype(np.int8), minlength=90)
array([0, 6, 1, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
       0, 0])
  • image
np.bincount(img.flatten())

Reference

@junxnone
Copy link
Owner Author

#17

@junxnone junxnone changed the title numpy 统计数组数值 numpy - 统计数组数值 Feb 19, 2021
@junxnone junxnone changed the title numpy - 统计数组数值 numpy usecase bincount Jul 12, 2023
@junxnone junxnone changed the title numpy usecase bincount Tools Numpy usecase bincount Jul 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant