def common_subarrays(X, axis=<NUM_LIT:0>):
from collections import defaultdict<EOL>from itertools import count<EOL>from operator import iadd<EOL>assert X.ndim == <NUM_LIT:2> and axis in (<NUM_LIT:0>,<NUM_LIT:1>), "<STR_LIT>"<EOL>subarrays = defaultdict(list)<EOL>cnt = count()<EOL>def accumulate(x, s, c):<EOL><INDENT>t = tuple(x)<EOL>col = next(c)<EOL>iadd(s[t], [col])<EOL>return None<EOL><DEDENT>if axis == <NUM_LIT:0>: [accumulate(x, subarrays, cnt) for x in X]<EOL>else: [accumulate(x, subarrays, cnt) for x in X.T]<EOL>return subarrays<EOL>
Find common subarrays of 2 dimensional X, where axis is the axis to apply the search over.
Common subarrays are returned as a dictionary of <subarray, [index]> pairs, where
the subarray is a tuple representing the subarray and the index is the index
for the subarray in X, where index is the index to the remaining axis.

:param :class:`np.ndarray` X: 2d array to check for common subarrays in
:param int axis: axis to apply subarray detection over.
    When the index is 0, compare rows -- columns, otherwise.

Examples:
=========

In a 2d array:
>>> import numpy as np
>>> X = np.zeros((3,6), dtype=bool)
>>> X[[1,1,1],[0,4,5]] = 1; X[1:,[2,3]] = 1
>>> X
array([[False, False, False, False, False, False],
       [ True, False,  True,  True,  True,  True],
       [False, False,  True,  True, False, False]], dtype=bool)
>>> d = common_subarrays(X,axis=1)
>>> len(d)
3
>>> X[:, d[tuple(X[:,0])]]
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> d[tuple(X[:,4])] == d[tuple(X[:,0])] == [0, 4, 5]
True
>>> d[tuple(X[:,1])]
[1]