1) 答:形状为 (10000,) 的数组是一维数组,而形状为 (10000,1) 的数组是二维数组
import numpy as np
# Array with shape (10000,) is a 1-Dimensional Array
one=np.ones((10,))
print("1-Dimensional Array:\n",one)
#Array with shape (10000,1) is a 2-Dimensional Array.
two=np.ones((10,1))
print("2-Dimensional Array:\n",two)
输出:
1-Dimensional Array:
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
2-Dimensional Array:
[[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]]
2) 答案:
None 只是为数组对象添加一个维度。您可以使用“None”或 numpy 的“newaxis”来创建新维度。
一般提示:您也可以使用 None 代替 np.newaxis
import numpy as np
#### Import the Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
#Original Dimension
print(train_images.shape)
train_images1 = train_images[None,:,:,:]
#Add Dimension using None
print(train_images1.shape)
train_images2 = train_images[np.newaxis is None,:,:,:]
#Add dimension using np.newaxis
print(train_images2.shape)
#np.newaxis and none are same
np.newaxis is None
输出:
(60000, 28, 28)
(1, 60000, 28, 28)
(1, 60000, 28, 28)
True
3) 答案:
axis 参数指定新轴在结果维度中的索引。例如,如果axis=0,它将是第一个维度,如果axis=-1,它将是最后一个维度。
# input array
a = np.array([[ 1, 2, 3], [ -1, -2, -3]] )
print ("1st Input array : \n", a)
b = np.array([[ 4, 5, 6], [ -4, -5, -6]] )
print ("2nd Input array : \n", b)
# Stacking the two arrays along axis 0
out1 = np.stack((a, b), axis = 0)
print ("Output array along axis 0:\n ", out1)
# Stacking the two arrays along axis 1
out2 = np.stack((a, b), axis = 1)
print ("Output array along axis 1:\n ", out2)
# Stacking the two arrays along axis -1
out3 = np.stack((a, b), axis = -1)
print ("Output array along axis -1:\n ", out3)
输出:
1st Input array :
[[ 1 2 3]
[-1 -2 -3]]
2nd Input array :
[[ 4 5 6]
[-4 -5 -6]]
Output array along axis 0:
[[[ 1 2 3]
[-1 -2 -3]]
[[ 4 5 6]
[-4 -5 -6]]]
Output array along axis 1:
[[[ 1 2 3]
[ 4 5 6]]
[[-1 -2 -3]
[-4 -5 -6]]]
Output array along axis -1:
[[[ 1 4]
[ 2 5]
[ 3 6]]
[[-1 -4]
[-2 -5]
[-3 -6]]]