0
0
Matplotlibdata~10 mins

Bin count and bin edges in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the number of bins for the histogram.

Matplotlib
import numpy as np

data = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6])
bin_count = len(np.histogram_bin_edges(data, bins=[1])) - 1
print(bin_count)
Drag options to blanks, or click blank then click option'
A'auto'
B5
C'sqrt'
D'sturges'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer directly as bins when expecting a method string.
Confusing bin count with number of edges.
2fill in blank
medium

Complete the code to get the bin edges for the data using 4 bins.

Matplotlib
import numpy as np

data = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6])
bin_edges = np.histogram_bin_edges(data, bins=[1])
print(bin_edges)
Drag options to blanks, or click blank then click option'
A4
B3
C5
D'auto'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for fixed bin count.
Confusing bin count with number of edges.
3fill in blank
hard

Fix the error in the code to correctly calculate bin edges for data with 3 bins.

Matplotlib
import numpy as np

data = np.array([10, 20, 30, 40, 50])
bin_edges = np.histogram_bin_edges(data, bins=[1])
print(bin_edges)
Drag options to blanks, or click blank then click option'
A'auto'
B'3'
C3
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the number of bins as a string instead of an integer.
Passing None which causes an error.
4fill in blank
hard

Fill both blanks to create a dictionary with bin edges as keys and counts as values.

Matplotlib
import numpy as np

data = np.array([1, 2, 2, 3, 4, 5, 5, 5, 6])
counts, edges = np.histogram(data, bins=[1])
bin_dict = {edges[i]: counts[i] for i in [2]
print(bin_dict)
Drag options to blanks, or click blank then click option'
A4
Brange(len(counts))
Crange(len(edges) - 1)
Drange(len(edges))
Attempts:
3 left
💡 Hint
Common Mistakes
Using the full length of edges which is one more than counts.
Using a string instead of an integer for bins.
5fill in blank
hard

Fill all three blanks to create a dictionary with bin edges as keys and counts as values, using 5 bins.

Matplotlib
import numpy as np

data = np.array([2, 4, 6, 8, 10, 12, 14])
counts, edges = np.histogram(data, bins=[1])
bin_dict = {edges[i]: counts[i] for i in [2] if counts[i] [3] 0}
print(bin_dict)
Drag options to blanks, or click blank then click option'
A5
Brange(len(counts))
C>
Drange(len(edges))
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong range length for iteration.
Using a comparison operator other than '>'.