Returns the most common value (mode) of a matrix / array

A simple question: how to get the most common matrix value?

The matrix is ​​a specialized two-dimensional array that preserves its two-dimensional nature through operations.

This is a snippet of my whole implementation, so I decided to show you only the important parts related to my main question:

import numpy as np
...
from src.labelHandler import LabelHandler
from collections import Counter

def viewData(filePathList, labelHandler=None):
...
    c = Counter(a)       #(1)
    print(c)             #(2)
    b = np.argmax(c)     #(3)
    print(b)             #(4)
...

The conclusion will be:

{0.3: [(0, 0, 0), (0, 10, 0), (0, 11, 0), ...], 0.2: [(0, 18, 0), ...]}
Counter({0.3: 7435, 0.2: 6633, ...})
0

This is also a snippet from all of my output.

The important line is the last one with 0. The problem is that this is line (3).

b = np.argmax(c)

It just prints out the position of my highest value, which is at index 0. But I would like to return a float instead of an index.

How to solve this problem?

Thanks in advance!

+4
source share
1

scipy.stats np.array.ravel(), . , .

import numpy as np
from scipy import stats

A = np.random.randint(0, 9, (10, 10))

res = stats.mode(A.ravel())

# ModeResult(mode=array([4]), count=array([19]))
+3

Source: https://habr.com/ru/post/1695535/


All Articles