<jupyter_start><jupyter_text>Arbitrary interval width discretization with pandasFirst we need to determine the interval edges or limits.<jupyter_code># let's plot a histogram to understand the value range of 
#the variable LSTAT

data['LSTAT'].hist(bins=30)
# bin boundaries - we will use with pandas cut()
intervals = [0, 10, 20, 30, np.Inf]

# bin labels - we will use with pandas cut()
labels = ['0-10', '10-20', '20-30', '>30']
# discretize LSTAT

# create a discretized version of the variable where the values are the
# strings we created in the previous cell
data['lstat_labels'] = pd.cut(
    data['LSTAT'], bins=intervals, labels=labels, include_lowest=True)

# let's create a version of the discretized variable were the values are
# the interval limits
data['lstat_intervals'] = pd.cut(
    data['LSTAT'], bins=intervals, labels=None, include_lowest=True)

# plot the original and 2 versions of the discretized variable
print(data[['LSTAT', 'lstat_labels', 'lstat_intervals']].head())
# let's check the number of observations per bin

data['lstat_intervals'].value_counts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11220825852782765, 'fraction_numerical': 0.02064631956912029, 'mean_word_length': 5.636904761904762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '291843', 'license': 'MIT', 'n_tokens_mistral': 366, 'n_tokens_neox': 341, 'n_words': 126, 'path': 'Chapter05/Recipe-4-Arbitrary-interval-discretisation.ipynb', 'repo_name': 'paulorobertolds/Python-Feature-Engineering-Cookbook', 'size': '1114'}