在 Python 中查詢 Numpy 陣列列表的平均值
Numpy 是一個非常強大的 Python 庫,用於數字資料處理。它通常以陣列形式獲取資料,並應用各種函式(包括統計函式)以從陣列中獲取結果。在本文中,我們將學習如何獲得給定陣列的平均值。
有平均值
平均值函式可以接受一個數組,並給出其中所有元素的數學平均值。因此,我們設計一個 for 迴圈來跟蹤輸入的長度並遍歷每個陣列來計算其平均值。
示例
import numpy as np
# GIven Array
Arrays_In = [np.array([11, 5, 41]),
np.array([12, 13, 26]),
np.array([56, 20, 51])]
# Resultihg Array
Arrays_res = []
# With np.mean()
for x in range(len(Arrays_In)):
Arrays_res.append(np.mean(Arrays_In[x]))
# Result
print("The means of the arrays: \n",Arrays_res)輸出
執行上述程式碼,將得到以下結果 -
The means of the arrays: [19.0, 17.0, 42.333333333333336]
有平均數
它與上述方法非常相似,但使用平均數函式而不用平均值函式。它給出了完全相同的結果。
示例
import numpy as np
# GIven Array
Arrays_In = [np.array([11, 5, 41]),
np.array([12, 13, 26]),
np.array([56, 20, 51])]
# Resultihg Array
Arrays_res = []
# With np.average()
for x in range(len(Arrays_In)):
Arrays_res.append(np.average(Arrays_In[x]))
# Result
print("The means of the arrays: \n",Arrays_res)輸出
執行上述程式碼,將得到以下結果 -
The means of the arrays: [19.0, 17.0, 42.333333333333336]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP