shape [0]和shape [1]在python中做什么?

时间:2018-07-21 12:09:11

标签: python-3.x

在python shape [0]中返回尺寸,但是在此代码中它返回集合的总数。有人可以告诉我shape [0]和shape [1]的工作吗?

代码:

m_train = train_set_x_orig.shape[0]

m_test = test_set_x_orig.shape[0]

num_px = train_set_x_orig.shape[1]

print ("Number of training examples: m_train = " + str(m_train))

print ("Number of testing examples: m_test = " + str(m_test))

print ("Height/Width of each image: num_px = " + str(num_px))

print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")

print ("train_set_x shape: " + str(train_set_x_orig.shape))

print ("train_set_y shape: " + str(train_set_y.shape))

print ("test_set_x shape: " + str(test_set_x_orig.shape))

print ("test_set_y shape: " + str(test_set_y.shape))

输出:

Number of training examples: m_train = 209

Number of testing examples: m_test = 50

Height/Width of each image: num_px = 64

Each image is of size: (64, 64, 3)

train_set_x shape: (209, 64, 64, 3)

train_set_y shape: (1, 209)

test_set_x shape: (50, 64, 64, 3)

test_set_y shape: (1, 50)

1 个答案:

答案 0 :(得分:1)

这在计算机视觉中非常普遍,第一维是示例数,第二维和第三维提供示例数据。例如,在计算机视觉的情况下,很常见的是具有形状(x,y)的一组n个图像。在这种情况下,您的训练集将为(n,x,y)形状。数据中的第四维是通道数(在这种情况下为3,即RGB)。

在您的数据集中,每张图像的高度和宽度相同,因此仅可以通过第三行来检索图像的大小: num_px = train_set_x_orig.shape [1]