如何更改 python 中数组的维度?
how to change a dimension of array in python?
我是 python 的新手。我对数组的维度有疑问。
我有 (10,192,192,1) 个类型为 (class 'numpy.ndarray') 的数组。
我想将这个数组分成 10 个分隔的数组,如 10 * (1,192,192,1)。但是当我分开时我总是得到 (192,192,1) 数组。
如何获得与原始数组相同维度类型的分离数组?
下面是我的代码。
b = np.ndarray((a.shape[0],a.shape[1],a.shape[2],a.shape[3]))
print(b.shape) # (10,192,192,1)
for i in range(a.shape[0]):
b[i] = a[i]
print(b[i].shape) # (192,192,1), but I want to get (1,192,192,1)
只需使用 numpy .reshape()
函数:
b[i].reshape((1,192,192,1))
查看文档 here
例如:
>>> x = np.zeros((13,24))
>>> x.shape
(13,24)
>>> x.resize((1,13,24)).shape
(1,13,24)
您可以使用 np.array()
重塑它
b = np.zeros((192,192,1))
print(b.shape) #(192, 192, 1)
print(np.array([b]).shape) #(1, 192, 192, 1)
我是 python 的新手。我对数组的维度有疑问。
我有 (10,192,192,1) 个类型为 (class 'numpy.ndarray') 的数组。 我想将这个数组分成 10 个分隔的数组,如 10 * (1,192,192,1)。但是当我分开时我总是得到 (192,192,1) 数组。 如何获得与原始数组相同维度类型的分离数组?
下面是我的代码。
b = np.ndarray((a.shape[0],a.shape[1],a.shape[2],a.shape[3]))
print(b.shape) # (10,192,192,1)
for i in range(a.shape[0]):
b[i] = a[i]
print(b[i].shape) # (192,192,1), but I want to get (1,192,192,1)
只需使用 numpy .reshape()
函数:
b[i].reshape((1,192,192,1))
查看文档 here
例如:
>>> x = np.zeros((13,24))
>>> x.shape
(13,24)
>>> x.resize((1,13,24)).shape
(1,13,24)
您可以使用 np.array()
b = np.zeros((192,192,1))
print(b.shape) #(192, 192, 1)
print(np.array([b]).shape) #(1, 192, 192, 1)