在利用tensorflow做图像的时候经常遇到numpy.expand_dims
所以今天自己运行一下,希望各位也能理解,好了直接代码测试:
测试一维的数据
(1)源数据:
x = np.array([1,2,3])
print (x)
print (x.shape)
输出:
[1 2 3]
(3,)
(2)axis=0
import numpy as np
x = np.array([1,2,3])
y = np.expand_dims(x,axis=0)
print(y)
print("y.shape: ",y.shape)
print("y[0][1]: ",y[0][1])
输出:
(3)axis=1
import numpy as np
x = np.array([1,2,3])
y = np.expand_dims(x,axis=1)
print(y)
print("y.shape: ",y.shape)
print("y[0][1]: ",y[1][0])
输出:
(4)axis=3
import numpy as np
x = np.array([1,2,3])
y = np.expand_dims(x,axis=3)
print(y)
print("y.shape: ",y.shape)
print("y[0][1]: ",y[2][0])
输出:
测试二维的数据
(1)源数据:
x = np.array([[1,2,3],[4,5,6]])
print (x)
print (x.shape)
输出:
[[1 2 3]
[4 5 6]]
(2, 3)
(2)axis=0
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
y = np.expand_dims(x,axis=0)
print(y)
print("y.shape: ",y.shape)
print("y[0][1]: ",y[0][1])
输出:
(3)axis=1
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
y = np.expand_dims(x,axis=1)
print(y)
print("y.shape: ",y.shape)
print("y[0][1]: ",y[1][0])
输出: