#np中的0,1轴。方括号。np.sum函数。
import numpy as np
a = np.array([1, 2, 4]) #此为(3,)阵,为列阵?
b = np.array([[5, 3, 6]]) #此为(1, 3)阵,为行阵
print(a.shape, b.shape)
print("===================")
c = np.array([[1, 2, 6],[2, 2, 4],[5, 2, 1],[1, 4, 2]])
print(c)
print(c.shape)
print("===================")
print(c[:, 0], c[:, 1], c[:, 2], c[2, 0]) #取1轴0,1,2(各列)数据...单点数据。
#print(c[0], c[1], c[2], c[3], c[4]) 。
#IndexError:index 4 is out of bounds for axis 0 with size 4(指数4超出界限,axis0只有4(行))
print(c[0], c[1], c[2], c[3]) #取0轴0,1,2,3行(各行)数据
print("===================")
print(c[:1])
print(c[:3]) #[:n] 表示取0轴前n行 ###求取来的还是矩阵格式
print(c[3:]) #[n:] 表示去除0轴前n行,取剩余
print("===================")
S0 = np.sum(c, axis=0) #沿0轴(竖轴)sum...即列c求和,因为0轴为列(竖)
S1 = np.sum(c, axis=1) #沿1轴(横轴)sum...即行r求和,因为1轴为行(横)
S =np.sum(c)
print(S0, S1, S)
输出结果==================================
(3,) (1, 3)
===================
[[1 2 6]
[2 2 4]
[5 2 1]
[1 4 2]]
(4, 3)
===================
[1 2 5 1] [2 2 2 4] [6 4 1 2] 5
[1 2 6] [2 2 4] [5 2 1] [1 4 2]
===================
[[1 2 6]]
[[1 2 6]
[2 2 4]
[5 2 1]]
[[1 4 2]]
===================
[ 9 10 13] [9 8 8 7] 32
np中的0,1轴。方括号。np.sum函数
最新推荐文章于 2021-11-29 17:47:44 发布