Reorder matrix values by indices in Python

Viewed 134

If I have this matrix:

Then, the flattened array of A is:

And I want to find one list of lists that indicates the index of each distinct value and a vector with the the repetitions of those.

The list of lists with the indices must be (where the index of each list is referred to the taken value: list's index 0 for value 0, list's index 1 for value 1... etc):

and the vector of repetitions must be (where the index in reps array indicates the value in the matrix A: index 0 for value 0, index 2 for value 2... etc)

my first approach to solve this have been iterating the A flattened matrix to get the index and populate the list of lists. Once populated the list, I iterate it to get the longs of each list to find the repetitions of each value in matrix A. The code is:

def data_mask(i_s, f_s):
    # Matrix data origin
    mask, out_dims = im2col_msk(i_s, f_s, out=True)
    # Flattened data
    a = (mask.flatten()).astype(np.int32)
    # List of lists
    data = np.empty((np.max(a)+1, 0)).tolist()
    for i in range(len(a)):
        # Each distinct value in the A matrix correponds with the index
        # in the list of lists
        data[a[i]].append(i)
    # Get the long of each list to obtain the index values.
    reps = np.array(list(map(len, data)), dtype=np.int32)
    # Convert the list of lists in a flattened array
    data = np.array([item for sublist in data for item in sublist], dtype=np.int32)
    return data, index

Problem: I have very large matrices: thousands rows by hundreds columns... Efficient way to solve this? I believe the fastest way is with a loop in C++ because methods such as numpy.where() and similars has obtained a poor perfomance in my tests.

1 Answers

Operations with lists slow down the process.

This code is more optimal:

data = np.array([10,5,4,5,6,8,6,7,9,4],np.int32)
u_data = np.unique(data)
rep = np.zeros_like(u_data)
print(u_data)
cnt=0
for i in u_data:
    rep[cnt] = np.where(data==i)[0].size
    cnt+=1
print(rep)
Related