示例:
[root@steve-lidiliang /user/MLiA_SourceCode/Ch02]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> a = array([[1, 2],
... [2, 3]
... ,[3, 4],
... [4, 5]]) ## 数组
>>> a
array([[1, 2],
[2, 3],
[3, 4],
[4, 5]])
>>> sum(a) ## 所有元素相加
24
>>> a.sum(axis=0) ## 列相加
array([10, 14])
>>> a.sum(axis=1) ## 行相加
array([3, 5, 7, 9])
>>> a.sum() ## 所有元素相加
24
>>> b=mat(a) ## 矩阵
>>> b
matrix([[1, 2],
[2, 3],
[3, 4],
[4, 5]])
>>> sum(b) # 所有元素相加
24
>>> b.sum() # 所有元素相加
24
>>> b.sum(axis=0) # 列相加
matrix([[10, 14]])
>>> b.sum(axis=1) # 行相加
matrix([[3],
[5],
[7],
[9]])
>>>