統計眾數介紹
在統計學中,在所提供的一組數據值中更經常出現的值被稱為眾數。換句話說,具有高頻率或重復出現的數字或值被稱為眾數或眾數值。眾數是中心趨勢的三個措施之一。另外兩個指標分別是平均值和中位數。
例如-
我們有一套A = { 4,5,6,6,7,8,9} 。由于數字 6 的頻率較高,因此A 組的眾數為 6 。因此,對于有限數量的觀察,很容易找到眾數。一組數據值可能有一個模態值或多個模態值,或者根本沒有模態。連續概率分布的一種眾數常被稱為任意值 x,它的概率密度函數有一個最大局部值,所以任意一個峰值都是一種眾數。
Python 中的 mode()函數
Python 在處理統計數據和處理一組大范圍的數據值時,成為了一種非常強大的編程語言。Python 為 統計模塊提供了許多進程相當大數據集的功能,而眾數()功能就是其中之一。眾數()函數用于返回給定數據集范圍內中心數據點的穩健度量。
眾數()函數是 Python 編程語言的標準統計庫中唯一可以應用于非數值(標稱)數據的函數。
讓我們看看 Python 中眾數函數的語法。
語法:
眾數()功能的語法如下所示:
statistics.mode(data)
Python 中 mode()函數的參數
眾數()功能的參數是數據。它可以是一個迭代或序列——例如,列表、元組等等。
注意:如果數據參數為空,mode()函數將引發 StatisticsError。
Python 中 mode()函數的返回值
一旦在迭代器(例如,列表、元組等等)中計算了所提供數據的眾數,眾數()函數將根據參數中所提供的數據返回一個浮點數或非數值(標稱)值。
讓我們考慮一些基于 Python 編程語言的標準統計庫的眾數()函數的例子。
例 1:找到下面給出的數據集的眾數:
# importing the statistics library
import statistics
# creating the data set
my_set = [10, 20, 30, 30, 40, 40, 40, 50, 50, 60]
# estimating the mode of the given set
my_mode = statistics.mode( my_set)
# printing the estimated mode to the users
print("Mode of given set of data values is", my_mode)
輸出:
Mode of given set of data values is 40
說明:
在上例中,我們導入了統計庫,并創建了一個集合作為 my_set 。然后,我們使用 statistics.mode() 函數估計給定集合的眾數,并將其值打印給用戶。結果,該組中具有最高頻率的值被成功打印。
示例 2:演示 mode()函數在不同種類的數據類型上的工作。
# importing the statistics library
import statistics
# importing the fractions module
from fractions import Fraction as fr
# creating the tuple of positive integer numbers
data_1 = (20, 30, 30, 40, 50, 50, 50, 60, 70, 70)
# creating the tuple of floating point values
data_2 = (1.2, 2.3, 2.3, 3.4, 4.5, 4.5, 4.5, 5.6, 5.6, 7.8)
# creating the tuple of fractional numbers
data_3 = (fr(1,3), fr(1,5), fr(1,5), fr(2,3), fr(3,4), fr(8,9))
# creating the tuple of negative integer numbers
data_4 = (-9, -8, -7, -7, -7, -6, -5, -5, -4, -2)
# creating the tuple of strings
data_5 = ("apple", "mango", "mango", "mango", "banana", "guava", "guava")
# estimating the mode of the given datasets
mode_1 = statistics.mode( data_1)
mode_2 = statistics.mode( data_2)
mode_3 = statistics.mode( data_3)
mode_4 = statistics.mode( data_4)
mode_5 = statistics.mode( data_5)
# printing the estimated modes to the users
print("1\. Mode of First Data set is", mode_1)
print("2\. Mode of Second Data set is", mode_2)
print("3\. Mode of Third Data set is", mode_3)
print("4\. Mode of Forth Data set is", mode_4)
print("5\. Mode of Fifth Data set is", mode_5)
輸出:
1\. Mode of First Data set is 50
2\. Mode of Second Data set is 4.5
3\. Mode of Third Data set is 1/5
4\. Mode of Forth Data set is -7
5\. Mode of Fifth Data set is mango
說明:
在上例中,我們導入了統計庫和分數模塊。然后,我們創建了一個不同范圍的元組來檢查眾數()函數是否適用于各種數據類型。我們已經創建了一個由正整數、浮點值、小數、負整數和字符串組成的元組。然后我們使用 statistics.mode() 函數來計算每個數據集的眾數。然后,我們將這些估計值打印給用戶。
mode()函數的一些應用
眾數()功能是一個統計功能,通常用于金融行業,以便將價格和價值與以前的記錄進行比較。它還有助于從價格分布集合中計算和預測未來可能的價格。眾數()功能不單獨使用;然而,除此之外還有另外兩種統計方法,即平均值和中位數。這三者共同作為一個強大的工具來揭示數據的許多方面。