w3resource
NumPy Exercises

NumPy: Compute the histogram of nums against the bins

NumPy Statistics: Exercise-14 with Solution

Write a NumPy program to compute the histogram of nums against the bins.

Sample Solution:-

Python Code:

import numpy as np
import matplotlib.pyplot as plt
nums = np.array([0.5, 0.7, 1.0, 1.2, 1.3, 2.1])
bins = np.array([0, 1, 2, 3])
print("nums: ",nums)
print("bins: ",bins)
print("Result:", np.histogram(nums, bins))
plt.hist(nums, bins=bins)
plt.show()

Sample Output:

nums:  [0.5 0.7 1.  1.2 1.3 2.1]
bins:  [0 1 2 3]
Result: (array([2, 3, 1], dtype=int64), array([0, 1, 2, 3]))
NumPy Statistics: Compute the histogram of nums against the bins

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to count number of occurrences of each value in a given array of non-negative integers.

What is the difficulty level of this exercise?