Outliers can indeed skew the perception of the distribution. A joint histogram (or a 2D histogram) can be a great way to visualize the relationship between two variables, such as the chunk lengths produced by two different tokenizers. This can help identify patterns or correlations that might be obscured by outliers in individual distributions.
Example Code for Joint Histogram
import matplotlib.pyplot as plt
import numpy as np
# Select two tokenizers for comparison
tokenizer1 = list(chunk_lens.keys())[0]
tokenizer2 = list(chunk_lens.keys())[1]
# Get the chunk lengths for the selected tokenizers
lengths1 = chunk_lens[tokenizer1]
lengths2 = chunk_lens[tokenizer2]
# Ensure both lists are of the same length for comparison
min_len = min(len(lengths1), len(lengths2))
lengths1 = lengths1[:min_len]
lengths2 = lengths2[:min_len]
# Create a 2D histogram
plt.figure(figsize=(10, 8))
plt.hist2d(lengths1, lengths2, bins=50, cmap='hot', cmin=1)
plt.colorbar(label='Frequency')
plt.title(f'Joint Histogram of Chunk Lengths: {tokenizer1} vs {tokenizer2}')
plt.xlabel(f'{tokenizer1} Chunk Length')
plt.ylabel(f'{tokenizer2} Chunk Length')
plt.show()
# Optional: Add a regression line to highlight the relationship
import numpy as np
from sklearn.linear_model import LinearRegression
# Convert lists to numpy arrays
X = np.array(lengths1).reshape((-1, 1))
y = np.array(lengths2)
# Fit a linear regression model
model = LinearRegression().fit(X, y)
# Predict y values
y_pred = model.predict(X)
# Plot the regression line on the joint histogram
plt.figure(figsize=(10, 8))
plt.hist2d(lengths1, lengths2, bins=50, cmap='hot', cmin=1)
plt.colorbar(label='Frequency')
plt.plot(X, y_pred, color='red', linewidth=2)
plt.title(f'Joint Histogram with Regression Line: {tokenizer1} vs {tokenizer2}')
plt.xlabel(f'{tokenizer1} Chunk Length')
plt.ylabel(f'{tokenizer2} Chunk Length')
plt.show()
Interpretation
-
Correlation:
If the points in the joint histogram tend to cluster around a line (either the regression line or a visible trend), it suggests a correlation between the chunk lengths produced by the two tokenizers. A strong correlation might indicate that both tokenizers are segmenting the text in a similar manner.
-
Scatter:
A wide scatter of points indicates little to no correlation, suggesting that the tokenizers produce chunk lengths independently of each other.
-
Outliers:
While outliers can still be present, the joint histogram can help contextualize them by showing how they relate to the other tokenizer’s chunk lengths.
-
Density:
Areas of high density in the histogram indicate common combinations of chunk lengths between the two tokenizers.
By examining the joint histogram, you can gain insights into how different tokenizers compare in terms of chunk length distribution, which can inform your choice of tokenizer for specific tasks or datasets.