import numpy as np
# Assuming you have a NumPy array of shape (50, 100, 100)
array = np.random.random((50, 100, 100))
# Flatten the last two dimensions
flattened_array = array.reshape((array.shape[0], -1))
# Print the shape of the flattened array
print(flattened_array.shape)
2条答案
按热度按时间igsr9ssn1#
可以使用
reshape
代替flatten
。要使用它,您需要传递希望数组变成的形状。你知道你希望第一个维度是50,你可以传递-1作为第二个维度,这告诉numpy找出这个值应该是什么。字符串
ql3eal8s2#
要展平NumPy数组的最后两个维度,可以使用reshape函数。
字符串
在上面的代码中,array是原始的NumPy数组,形状为(50,100,100)。通过使用reshape函数,我们将新形状指定为(array.shape[0],-1),其中-1表示NumPy应根据剩余维度自动计算展开维度的适当大小