0
0
ML Pythonml~10 mins

Binning continuous variables in ML Python - Interactive Code Practice

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

Complete the code to create 3 equal-width bins for the data.

ML Python
import pandas as pd

values = [1, 5, 8, 12, 15, 18]
bins = pd.cut(values, [1])
print(bins)
Drag options to blanks, or click blank then click option'
A5
B[0, 10, 20]
C3
Dpd.qcut(values, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list of bin edges instead of the number of bins.
Using pd.qcut instead of pd.cut for equal-width bins.
2fill in blank
medium

Complete the code to create 4 bins with equal number of data points.

ML Python
import pandas as pd

values = [2, 4, 6, 8, 10, 12, 14, 16]
bins = pd.qcut(values, [1])
print(bins)
Drag options to blanks, or click blank then click option'
A3
B4
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using pd.cut instead of pd.qcut for equal-sized bins.
Passing the wrong number of bins.
3fill in blank
hard

Fix the error in the code to create bins with custom edges.

ML Python
import pandas as pd

values = [3, 7, 11, 15, 19]
bins = pd.cut(values, bins=[1])
print(bins)
Drag options to blanks, or click blank then click option'
A[0, 5, 10, 15, 20]
B(0, 5, 10, 15, 20)
C0, 5, 10, 15, 20
Drange(0, 21, 5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for bin edges.
Passing a range object instead of a list.
4fill in blank
hard

Fill both blanks to create bins and label them as 'Low', 'Medium', 'High'.

ML Python
import pandas as pd

values = [2, 5, 8, 11, 14]
bins = pd.cut(values, [1], labels=[2])
print(bins)
Drag options to blanks, or click blank then click option'
A[0, 6, 12, 18]
B[1, 7, 13, 19]
C['Low', 'Medium', 'High']
D['Small', 'Medium', 'Large']
Attempts:
3 left
💡 Hint
Common Mistakes
Using labels that don't match the number of bins.
Using bin edges that don't cover all data points.
5fill in blank
hard

Fill all three blanks to create quantile bins, assign labels, and include the lowest value.

ML Python
import pandas as pd

values = [1, 3, 5, 7, 9, 11]
bins = pd.qcut(values, [1], labels=[2], [3]=True)
print(bins)
Drag options to blanks, or click blank then click option'
A3
B['Low', 'Medium', 'High']
Cinclude_lowest
Dduplicates='drop'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name instead of 'include_lowest'.
Mismatch between number of bins and labels.