python-scipyHow do I calculate the mode using Python Scipy Stats?
The mode
of a dataset is the most frequently occurring value in the dataset. We can use the mode
function from the scipy.stats
module to calculate the mode of a dataset in Python.
Example code
from scipy.stats import mode
# Create a dataset
data = [1, 1, 2, 3, 3, 3, 4, 5, 5]
# Calculate the mode
mode(data)
Output example
ModeResult(mode=array([3]), count=array([3]))
The mode
function returns a ModeResult
object which contains two properties:
mode
: an array containing the most frequent value in the datasetcount
: an array containing the number of occurrences of the most frequent value
In the example above, the mode of the dataset is 3
, which appears 3
times.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python Scipy to zoom in on an image?
- How do I create a numpy array of zeros using Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How to use Python, XML-RPC, and NumPy together?
- How can I use the x.shape function in Python Numpy?
- How do I create a zero matrix using Python and Numpy?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Scipy zeros in Python?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
See more codes...