Home >Backend Development >Python Tutorial >How to find the average of a list in python?
Recommended tutorial: "python video tutorial"
How to find the average of a list in python?
Python function method to find the average of a list:
Usage: mean(matrix,axis=0) where matrix is a matrix and axis is a parameter
Take m * n matrix as an example:
axis does not set a value, averages m*n numbers, and returns a real number
axis=0: compress rows, each Calculate the average of the columns, return 1* n matrix
axis=1: compress the column, average each row, return m *1 matrix
>>> import numpy as np >>> num1 = np.array([[1,2,3],[2,3,4],[3,4,5],[4,5,6]]) >>> now2 = np.mat(num1) >>> now2 matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]) >>> np.mean(now2) # 对所有元素求均值 3.5 >>> np.mean(now2,0) # 压缩行,对各列求均值 matrix([[ 2.5, 3.5, 4.5]]) >>> np.mean(now2,1) # 压缩列,对各行求均值 matrix([[ 2.], [ 3.], [ 4.], [ 5.]])
Recommended related articles: "python tutorial》
The above is the detailed content of How to find the average of a list in python?. For more information, please follow other related articles on the PHP Chinese website!