Reputation: 1365
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
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.
Edit: If you want to really find the mode then
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)
Upvotes: 1
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