Let say I have two numPy arrays arr1and arr2:
arr1 = np.random.randint(3, size = 100)
arr2 = np.random.randint(3, size = 100)
I would like to build a matrix that contains the number of joint occurrences.
In other words, for all the values of arr1 that are 0, find the elements in arr2 that are also 0 and are located at the same position. And so, I would like to get the following matrix:
M = [[p(0,0), p(0,1), p(0,2)],
[p(1,0), p(1,1), p(1,2)],
[p(2,0), p(2,1), p(2,2)]]
Where p(0,0)stands for the number of occurrences that are 0 on arr1and 0 on arr2.
First Attempt:
As a first attempt I have tried the following:
[[sum(arr1[arr2 == y] == x) for x in np.arange(0,3)] for y in np.arange(0,3)]
But python throws the following error:
NameError: name 'arr1' is not defined
Second Attempt:
I tried to dig into this error by making use of for-loops:
M = np.array([])
for x in np.arange(0,dim):
result = np.array([])
for y in np.arange(0,dim):
result_temp = sum(arr1[arr2 == x] == y)
result = np.append(result, result_temp)
M = np.append(M,result)
In this case Python does not throw the previous Error, but instead of getting a 3x3 array, I get a 1x9 array, and I am not able to get the desired 3x3 array.
Thanks in advance.