📜  如何计算 NumPy 数组的众数?

📅  最后修改于: 2022-05-13 01:54:52.199000             🧑  作者: Mango

如何计算 NumPy 数组的众数?

在本文中,我们将讨论如何计算 Numpy Array 的众数。

众数是指数组中重复次数最多的元素。我们可以使用以下方法从 NumPy 数组中找到模式。

方法一:使用 scipy.stats 包

让我们看看 mode()函数的语法

注意:要应用模式,我们需要创建一个数组。在Python中,我们可以使用 numpy 包创建一个数组。因此,首先我们需要使用 numpy 包创建一个数组,并在该数组上应用 mode()函数。让我们看一些例子以便更好地理解。

示例 1:

应用于一维阵列

Python3
# importing required packages
from scipy import stats as st
import numpy as np
 
# creating an array using array() method
abc = np.array([1, 1, 2, 2, 2, 3, 4, 5])
 
# applying mode operation on array and
# printing result
print(st.mode(abc))


Python3
# importing required modules
import numpy as np
from scipy import stats as st
 
# creating a 2-D array using numpy package
arr = np.array([[1, 2, 3, 4, 5],
                [1, 2, 2, 2, 2],
                [4, 5, 7, 9, 4],
                [6, 7, 8, 9, 2],
                [2, 3, 4, 8, 6]])
 
# applying mode operation and printing the
# result
print(st.mode(arr))


Python3
import statistics as st
import numpy as np
 
# create an 1 d array
arr1 = np.array([9, 8, 7, 6, 6, 6, 6, 5, 5, 4,
                 3, 2, 1, 1, 1, 1, 1, 1])
 
# display the mode
print(st.mode(arr1))


Python3
# creating a list
lst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 5, 5, 5]
 
# defining a function to calculate mode. It
# takes list variable as argument
def mode(lst):
     
    # creating a dictionary
    freq = {}
    for i in lst:
       
        # mapping each value of list to a
        # dictionary
        freq.setdefault(i, 0)
        freq[i] += 1
         
    # finding maximum value of dictionary
    hf = max(freq.values())
     
    # creating an empty list
    hflst = []
     
    # using for loop we are checking for most
    # repeated value
    for i, j in freq.items():
        if j == hf:
            hflst.append(i)
             
    # returning the result
    return hflst
 
# calling mode() function and passing list
# as argument
print(mode(lst))


输出 :

ModeResult(mode=array([2]), count=array([3]))

示例 2:

应用于二维阵列

Python3

# importing required modules
import numpy as np
from scipy import stats as st
 
# creating a 2-D array using numpy package
arr = np.array([[1, 2, 3, 4, 5],
                [1, 2, 2, 2, 2],
                [4, 5, 7, 9, 4],
                [6, 7, 8, 9, 2],
                [2, 3, 4, 8, 6]])
 
# applying mode operation and printing the
# result
print(st.mode(arr))

输出 :

方法二:使用统计模块

与 NumPy 模块一样,统计模块也包含统计函数,如 mean 、 median 、 mode ....etc 。因此,让我们看一个使用统计模块的模式示例。

例子 :

Python3

import statistics as st
import numpy as np
 
# create an 1 d array
arr1 = np.array([9, 8, 7, 6, 6, 6, 6, 5, 5, 4,
                 3, 2, 1, 1, 1, 1, 1, 1])
 
# display the mode
print(st.mode(arr1))

输出 :

1

方法三:使用自定义函数

在这里,我们没有使用任何预定义函数来获取系列的模式。让我们看一个示例,演示如何在没有预定义函数的情况下计算众数。

例子 :

Python3

# creating a list
lst = [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 5, 5, 5]
 
# defining a function to calculate mode. It
# takes list variable as argument
def mode(lst):
     
    # creating a dictionary
    freq = {}
    for i in lst:
       
        # mapping each value of list to a
        # dictionary
        freq.setdefault(i, 0)
        freq[i] += 1
         
    # finding maximum value of dictionary
    hf = max(freq.values())
     
    # creating an empty list
    hflst = []
     
    # using for loop we are checking for most
    # repeated value
    for i, j in freq.items():
        if j == hf:
            hflst.append(i)
             
    # returning the result
    return hflst
 
# calling mode() function and passing list
# as argument
print(mode(lst))

输出 :

[5]