1.建立矩陣
b = np.array([, , ])
print b
----------
c = np.array([[1, 2, 3], [1, 2, 3]])
#O: [[1 2 3]
[1 2 3]]
----------
b = np.arange()
print b
#O: [ ]
----------
b = np.arange().reshape(,)
print b
#O: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
----------
b = np.eye(,)
print b
#O: [[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
----------
c = np.eye(,)
print c
#O [[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]]
2.矩陣次元
import numpy as np
b = np.arange().reshape(, )
print b
print b.shape
#O [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
(L, L)
3.求和,極值
b = np.arange().reshape(, )
print b.sum()
#O 66
print b.max()
#O 11
print b.min()
#O 0
print b.mean()
#O 5.5
test1 = np.array([[, , ],
[, , ],
[, , ]])
#行求和
test1.sum(axis=)
# 輸出 array([30, 75, 120])
#列求和
test1.sum(axis=)
# 輸出 array([60, 75, 90])
4.數組乘法
a = np.array([[1, 2],
[3, 4]])
b = np.array([[5, 6],
[7, 8]])
#按元素相乘 elementwise
print a*b
#輸出 [[ 5 12]
[21 32]]
#矩陣乘法
print a.dot(b)
#輸出 [[19 22]
[43 50]]
5.元素運算
a = np.arange()
print a
print a** #square
print np.exp(a) #power of E
print np.sqrt(a) #root
print np.floor(np.sqrt(a)) #round
#OUT [0 1 2 3]
[ ]
[ ]
[ ]
[ ]
6.轉置
a = np.arange().reshape(, )
b = a.T
print a
print b
#OUT [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
7.數組删除指定,行列
z= np.arange().reshape(, )
print z
z = np.delete(z, np.s_[:],axis = )
print z
#OUT [[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
[[6 7]
[8 9]]
z = np.delete(z, np.s_[,],axis = )
print z
#OUT [[2 3]
[6 7]
[8 9]]
z= np.arange().reshape(, )
print z
z = np.delete(z, np.s_[:],axis = )
print z
#OUT [[0 1 2 3 4]
[5 6 7 8 9]]
[[2 3 4]
[7 8 9]]
8.矩陣拼接
import numpy as np
a = [[1, 2, 3],
[1, 2, 3]]
b = [[4,4,4],
[4,4,4]]
c = np.row_stack((a, b))
print c
c = np.column_stack((a, b))
print c
#OUT [[1 2 3]
[1 2 3]
[4 4 4]
[4 4 4]]
[[1 2 3 4 4 4]
[1 2 3 4 4 4]]