LoginSignup
1
0

More than 5 years have passed since last update.

bincount and at method in numpy

Posted at
print(np.bincount(np.array([0, 1, 1, 3, 2, 1, 7])))
Z = np.ones(10)
l = len(Z)
print(l)
I = np.random.randint(0,l,20)
print(I)
B = np.bincount(I, minlength=20)
print(B)
B = np.bincount(I, minlength=l)
print(B)
X = Z + B
print("====X====")
print(X)

# Another solution
# Author: Bartosz Telenczuk
np.add.at(Z, I, 1)
print(Z)
  • e.g.
[1 3 1 1 0 0 0 1]
10
[6 6 9 5 6 4 5 4 2 8 1 9 0 2 8 7 0 2 9 2]
[2 1 4 0 2 2 3 1 2 3 0 0 0 0 0 0 0 0 0 0]
[2 1 4 0 2 2 3 1 2 3]
====X====
[ 3.  2.  5.  1.  3.  3.  4.  2.  3.  4.]
[ 3.  2.  5.  1.  3.  3.  4.  2.  3.  4.]

Refs.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0