Skip to content

Tools Numpy usecase bincount #230

@junxnone

Description

@junxnone

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions