develarist
develarist

Reputation: 1365

Python mode function gives error for real-valued vector: No unique mode; found 2 equally common values

Why can't statistics.mode find the mode for a normally distributed (therefore, unimodal) random variable, but works fine for vectors containing integers?

import numpy as np
from numpy.random import rand,randn
import statistics as st

y = randn(20)
print(st.mode(y))

This returns the following error

StatisticsError: no unique mode; found 20 equally common values

Upvotes: 1

Views: 509

Answers (2)

Lawhatre
Lawhatre

Reputation: 1450

That's because mode doesn't exist. The number of unique element in y and the total element in y are same so no mode exits by definition.

np.size(np.unique(y)) - np.size(y)

>>> 0

Mode doesn't exist can also be verified by looking at the histogram (flat in the present case). Peaks in this graph represents mode and since we cann't find a peak, mode is None.

Histogram for y

Edit: If you want to really find the mode then

  1. Draw enough samples from the distribution. So that it reflects the original pdf
  2. Adjust the precision (I have rounded it off to 1 decimal place). Consequently, the model will have a error range accordingly.
import numpy as np
from numpy.random import rand,randn
import statistics as st

y = randn(10000000)
st.mode(list(np.round(y,1)))

This gives

>>> 0.0 

Following is the hist (See now you also get a peak at 0.0) Hist for large samples

Upvotes: 1

honno
honno

Reputation: 53

randn returns a third-party ndarray rather than a Python builtin array (i.e. a list). The statistics module was not built to serve numpy explicitly and so unexpected behaviour occurs.

A solution could be converting y to a list (i.e. st.mode(list(y))).

Upvotes: 1

Related Questions