Write a NumPy program to compute the histogram of a set of data. Return the histogram.
Example 1:
Input: np.array([1, 2, 1, 3, 2, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]), 10 Output: (array([6, 5, 2, 1, 1, 1, 1, 1, 1, 1]), array([1. , 1.8, 2.6, 3.4, 4.2, 5. , 5.8, 6.6, 7.4, 8.2, 9. ]))
Example 2:
Input: np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]), 10 Output: (array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), array([ 10., 19., 28., 37., 46., 55., 64., 73., 82., 91., 100.]))
You can use np.histogram() function to compute the histogram of a set of data.
import numpy as np def compute_histogram(array, bins): return np.histogram(array, bins) print(compute_histogram(np.array([1, 2, 1, 3, 2, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]), 10)) print(compute_histogram(np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]), 10))
Unlock AI & Data Science treasures. Log in!