Home > Software engineering >  Memory efficient majority voting cell wise for large matrix in Numpy
Memory efficient majority voting cell wise for large matrix in Numpy

Time:10-13

Given a list of array, I would like to extract the frequent elements in every cell.

For example, for 3 array

arr 1

0,0,0
0,4,1
0,1,4

arr 2

0,0,0
0,7,1
0,1,1

arr 3

5,0,0
0,4,1
0,1,1

The most frequent element for each cell would be

0 0 0
0 4 1
0 1 1

May I know how to achieve this with Numpy? And in actual case, the list of array can be up to 10k in shape.

The list of array are defined as below

import numpy as np

arr=np.array([[0,0,0],[0,4,1],[0,1,4]])
arr2=np.array([[0,0,0],[0,7,1],[0,1,1]])
arr3=np.array([[5,0,0],[0,4,1],[0,1,1]])
arr = np.stack([arr,arr2,arr3], axis=0)

CodePudding user response:

You can stack the arrays into a large matrix and then use scipy.stats.mode along the axis of interest:

import numpy as np
import scipy.stats
arr1 = [[0,0,0],
        [0,4,1],
        [0,1,4]]
arr2 = [[0,0,0],
        [0,7,1],
        [0,1,1]]
arr3 = [[5,0,0],
        [0,4,1],
        [0,1,1]]

arr = np.stack((arr1, arr2, arr3), axis=0)
output = scipy.stats.mode(arr, axis=0).mode[0]
print(output)

# [[0 0 0]
#  [0 4 1]
#  [0 1 1]]
  • Related