Bird
Raised Fist0
NumPydata~20 mins

Histogram computation with np.histogram() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Histogram Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.histogram() with bins=6
What is the output of the following code snippet?
NumPy
import numpy as np

arr = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6])
hist, bins = np.histogram(arr, bins=6)
print(hist, bins)
A[1 2 1 0 3 2] [1. 2. 3. 4. 5. 6. ]
B[1 2 1 1 3 1] [1. 2. 3. 4. 5. 6. 7. ]
C[1 2 1 1 3 2] [1. 1.83333333 2.66666667 3.5 4.33333333 5.16666667 6. ]
D[1 2 1 1 3 2] [1. 2. 3. 4. 5. 6. 7. ]
Attempts:
2 left
💡 Hint
Check how np.histogram divides the range into equal-width bins when bins=6.
❓ data_output
intermediate
2:00remaining
Number of bins and counts with custom bins
Given the array and bins, what is the output counts array from np.histogram?
NumPy
import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
bins = [0, 3, 6, 9]
hist, _ = np.histogram(arr, bins=bins)
print(hist)
A[3 4 3]
B[3 3 4]
C[4 3 3]
D[3 3 3]
Attempts:
2 left
💡 Hint
Count how many numbers fall into each bin range: [0-3), [3-6), [6-9).
🔧 Debug
advanced
2:00remaining
Identify the error in np.histogram bin specification
What error does the following code raise?
NumPy
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
bins = [5, 4, 3, 2, 1]
hist, edges = np.histogram(arr, bins=bins)
AValueError: bins must increase monotonically.
BTypeError: bins must be an integer or sequence.
CIndexError: list index out of range.
DNo error, runs successfully.
Attempts:
2 left
💡 Hint
Bins must be sorted in ascending order.
❓ visualization
advanced
2:00remaining
Histogram bin edges with uneven bin widths
What are the bin edges and counts for this histogram?
NumPy
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
bins = [1, 3, 6, 10]
hist, edges = np.histogram(arr, bins=bins)
print(hist, edges)
A[2 3 4] [ 1 3 6 10]
B[2 4 3] [ 1 3 6 10]
C[3 3 3] [ 1 3 6 10]
D[2 3 3] [ 1 3 6 10]
Attempts:
2 left
💡 Hint
Count how many values fall into each bin: [1-3), [3-6), [6-10).
🚀 Application
expert
3:00remaining
Calculate histogram and cumulative counts
Given the array, which option correctly computes the histogram counts and cumulative counts using np.histogram?
NumPy
import numpy as np

arr = np.array([2, 4, 4, 4, 5, 5, 7, 9])
bins = [0, 3, 6, 9, 12]
hist, edges = np.histogram(arr, bins=bins)
cum_counts = np.cumsum(hist)
print(hist, cum_counts)
A[1 4 2 1] [1 5 7 8]
B[1 5 1 1] [1 6 7 8]
C[2 4 1 1] [2 6 7 8]
D[1 5 2 0] [1 6 8 8]
Attempts:
2 left
💡 Hint
Count values in each bin and then sum counts cumulatively.

Practice

(1/5)
1. What does the np.histogram() function in NumPy do?
easy
A. It creates a line plot of the data points.
B. It sorts the data points in ascending order.
C. It counts how many data points fall into specified bins and returns counts and bin edges.
D. It calculates the mean and median of the data points.

Solution

  1. Step 1: Understand the purpose of np.histogram()

    The function divides data into bins and counts how many points fall into each bin.
  2. Step 2: Identify the output of np.histogram()

    It returns two arrays: one with counts per bin and one with the bin edges.
  3. Final Answer:

    It counts how many data points fall into specified bins and returns counts and bin edges. -> Option C
  4. Quick Check:

    np.histogram() = counts and edges [OK]
Hint: np.histogram() returns counts and bin edges separately [OK]
Common Mistakes:
  • Thinking np.histogram() sorts data
  • Confusing histogram with mean calculation
  • Expecting a plot from np.histogram()
2. Which of the following is the correct way to call np.histogram() to create 4 bins for data array data?
easy
A. np.histogram(data, bins=4)
B. np.histogram(4, data)
C. np.histogram(bins=data, 4)
D. np.histogram(data, bins=[4])

Solution

  1. Step 1: Check the correct parameter usage for bins

    The bins parameter can be passed as a keyword argument like bins=4.
  2. Step 2: Identify the correct syntax

    np.histogram(data, bins=4) is the standard and clear way to specify 4 bins.
  3. Final Answer:

    np.histogram(data, bins=4) -> Option A
  4. Quick Check:

    Use bins=4 to specify number of bins [OK]
Hint: Use bins=number to set bin count in np.histogram() [OK]
Common Mistakes:
  • Passing bins as a list with one number
  • Swapping argument order incorrectly
  • Using positional argument without data first
3. What is the output of this code?
import numpy as np
data = np.array([1, 2, 2, 3, 4, 5])
counts, bins = np.histogram(data, bins=[1, 3, 5])
print(counts)
medium
A. [2 4]
B. [3 3]
C. [1 5]
D. [3 2]

Solution

  1. Step 1: Understand bins and data distribution

    Bins are [1, 3) and [3, 5). Count how many data points fall into each bin.
  2. Step 2: Count data points in each bin

    Bin [1,3) includes 1, 2, 2 -> 3 points; Bin [3,5) includes 3, 4 -> 2 points. Note that 5 equals the rightmost bin edge and is included in the last bin.
  3. Final Answer:

    [3 3] -> Option B
  4. Quick Check:

    Counts per bin = [3, 3] [OK]
Hint: Count points in each bin range carefully [OK]
Common Mistakes:
  • Including right edge in left bin
  • Miscounting repeated values
  • Confusing bin edges with counts
4. The following code throws an error. What is the problem?
import numpy as np
data = [1, 2, 3, 4]
counts, bins = np.histogram(data, bins='five')
medium
A. The counts and bins cannot be unpacked together.
B. The data must be a NumPy array, not a list.
C. np.histogram() requires at least 5 data points.
D. The bins parameter must be an integer or sequence, not a string.

Solution

  1. Step 1: Check the bins parameter type

    bins='five' is a string, but bins must be an int or a sequence of numbers.
  2. Step 2: Understand np.histogram() input requirements

    Data can be list or array, and unpacking counts and bins is valid.
  3. Final Answer:

    The bins parameter must be an integer or sequence, not a string. -> Option D
  4. Quick Check:

    bins must be int or sequence, not string [OK]
Hint: Bins must be int or list/array, not text [OK]
Common Mistakes:
  • Thinking data must be np.array
  • Assuming unpacking counts and bins is invalid
  • Believing minimum data length is required
5. You have data points [0, 1, 2, 3, 4, 5, 6]. You want to create a histogram with bins of width 2 starting from 0 up to 6 (inclusive). Which bins parameter should you pass to np.histogram() to get correct bin counts?
hard
A. [0, 2, 4, 6, 8]
B. [0, 2, 4, 6]
C. 3
D. [0, 1, 2, 3, 4, 5, 6]

Solution

  1. Step 1: Determine bin edges for width 2 from 0 to 6 inclusive

    Bins should cover ranges: 0-2, 2-4, 4-6, and include 6, so edges are [0, 2, 4, 6, 8].
  2. Step 2: Understand np.histogram bin edge behavior

    Rightmost edge is inclusive for the last bin, so to include 6, bin edges must go beyond 6.
  3. Final Answer:

    [0, 2, 4, 6, 8] -> Option A
  4. Quick Check:

    Bin edges must cover all data points [OK]
Hint: Add one extra bin edge beyond max data to include last point [OK]
Common Mistakes:
  • Using bins that exclude the max value
  • Using number of bins instead of edges
  • Using bin edges with wrong intervals
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard