python:numpy数组常用的统计函数

数据准备:

In [1]:import numpy as np
In [2]:t = np.arange(24).reshape(4,6)    # 创建数组
In [3]:t
Out[3]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
In [4]:t = t.astype(float)                      # 修改数组的数据类型为float
In [5]: t
Out[5]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.],
       [ 6.,  7.,  8.,  9., 10., 11.],
       [12., 13., 14., 15., 16., 17.],
       [18., 19., 20., 21., 22., 23.]])
In [6]:t[3,3:] = np.nan                        # 赋值nan
In [7]:t
Out[7]: 
array([[ 0.,  1.,  2.,  3.,  4.,  5.],
       [ 6.,  7.,  8.,  9., 10., 11.],
       [12., 13., 14., 15., 16., 17.],
       [18., 19., 20., nan, nan, nan]])
  1. 求和
In [8]:np.sum(t,axis=0)
Out[8]: array([36., 40., 44., nan, nan, nan])
In [9]:np.sum(t,axis=1)
Out[9]: array([15., 51., 87., nan])
In [10]:t.sum(axis=0)
Out[10]: array([36., 40., 44., nan, nan, nan])
In [11]:t.sum(axis=1)
Out[11]: array([15., 51., 87., nan])

  1. 求均值
In [12]:t.mean()
Out[12]: nan
In [13]:t.mean(axis=0)
Out[13]: array([ 9., 10., 11., nan, nan, nan])
In [14]:t.mean(axis=1)
Out[14]: array([ 2.5,  8.5, 14.5,  nan])

  1. 求中值
In [15]: t.median()          #  无该种求中值类型
Traceback (most recent call last):

  File "", line 1, in 
    t.median()

AttributeError: 'numpy.ndarray' object has no attribute 'median'
In [16]:np.median(t,axis=0)
Out[16]: array([ 9., 10., 11., nan, nan, nan])
In [17]:np.median(t,axis=1)
Out[17]: array([ 2.5,  8.5, 14.5,  nan])
  1. 求最大值和最小值
#  求最大值
In [18]:np.max(t,axis=0)                           #   方案一
Out[18]: array([18., 19., 20., nan, nan, nan])
In [19]:np.max(t,axis=1)
Out[19]: array([ 5., 11., 17., nan])
**********************************************
In [20]:t.max(axis=0)                              #   方案二
Out[20]: array([18., 19., 20., nan, nan, nan])
In [21]:t.max(axis=1)
Out[21]: array([ 5., 11., 17., nan])
===========================================
# 求最小值
In [22]:np.min(t,axis=0)
Out[22]: array([ 0.,  1.,  2., nan, nan, nan])
In [23]:np.min(t,axis=1)
Out[23]: array([ 0.,  6., 12., nan])
********************************************
In [24]:t.min(axis=0)
Out[24]: array([ 0.,  1.,  2., nan, nan, nan])
In [25]:t.min(axis=1)
Out[25]: array([ 0.,  6., 12., nan])
  1. 求极值(最大值和最小值之差)、
In [26]:t.ptp(axis=0)                               #  两种方案,都可以求得结果
Out[26]: array([18., 18., 18., nan, nan, nan])
In [29]:np.ptp(t,axis=0)
Out[29]: array([18., 18., 18., nan, nan, nan])
In [27]:t.ptp(axis=1)
Out[27]: array([ 5.,  5.,  5., nan])
In [30]:np.ptp(t,axis=1)
Out[30]: array([ 5.,  5.,  5., nan])

6、标准差

In [31]:np.std(t,axis=0)               #   #  两种方案,都可以求得结果
Out[31]: 
array([6.70820393, 6.70820393, 6.70820393,        nan,        nan,
              nan])
In [33]:np.std(t,axis=1)
Out[33]: 
array([6.70820393, 6.70820393, 6.70820393,        nan,        nan,
              nan])
In [32]:np.std(t,axis=1)
Out[32]: array([1.70782513, 1.70782513, 1.70782513,        nan])
In [34]:t.std(axis=1)
Out[34]: array([1.70782513, 1.70782513, 1.70782513,        nan])

你可能感兴趣的:(python:numpy数组常用的统计函数)