>>> a = np.array([[1,2,3],[2,3,4],[4,5,6]])
>>> a
array([[1, 2, 3],
[2, 3, 4],
[4, 5, 6]])
>>> a.ndim # 数组维度
2
>>> a.shape # 行数、列数
(3, 3)
>>> a.size # 总元素个数
9
>>> b = np.array([[1,2,3],[2,3,4],[4,5,6]],dtype=np.float32) # 定义元素类型
>>> b
array([[1., 2., 3.],
[2., 3., 4.],
[4., 5., 6.]], dtype=float32)
>>> np.zeros((3,4)) # 注意有两个括号
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> np.ones((1,2))
array([[1., 1.]])
>>> np.empty((2,2))
array([[0.00000000e+000, 0.00000000e+000],
[9.72022304e-312, 9.72022433e-312]])
>>> np.random.randn(2,1) # 不知两者区别,[0,1)
array([[ 0.5679706],
[-0.0044531]])
>>> np.random.random((2,1))
array([[0.23639781],
[0.57213322]])
>>> np.arange(10,20,2) # 起始值,终点值(取不到),步长
array([10, 12, 14, 16, 18])
>>> np.arange(8).reshape((2,4)) # 改变形状
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
>>> np.linspace(1,10,4) # 起始值,终点值(取得到),个数
array([ 1., 4., 7., 10.])
>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[5,6],[7,8]])
>>> b-a
array([[4, 4],
[4, 4]])
>>> b+a
array([[ 6, 8],
[10, 12]])
>>> a*b # 元素乘积
array([[ 5, 12],
[21, 32]])
>>> a**2 # 元素幂运算
array([[ 1, 4],
[ 9, 16]], dtype=int32)
>>> b<6 # 元素相比
array([[ True, False],
[False, False]])
>>> np.sin(a)
array([[ 0.84147098, 0.90929743],
[ 0.14112001, -0.7568025 ]])
>>> np.dot(a,b) # 矩阵乘积
array([[19, 22],
[43, 50]])
>>> a.dot(b)
array([[19, 22],
[43, 50]])
>>> np.sum(a)
10
>>> np.max(a)
4
>>> np.min(a)
1
>>> np.sum(a,axis=1) # 行 加
array([3, 7])
>>> np.argmin(a) # 最小元素的序号
0
>>> np.argmax(a)
3
>>> a = np.arange(12).reshape((3,4))
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> np.mean(a) # 均值
5.5
>>> np.median(a) # 中位数
5.5
>>> np.cumsum(a) # 前i个元素累加
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66], dtype=int32)
>>> np.diff(a) # 两个元素间的差
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
>>> np.nonzero(a)
(array([0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), # 表示行
array([1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64)) # 表示列
>>> a.T # 转置
array([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
>>> np.clip(a,5,9) # 小于5,置5.大于9,置9.
array([[5, 5, 5, 5],
[5, 5, 6, 7],
[8, 9, 9, 9]])
>>> a = np.arange(12,0,-1).reshape((3,4))
>>> a
array([[12, 11, 10, 9],
[ 8, 7, 6, 5],
[ 4, 3, 2, 1]])
>>> np.sort(a) # 逐行排序,从小到大
array([[ 9, 10, 11, 12],
[ 5, 6, 7, 8],
[ 1, 2, 3, 4]])
>>> a = np.arange(12).reshape((3,4))
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a[1,1]
5
>>> a[1]
array([4, 5, 6, 7])
>>> a[1,:]
array([4, 5, 6, 7])
>>> a[:,2]
array([ 2, 6, 10])
>>> for row in a: # 打印行
print(row)
[0 1 2 3]
[4 5 6 7]
[ 8 9 10 11]
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a.flatten() # 展开属性
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> for item in a.flat: # a.flat 是一个迭代器
print(item)
0
1
2
3
4
5
6
7
8
9
10
11
>>> a
array([1, 1, 1]) # 无法进行转置
>>> b
array([2, 2, 2])
>>> np.vstack((a,b))
array([[1, 1, 1],
[2, 2, 2]])
>>> np.hstack((a,b))
array([1, 1, 1, 2, 2, 2])
>>> np.concatenate((a,b,b,a),axis=0) # 横向合并
array([1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1])
>>> a[np.newaxis,:] # 转换成矩阵,可转置
array([[1, 1, 1]])
>>> a[:,np.newaxis]
array([[1],
[1],
[1]])
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> np.split(a,2,axis=1) # 纵向切,,必须等分
[array([[0, 1],
[4, 5],
[8, 9]]),
array([[ 2, 3],
[ 6, 7],
[10, 11]])]
>>> np.array_split(a,3,axis = 1) # 可以不等分
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
>>> np.vsplit(a,3) # 横向切,必须等分
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> c=a # a和c同一个
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a[1]=2
>>> c
array([[ 0, 1, 2, 3],
[ 2, 2, 2, 2],
[ 8, 9, 10, 11]])
>>> a
array([[ 0, 1, 2, 3],
[ 2, 22, 2, 2],
[ 8, 9, 10, 11]])
>>> d=a.copy() # a,d不同
>>> d
array([[ 0, 1, 2, 3],
[ 2, 22, 2, 2],
[ 8, 9, 10, 11]])
>>> a[0,0]=9
>>> d
array([[ 0, 1, 2, 3],
[ 2, 22, 2, 2],
[ 8, 9, 10, 11]])
>>> a
array([[ 9, 1, 2, 3],
[ 2, 22, 2, 2],
[ 8, 9, 10, 11]])