If the bar plot is still not providing clear insights due to a large number of unique chunk lengths or extreme outliers, binning the chunk lengths into broader categories can help. By grouping chunk lengths into bins such as "1-100" and "100+", we can simplify the visualization and focus on the overall distribution patterns rather than individual lengths.
Example Code for Binned Bar Plot
python
import matplotlib.pyplot as plt
import pandas as pd
# Create a DataFrame from the chunk_lens dictionary
data = []
for name, lengths in chunk_lens.items():
for l in lengths:
data.append({"Tokenizer": name, "Chunk Length": l})
df = pd.DataFrame(data)
# Define the binning function
def bin_chunk_length(length):
if length <= 100:
return "1-100"
else:
return "100+"
# Apply the binning function to create a new column
df["Chunk Length Bin"] = df["Chunk Length"].apply(bin_chunk_length)
# Group by Tokenizer and Chunk Length Bin, then count the occurrences
grouped_df = df.groupby(["Tokenizer", "Chunk Length Bin"]).size().reset_index(name="Count")
# Pivot the DataFrame to have Tokenizers as columns
pivoted_df = grouped_df.pivot(index="Chunk Length Bin", columns="Tokenizer", values="Count")
# Fill missing values with 0 (in case a tokenizer doesn't have a certain bin)
pivoted_df = pivoted_df.fillna(0)
# Plot the bar chart
pivoted_df.plot(kind="bar", figsize=(10, 6), rot=0)
plt.title("Bar Plot of Binned Chunk Lengths by Tokenizer")
plt.xlabel("Chunk Length Bin")
plt.ylabel("Frequency")
plt.legend(title="Tokenizer")
plt.show()
Interpretation
Simplified Comparison:
By binning the chunk lengths into "1-100" and "100+", the bar plot becomes much simpler, with only two bars per tokenizer. This makes it easier to compare the general behavior of each tokenizer, especially in terms of producing short versus long chunks.
Tokenizer Behavior:
A tokenizer with a tall bar in the "1-100" bin and a short bar in the "100+" bin indicates it predominantly produces shorter chunks.
Conversely, a tokenizer with a tall bar in the "100+" bin suggests it often produces longer chunks.
Outlier Handling:
Binning effectively handles outliers by grouping all chunk lengths greater than 100 into a single category, reducing the visual clutter caused by extreme values.
This binned bar plot provides a clearer and more focused comparison of the chunk length distributions across different tokenizers, making it easier to identify patterns and differences in their segmentation behaviors.