The subtraction (sample_Oklab[np.newaxis, :, :] - color_set[:, np.newaxis, :]) results in a shape of (2, 16, 3) because NumPy broadcasts the arrays to align the axes.
Calculating Distances:
np.sum(..., axis=2) sums over the last dimension (the color channels), resulting in a shape of (2, 16).
np.sqrt(...) maintains the shape as (2, 16).
What You Want
You mentioned that you want the output to have a shape of (16, 2, 3). This suggests that you aim to have per-component differences between each sample and each color set, rather than a single distance value.
Solution
To achieve an output shape of (16, 2, 3), you should:
Adjust the Axis Alignment:
Swap the placement of the np.newaxis so that sample_Oklab is along the first dimension and color_set is along the second dimension. This aligns the samples and color sets correctly for broadcasting.
Remove the Summation and Square Root:
Since you want the per-component differences (i.e., the difference in each of the 3 color channels), you should avoid summing over the color channels and taking the square root. These operations reduce the dimensionality, which isn't desired in this case.
Corrected Code
Here's how you can modify your code to achieve the desired output shape:
python
import numpy as np
# Assuming sample_Oklab is of shape (16, 3)
# and color_set is of shape (2, 3)
# Compute per-component differences with shape (16, 2, 3)
differences = sample_Oklab[:, np.newaxis, :] - color_set[np.newaxis, :, :]
print(differences.shape) # Output: (16, 2, 3)
Explanation of the Corrected Code
Adding New Axes Correctly:
sample_Oklab[:, np.newaxis, :] reshapes sample_Oklab to (16, 1, 3).
color_set[np.newaxis, :, :] reshapes color_set to (1, 2, 3).
Broadcasting Subtraction:
The subtraction sample_Oklab[:, np.newaxis, :] - color_set[np.newaxis, :, :] broadcasts the arrays to (16, 2, 3), resulting in the desired shape.
Result:
differences now contains the per-component differences between each of the 16 samples and each of the 2 color sets.
If You Intend to Compute Distances Instead
If your actual intention was to compute the Euclidean distances between each sample and each color set, resulting in a shape of (16, 2), you should adjust both the axis alignment and keep the summation and square root: