Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
I have this large 1d NumPy array that has the values between [0, 6]. For example, suppose A = [0, 0, 2, 4, 4]

I want to create a array using A: B = [2, 0, 1, 0, 2, 0, 0]

This is because there are 2 0s in the A, 0 1s in A, 1 2s in A, 0 3s in A, 2 4s in A, 0 5s in A, 0 6s in A.

Kindly suggests to me an efficient method is to do this.

1 Answer

0 votes
by (36.8k points)

Try the np.bincount

In [944]: np.bincount(A, minlength=7)

Out[944]: array([2, 0, 1, 0, 2, 0, 0], dtype=int64)

Improve your knowledge in data science from scratch using Data Scientist

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...