numpy

numpy 的各种函数汇总

    • 1.标准数组
    • 2.transpose
    • 3.数组索引
    • 4.

1.标准数组

# ones_like,zeros_like创建与指定数组相似的都是1/0的数组
arr = np.ones((2,2), dtype = 'int16')
print(arr)
arr = np.zeros((2,3))
print(arr)
# empty创建新数组,只分配内存空间不填充任何值
arr = np.empty((2,2))
print(arr)
[[1 1]
 [1 1]]
 
[[0. 0. 0.]
 [0. 0. 0.]]
 
 [[6.89799040e-307 2.55896905e-307]
 [1.61324391e-307 1.37961302e-306]]
# eye,identity创建一个正方的 N*N单位矩阵(对角线为1,其余为0)
arr = np.eye((3))
print(arr)
arr = np.identity((4),dtype = 'float64')
print(arr)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
 
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

2.transpose

二维数组

arr = np.arange(6).reshape((2, 3))
print(arr)
# 三个效果一致
print(arr.swapaxes(0, 1))  # x, y
print(arr.transpose())
print(arr.T)
[[0 1 2]
 [3 4 5]]
 #
[[0 3]
 [1 4]
 [2 5]]
 #
[[0 3]
 [1 4]
 [2 5]]
 #
[[0 3]
 [1 4]
 [2 5]]

三维数组

arr = np.arange(16).reshape((2, 2, 4))
print(arr)
# 一样
print(arr.swapaxes(0, 1))  # z, x, y --> z x 换
print(arr.transpose(1, 0, 2)) # 0 1 2 --> z x y
[[[ 0  1  2  3]
  [ 4  5  6  7]]

 [[ 8  9 10 11]
  [12 13 14 15]]]
  #
[[[ 0  1  2  3]
  [ 8  9 10 11]]

 [[ 4  5  6  7]
  [12 13 14 15]]]
  #
[[[ 0  1  2  3]
  [ 8  9 10 11]]

 [[ 4  5  6  7]
  [12 13 14 15]]]

3.数组索引

arr = np.arange(15).reshape((3,5))
print(arr.sum(axis=0))                # sum of each column
print(arr.min(axis=1))                  # min of each row
print(arr.cumsum(axis=1))         # cumulative sum along each row
[15 18 21 24 27]

[ 0  5 10]

[[ 0  1  3  6 10]
 [ 5 11 18 26 35]
 [10 21 33 46 60]]
a = np.random.randn(3,2)
print(a)
print(np.hstack((arr,a)))
b = np.random.randint(10, size=(2,5))
print(b)
print(np.vstack((arr,b)))
[[ 1.54110668  2.11057107]
 [ 0.12192709 -0.41017484]
 [-0.0910205  -0.89374915]]
 
[[ 0.     1.     2.     3.     4.     1.54110668   2.11057107]
 [ 5.     6.     7.     8.     9.     0.12192709  -0.41017484]
 [10.    11.    12.    13.    14.    -0.0910205  -0.89374915]]
  
[[0 9 9 5 0]
 [0 0 1 9 1]]
 
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [ 0  9  9  5  0]
 [ 0  0  1  9  1]]
c = np.concatenate((arr,a),axis=1)        # hstack效果一样  水平拼接
print(c)
d = np.concatenate((arr,b),axis=0)        # vstack 效果一样  垂直拼接
print(d)
[[ 0.     1.     2.     3.     4.     1.54110668   2.11057107]
 [ 5.     6.     7.     8.     9.     0.12192709  -0.41017484]
 [10.    11.    12.    13.    14.    -0.0910205  -0.89374915]]
  
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [ 0  9  9  5  0]
 [ 0  0  1  9  1]]

4.

。。

你可能感兴趣的:(numpy,numpy,python,机器学习,tensorflow,神经网络)