【数据挖掘重要笔记day07】NumPy 官方快速入门教程(译)

写在前面

1、本文是NumPy官方网站(http://www.numpy.org/)的快速入门教程(Quickstart tutorial)的完整翻译版本,原文地址是https://docs.scipy.org/doc/numpy/user/quickstart.html。适合新手上路。
2、原文代码使用python自带的IDE(IDLE)作为编辑器,但不利于代码整体复制粘贴,故作者除了贴出原文代码,还基于pycharm整合出了整体代码,使得看起来更方便,且符合PEP8标准。

—————————————–正文开始!——————————————-

快速入门教程

预备知识

在阅读本教程之前,您应该了解一些Python。如果您想要刷新您的记忆,请查看主页专栏的Python教程。

基础部分

NumPy的主要对象是同构多维数组(homogeneous multidimensional array),它是由同一类型元素组成的表(通常是数字)并由一个正整数元组索引。在NumPy中,维度(dimensions)称为轴(axes)。
举例来说,在3D空间中的一个点的坐标[1, 2, 1]有1个轴。该轴有3个元素,所以我们说这个轴的长度是3。下面的例子中,数组有2个轴。第一个轴长度为2,第二个轴长度为3。

[[ 1., 0., 0.],
 [ 0., 1., 2.]]12

NumPy的数组类(array class)称为ndarray。它还有个别名array。要注意的是,numpy.array和python标准库的array.array类不同,后者只能处理1维素组,并且功能较少。ndarray对象的重要属性有:

ndarray.ndim
#数组的轴(维)数。
ndarray.shape
  #数组的维度。它是一个表示每个维度尺寸的整数元组。对于一个n行m列的矩阵(matrix),shape是(n,m)。shape元组的长度也即是轴数,ndim。
ndarray.size
  #数组的元素总数。它等于shape中的每个元素的乘积。
ndarray.dtype
  #描述数组中元素类型的对象。可以使用标准Python类型创建或指定dtype。另外,NumPy提供了它自己的类型,如numpy.int32、numpy.int16 、numpy、float64等。
ndarray.itemsize
  #数组中每个元素的字节数。例如,一个元素类型为float64的数组的itemsize为8(=64/8),元素类型为comples32的数组的itemsize为4(=32/8)。
它还等于ndarray.dtype.itemsize。
ndarray.data
  #包含数组的实际元素的缓冲区。通常,我们不需要使用这个属性,因为我们将使用索引工具访问数组中的元素。

一些例子
官网源码:

>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8
>>> a.size
15
>>> type(a)
<type 'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
>>> type(b)
<type 'numpy.ndarray'>1234567891011121314151617181920212223

pycharm代码:

import numpy as np
a = np.arange(15).reshape(3, 5)
print('a is:\n', a)
print('--------------------')
print('a.shape is:     ', a.shape)
print('--------------------')
print('a.ndim is:      ', a.ndim)
print('--------------------')
print('a.dtype.name is:', a.dtype.name)
print('--------------------')
print('a.itemsize is:  ', a.itemsize)
print('--------------------')
print('a.size is:      ', a.size)
print('--------------------')
print('type(a) is:     ', type(a))
print('--------------------')
b = np.array([6, 7, 8])
print('b is:\n', b)
print('--------------------')
print('type(b) is:     ', type(b))1234567891011121314151617181920

输出结果

a is:
 [[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
--------------------
a.shape is:      (3, 5)
--------------------
a.ndim is:       2
--------------------
a.dtype.name is: int32
--------------------
a.itemsize is:   4
--------------------
a.size is:       15
--------------------
type(a) is:      <class 'numpy.ndarray'>
--------------------
b is:
 [6 7 8]
--------------------
type(b) is:      <class 'numpy.ndarray'>123456789101112131415161718192021

生成数组

有多种途径生成数组。

例如,您可以使用数组函数(array function)从常规的Python列表或元组中创建。生成的数组类型由序列(即,列表或元组。译者注)中元素的类型推断出来。
官网代码:

>>> import numpy as np
>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])
>>> a.dtype
dtype('int64')
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64')123456789

pycharm代码:

import numpy as np
a = np.array([2, 3, 4])
print(a)
print(a.dtype)
b = np.array([1.2, 3.5, 5.1])
print(b.dtype)123456

结果:

[2 3 4]
int32
float64123

调用array时应该以列表形式提供给array单一参数。有个常见的错误,就是用多个数字直接作为array的参数。

>>> a = np.array(1,2,3,4)    # WRONG
>>> a = np.array([1,2,3,4])  # RIGHT12

array把序列的序列转化成2维的数组,把序列的序列的序列转化为3维的数组,以此类推
官方代码:

>>> b = np.array([(1.5,2,3), (4,5,6)])
>>> b
array([[ 1.5,  2. ,  3. ],
       [ 4. ,  5. ,  6. ]])1234

pycharm代码:

import numpy as np
b = np.array([(1.5, 2, 3), (4, 5, 6)])
print(b)123

结果:

[[1.5 2.  3. ]
 [4.  5.  6. ]]12

可以在创建数组时显式地指定数据类型:
官方代码:

>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
>>> c
array([[ 1.+0.j,  2.+0.j],
       [ 3.+0.j,  4.+0.j]])1234

pycharm代码:

import numpy as np
c = np.array([[1, 2], [3, 4]], dtype=complex)
print(c)123
[[1.+0.j 2.+0.j]
 [3.+0.j 4.+0.j]]12

通常来说,数组元素最初是未知的,但尺寸是已知的。因此,NumPy提供了几个函数来创建带有初始占位符内容的数组。这些函数最小化了增大数组尺寸这种操作成本极高的需求。

函数zeros创建了一个全0的数组,函数ones创建了一个全1数组,而函数empty创建了一个初始内容是随机,依赖于内存的状态的数组(元素值都是极小的数字。译者注)。默认情况下,创建的数组的dtype是float64。
官方代码:

>>> np.zeros( (3,4) )
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
>>> np.ones( (2,3,4), dtype=np.int16 )                # dtype can also be specified
array([[[ 1, 1, 1, 1],
        [ 1, 1, 1, 1],
        [ 1, 1, 1, 1]],
       [[ 1, 1, 1, 1],
        [ 1, 1, 1, 1],
        [ 1, 1, 1, 1]]], dtype=int16)
>>> np.empty( (2,3) )                                 # uninitialized, output may vary
array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],
       [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])1234567891011121314

pycharm代码:

import numpy as np
eg1 = np.zeros((3, 4))
print(eg1)
print('--------------------')
eg2 = np.ones((2, 3, 4), dtype=np.int16)
print(eg2)
print('--------------------')
eg3 = np.empty((2, 3))
print(eg3)123456789
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
--------------------
[[[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]

 [[1 1 1 1]
  [1 1 1 1]
  [1 1 1 1]]]
--------------------
[[1.60216183e-306 7.56602523e-307 1.11258446e-306]
 [6.23059726e-307 6.23060065e-307 1.50200974e-307]]1234567891011121314

为了创建数字序列,NumPy提供了一个类似于range这种返回数组而不是列表的函数。
官方代码:

>>> np.arange( 10, 30, 5 )
array([10, 15, 20, 25])
>>> np.arange( 0, 2, 0.3 )                 # it accepts float arguments
array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])1234

pycharm代码:

import numpy as np
eg1 = np.arange(10, 30, 5)
print(eg1)
print(type(eg1))
eg2 = np.arange(0, 2, 0.3)
print(eg2)
print(type(eg2))1234567
[10 15 20 25]
<class 'numpy.ndarray'>
[0.  0.3 0.6 0.9 1.2 1.5 1.8]
<class 'numpy.ndarray'>1234

当arange的参数为浮点数时,因为浮点数精度有限,不太可能预测元素的数量。因此,使用linspace会比较好,linspace接收元素数量的参数。
官方代码:

>>> from numpy import pi
>>> np.linspace( 0, 2, 9 )                 # 9 numbers from 0 to 2
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])
>>> x = np.linspace( 0, 2*pi, 100 )        # useful to evaluate function at lots of points
>>> f = np.sin(x)12345

pycharm代码:

import numpy as np
eg1 = np.linspace(0, 2, 9)
print(eg1)
print('--------------------------------')
x = np.linspace(0, 2*np.pi, 10)  # 原文要100个数,太多了,简化到10个
print(x)
print('--------------------------------')
f = np.sin(x)
print(f)123456789
[0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]
----------------------
[0.         0.6981317  1.3962634  2.0943951  2.7925268  3.4906585
 4.1887902  4.88692191 5.58505361 6.28318531]
----------------------
[ 0.00000000e+00  6.42787610e-01  9.84807753e-01  8.66025404e-01
  3.42020143e-01 -3.42020143e-01 -8.66025404e-01 -9.84807753e-01
 -6.42787610e-01 -2.44929360e-16]12345678

打印数组

当您打印一个数组时,NumPy以类似于嵌套列表的方式显示它,但是使用以下布局:

最后一个轴从左到右打印,
  倒数第二个轴从上到下打印,
  其余的部分也从上到下打印,每一块都由一条空的线隔开。

然后将一维数组打印为行,二维数组打印为矩阵,三维数组打印为矩阵列表。
官方原代码:

>>> a = np.arange(6)                         # 1d array
>>> print(a)
[0 1 2 3 4 5]
>>>
>>> b = np.arange(12).reshape(4,3)           # 2d array
>>> print(b)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
>>>
>>> c = np.arange(24).reshape(2,3,4)         # 3d array
>>> print(c)
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]
 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]12345678910111213141516171819

pycharm代码:

import numpy as np

a = np.arange(6)
print('a:\n', a)
print('-----------------------')
b = np.arange(12).reshape(4, 3)
print('b:\n', b)
print('-----------------------')
c = np.arange(24).reshape(2, 3, 4)
print('c:\n', c)12345678910
a:
 [0 1 2 3 4 5]
-----------------------
b:
 [[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
-----------------------
c:
 [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]1234567891011121314151617

看下面的内容以获得关于reshape的详情。
如果数组太大而无法打印,NumPy会自动跳过数组的中心部分,只打印边角:
官方原代码:

>>> print(np.arange(10000))
[   0    1    2 ..., 9997 9998 9999]
>>>
>>> print(np.arange(10000).reshape(100,100))
[[   0    1    2 ...,   97   98   99]
 [ 100  101  102 ...,  197  198  199]
 [ 200  201  202 ...,  297  298  299]
 ...,
 [9700 9701 9702 ..., 9797 9798 9799]
 [9800 9801 9802 ..., 9897 9898 9899]
 [9900 9901 9902 ..., 9997 9998 9999]]1234567891011

pycharm代码:

import numpy as np

print(np.arange(10000))
print('-------------------------------------------')
print(np.arange(10000).reshape(100,100))12345
[   0    1    2 ... 9997 9998 9999]
-------------------------------------------
[[   0    1    2 ...   97   98   99]
 [ 100  101  102 ...  197  198  199]
 [ 200  201  202 ...  297  298  299]
 ...
 [9700 9701 9702 ... 9797 9798 9799]
 [9800 9801 9802 ... 9897 9898 9899]
 [9900 9901 9902 ... 9997 9998 9999]]123456789

要禁用这种行为并强制NumPy打印整个数组,可以使用set_printoptions更改打印选项。
官方原代码:

>>> np.set_printoptions(threshold=np.nan)1
pycharm代码:

import numpy as np

print(np.arange(1001))
np.set_printoptions(threshold=np.nan)
print('---------------------------set to print all-------------------------------')
print(np.arange(1001))123456
[   0    1    2 ...  998  999 1000]
---------------------------set to print all-------------------------------
[   0    1    2    3    4    5    6    7    8    9   10   11   12   13
   14   15   16   17   18   19   20   21   22   23   24   25   26   27
   28   29   30   31   32   33   34   35   36   37   38   39   40   41
   42   43   44   45   46   47   48   49   50   51   52   53   54   55
   56   57   58   59   60   61   62   63   64   65   66   67   68   69
   70   71   72   73   74   75   76   77   78   79   80   81   82   83
   84   85   86   87   88   89   90   91   92   93   94   95   96   97
   98   99  100  101  102  103  104  105  106  107  108  109  110  111
  112  113  114  115  116  117  118  119  120  121  122  123  124  125
  126  127  128  129  130  131  132  133  134  135  136  137  138  139
  140  141  142  143  144  145  146  147  148  149  150  151  152  153
  154  155  156  157  158  159  160  161  162  163  164  165  166  167
  168  169  170  171  172  173  174  175  176  177  178  179  180  181
  182  183  184  185  186  187  188  189  190  191  192  193  194  195
  196  197  198  199  200  201  202  203  204  205  206  207  208  209
  210  211  212  213  214  215  216  217  218  219  220  221  222  223
  224  225  226  227  228  229  230  231  232  233  234  235  236  237
  238  239  240  241  242  243  244  245  246  247  248  249  250  251
  252  253  254  255  256  257  258  259  260  261  262  263  264  265
  266  267  268  269  270  271  272  273  274  275  276  277  278  279
  280  281  282  283  284  285  286  287  288  289  290  291  292  293
  294  295  296  297  298  299  300  301  302  303  304  305  306  307
  308  309  310  311  312  313  314  315  316  317  318  319  320  321
  322  323  324  325  326  327  328  329  330  331  332  333  334  335
  336  337  338  339  340  341  342  343  344  345  346  347  348  349
  350  351  352  353  354  355  356  357  358  359  360  361  362  363
  364  365  366  367  368  369  370  371  372  373  374  375  376  377
  378  379  380  381  382  383  384  385  386  387  388  389  390  391
  392  393  394  395  396  397  398  399  400  401  402  403  404  405
  406  407  408  409  410  411  412  413  414  415  416  417  418  419
  420  421  422  423  424  425  426  427  428  429  430  431  432  433
  434  435  436  437  438  439  440  441  442  443  444  445  446  447
  448  449  450  451  452  453  454  455  456  457  458  459  460  461
  462  463  464  465  466  467  468  469  470  471  472  473  474  475
  476  477  478  479  480  481  482  483  484  485  486  487  488  489
  490  491  492  493  494  495  496  497  498  499  500  501  502  503
  504  505  506  507  508  509  510  511  512  513  514  515  516  517
  518  519  520  521  522  523  524  525  526  527  528  529  530  531
  532  533  534  535  536  537  538  539  540  541  542  543  544  545
  546  547  548  549  550  551  552  553  554  555  556  557  558  559
  560  561  562  563  564  565  566  567  568  569  570  571  572  573
  574  575  576  577  578  579  580  581  582  583  584  585  586  587
  588  589  590  591  592  593  594  595  596  597  598  599  600  601
  602  603  604  605  606  607  608  609  610  611  612  613  614  615
  616  617  618  619  620  621  622  623  624  625  626  627  628  629
  630  631  632  633  634  635  636  637  638  639  640  641  642  643
  644  645  646  647  648  649  650  651  652  653  654  655  656  657
  658  659  660  661  662  663  664  665  666  667  668  669  670  671
  672  673  674  675  676  677  678  679  680  681  682  683  684  685
  686  687  688  689  690  691  692  693  694  695  696  697  698  699
  700  701  702  703  704  705  706  707  708  709  710  711  712  713
  714  715  716  717  718  719  720  721  722  723  724  725  726  727
  728  729  730  731  732  733  734  735  736  737  738  739  740  741
  742  743  744  745  746  747  748  749  750  751  752  753  754  755
  756  757  758  759  760  761  762  763  764  765  766  767  768  769
  770  771  772  773  774  775  776  777  778  779  780  781  782  783
  784  785  786  787  788  789  790  791  792  793  794  795  796  797
  798  799  800  801  802  803  804  805  806  807  808  809  810  811
  812  813  814  815  816  817  818  819  820  821  822  823  824  825
  826  827  828  829  830  831  832  833  834  835  836  837  838  839
  840  841  842  843  844  845  846  847  848  849  850  851  852  853
  854  855  856  857  858  859  860  861  862  863  864  865  866  867
  868  869  870  871  872  873  874  875  876  877  878  879  880  881
  882  883  884  885  886  887  888  889  890  891  892  893  894  895
  896  897  898  899  900  901  902  903  904  905  906  907  908  909
  910  911  912  913  914  915  916  917  918  919  920  921  922  923
  924  925  926  927  928  929  930  931  932  933  934  935  936  937
  938  939  940  941  942  943  944  945  946  947  948  949  950  951
  952  953  954  955  956  957  958  959  960  961  962  963  964  965
  966  967  968  969  970  971  972  973  974  975  976  977  978  979
  980  981  982  983  984  985  986  987  988  989  990  991  992  993
  994  995  996  997  998  999 1000]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374

基本操作

数组上的算术运算符使用元素。
一个新的数组被创建并填充结果。
官方原代码:

>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> 10*np.sin(a)
array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])
>>> a<35
array([ True, True, False, False])12345678910111213

pycharm代码:

import numpy as np

a = np.array([20, 30, 40, 50])
print('a is:\n', a)
b = np.arange(4)
print('b is:\n', b)
c = a - b
print('a - b =\n', c)
print('--------------------------------------------')
print('b**2 is:\n', b**2)
print('--------------------------------------------')
print('10*np.sin(a) is:\n', 10*np.sin(a))
print('--------------------------------------------')
print('a<35?\n', a < 35)
123456789101112131415
a is:
 [20 30 40 50]
b is:
 [0 1 2 3]
a - b =
 [20 29 38 47]
--------------------------------------------
b**2 is:
 [0 1 4 9]
--------------------------------------------
10*np.sin(a) is:
 [ 9.12945251 -9.88031624  7.4511316  -2.62374854]
--------------------------------------------
a<35?
 [ True  True False False]123456789101112131415

与许多矩阵语言不同,乘积操作符*在NumPy数组中对各个元素进行操作。
矩阵乘积可以使用点函数(dot function)或方法(method)进行:
官方原代码:

>>> A = np.array( [[1,1],
...             [0,1]] )
>>> B = np.array( [[2,0],
...             [3,4]] )
>>> A*B                         # elementwise product
array([[2, 0],
       [0, 4]])
>>> A.dot(B)                    # matrix product
array([[5, 4],
       [3, 4]])
>>> np.dot(A, B)                # another matrix product
array([[5, 4],
       [3, 4]])12345678910111213

pycharm代码:

import numpy as np

A = np.array([[1, 1],
              [0, 1]])
B = np.array([[2, 0],
              [3, 4]])

print('A*B=\n', A*B)
print('---------------------')
print('A.dot(B)=\n', A.dot(B))  # 矩阵乘积
print('---------------------')
print('np.dot(A, B)=\n', np.dot(A, B))  # 矩阵乘积的另外一种表示123456789101112
A*B=
 [[2 0]
 [0 4]]
---------------------
A.dot(B)=
 [[5 4]
 [3 4]]
---------------------
np.dot(A, B)=
 [[5 4]
 [3 4]]1234567891011

一些操作,如+=和*=,会适当地修改现有数组,而不是创建新的数组。
官方原代码:

>>> a = np.ones((2,3), dtype=int)
>>> b = np.random.random((2,3))
>>> a *= 3
>>> a
array([[3, 3, 3],
       [3, 3, 3]])
>>> b += a
>>> b
array([[ 3.417022  ,  3.72032449,  3.00011437],
       [ 3.30233257,  3.14675589,  3.09233859]])
>>> a += b                  # b is not automatically converted to integer type
Traceback (most recent call last):
  ...
TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'1234567891011121314

pycharm代码:

import numpy as np

a = np.ones((2,3), dtype=int)
b = np.random.random((2,3))
a *= 3
print(a)
b += a
print(b)
# a += b  # 取消注释该行,可看到错误出现123456789
[[3 3 3]
 [3 3 3]]
[[3.40489393 3.24349054 3.2576326 ]
 [3.09449733 3.12858911 3.26363046]]1234

当操作不同类型的数组时,结果数组的类型对应于更一般或更精确的数组(一种称为向上转换的行为)。
官方原代码:

>>> a = np.ones(3, dtype=np.int32)
>>> b = np.linspace(0,pi,3)
>>> b.dtype.name
'float64'
>>> c = a+b
>>> c
array([ 1.        ,  2.57079633,  4.14159265])
>>> c.dtype.name
'float64'
>>> d = np.exp(c*1j)
>>> d
array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,
       -0.54030231-0.84147098j])
>>> d.dtype.name
'complex128'123456789101112131415

pycharm代码:

import numpy as np

a = np.ones(3, dtype=np.int32)
b = np.linspace(0, np.pi, 3)
print('a is:', a)
print('b is:', b)
print('b type is:', b.dtype.name)
print('-'*50)
c = a + b
print('c is:', c)
print('c type is:', c.dtype.name)
print('-'*50)
d = np.exp(c*1j)
print('d is:\n', d)
print('d type is:', d.dtype.name)
12345678910111213141516
a is: [1 1 1]
b is: [0.         1.57079633 3.14159265]
b type is: float64
--------------------------------------------------
c is: [1.         2.57079633 4.14159265]
c type is: float64
--------------------------------------------------
d is:
 [ 0.54030231+0.84147098j -0.84147098+0.54030231j -0.54030231-0.84147098j]
d type is: complex12812345678910

许多一元运算,例如计算数组中所有元素的和,都是作为ndarray类的方法实现的。
官方原代码:

>> a = np.random.random((2,3))
>>> a
array([[ 0.18626021,  0.34556073,  0.39676747],
       [ 0.53881673,  0.41919451,  0.6852195 ]])
>>> a.sum()
2.5718191614547998
>>> a.min()
0.1862602113776709
>>> a.max()
0.685219500396759512345678910

pycharm代码:

import numpy as np

a = np.random.random((2,3))
print('a is:\n', a)
print('a.sum() is:', a.sum())
print('a.min() is:', a.min())
print('a.max() is:', a.max())1234567
a is:
 [[0.48359412 0.17621728 0.86433261]
 [0.94612411 0.84234692 0.93902957]]
a.sum() is: 4.251644614661762
a.min() is: 0.17621727774779938
a.max() is: 0.9461241148697829123456

默认情况下,这些操作应用于数组,就好像它是一个数字列表,不管它的形状如何。
但是,通过指定axis参数,您可以沿着数组的指定轴执行一个操作:
官方原代码:

>>> b = np.arange(12).reshape(3,4)
>>> b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>>
>>> b.sum(axis=0)                            # sum of each column
array([12, 15, 18, 21])
>>>
>>> b.min(axis=1)                            # min of each row
array([0, 4, 8])
>>>
>>> b.cumsum(axis=1)                         # cumulative sum along each row
array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]])12345678910111213141516

pycharm代码:

import numpy as np

b = np.arange(12).reshape(3,4)
print('b is:\n', b)
print('b.sum(axis=0) is:', b.sum(axis=0))
print('b.sum(axis=1) is:', b.sum(axis=1))
print('b.cumsum(axis=1) is:\n', b.cumsum(axis=1))
12345678
b is:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
b.sum(axis=0) is: [12 15 18 21]
b.sum(axis=1) is: [ 6 22 38]
b.cumsum(axis=1) is:
 [[ 0  1  3  6]
 [ 4  9 15 22]
 [ 8 17 27 38]]

请记得点赞和分享这篇文章让更多的人看到它!

我会阅读所有的评论,所以无论你有什么想要说的,或者是想要分享的,甚至是问题之类的,都可以在下面留言。

发布了476 篇原创文章 · 获赞 400 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_35456045/article/details/104260080