'How to subtract 1 dimension Numpy arrays with different shapes and plot the difference in seaborn or matplotlib? [duplicate]

Basically I have these arrays

print(img_arr[0].shape)->(returns) (122218,)
print(img_arr1[0].shape)->(returns) (125204,)

so when I did

difference = np.subtract(img_arr[0],img_arr1[0])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9596/1605062018.py in <module>
----> 1 difference = np.subtract(img_arr[0],img_arr1[0])
      2 #print(img_arr[0].shape)
      3 #print(img_arr1[0].shape)

ValueError: operands could not be broadcast together with shapes (122218,) (125204,)

So in order to subtract arrays the shape should be the same. Hence, I did this to find the difference so that I can see the distribution of difference

difference = []
for idx, value in enumerate(img_arr[0]):
    difference.append(img_arr[0][idx] - img_arr1[0][idx])

sns.displot(data=difference,kde=True)

So I just need some guidance that is my above approach valid to find the difference? enter image description here



Solution 1:[1]

What you're currently doing is finding the element-wise difference of each of the 122218 elements in img_arr[0] and the first 122218 of the total 125204 elements in img_arr1[0], and finally plotting a distribution plot of the difference. Your approach is valid, if you are okay with losing the last 2986 (125204 - 122218) elements of img_arr1[0], which are not being used anywhere.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 karthik_ghorpade