I have a python matrix
leafs = np.array([[1,2,3],[1,2,4],[2,3,4],[4,2,1]])
I would like to compute for each couple of rows the number of time they have the same element.
In this case I would get a 4x4 matrix proximity
proximity = array([[3, 2, 0, 1],
[2, 3, 1, 1],
[0, 1, 3, 0],
[1, 1, 0, 3]])
This is the code that I am currently using.
proximity = []
for i in range(n):
print(i)
proximity.append(np.apply_along_axis(lambda x: sum(x==leafs[i, :]), axis=1,
arr=leafs))
I need a faster solution
EDIT: The accepted solution does not work in this example
>>> type(f.leafs)
<class 'numpy.ndarray'>
>>> f.leafs.shape
(7210, 1000)
>>> f.leafs.dtype
dtype('int64')
>>> f.leafs.reshape(7210, 1, 1000) == f.leafs.reshape(1, 7210, 1000)
False
>>> f.leafs
array([[ 19, 32, 16, ..., 143, 194, 157],
[ 19, 32, 16, ..., 143, 194, 157],
[ 19, 32, 16, ..., 143, 194, 157],
...,
[139, 32, 16, ..., 5, 194, 157],
[170, 32, 16, ..., 5, 194, 157],
[170, 32, 16, ..., 5, 194, 157]])
>>>