python's numpy libraries mean () function usage presentation

This article describes the python's numpy libraries mean () function usage reports, has a good reference value, we want to help. Come and see, to follow the small series together

  1. Mean () function is defined: Here Insert Picture Description
    Here Insert Picture Description
    2 Mean () Function:
求取均值

经常操作的参数为axis,以m * n矩阵举例:

axis 不设置值,对 m*n 个数求均值,返回一个实数

axis = 0:压缩行,对各列求均值,返回 1* n 矩阵

axis =1 :压缩列,对各行求均值,返回 m *1 矩阵

举例:>>> 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.]])

Supplementary expansion: np.nanmax and np.max difference numpy of (pit)

The np.nanmax numpy and np.array ([1,2,3, np.nan]) . the difference of max () (pits)
Principle

Dataframe in calculating the maximum, the first use must be max () method () Series object 4 is the end result.

s1 = pd.Series([1,2,3,4,np.nan])
s1_max = s1.max()

However, because of the huge amount of data I, large number of columns, so in order to speed up the calculation, the maximum value is calculated using numpy, but, as the following code, the final result is NaN3, rather than 4. Found, using the maximum value calculated in this manner, it will be included into NaN3, and the final result is nan.

s1 = pd.Series([1,2,3,4,np.nan])
s1_max = s1.values.max()
>>>nan

By reading the document numpy found that the function exists np.nanmax, you can calculate the maximum np.nan excluded, and get the right results you want.

Of course, not only is the max, min, std, mean when the column will contain np.nan, s1.values.min / std / mean () Returns the present case nan.

Speed ​​difference

From fast to slow turn:

s1 = pd.Series([1,2,3,4,5,np.nan])
#速度由快至慢
np.nanmax(s1.values) > np.nanmax(s1) > s1.max()

Above this python's numpy libraries mean () function is to introduce the use of small series to share the entire contents of everyone's
content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are many the veteran study skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information, information on actual projects, the timing has to explain the Python programmer technology every day, share some learning methods and needs attention small detailsHere Insert Picture Description

Published 40 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105035980