【问题标题】:ValueError: cannot reshape array of size 0ValueError:无法重塑大小为 0 的数组
【发布时间】:2022-01-08 04:08:35
【问题描述】:

如果我在我的数据集上调用此函数:

def reconstruct_flight(data, sequence_lenght, flight_len, param_len):
    
    stack_factor = int(flight_len/sequence_lenght)
    
    data_reconstructed = []
    for i in range(0, len(data_clean), stack_factor):
        if i<len(data_clean):
            data_reconstructed.append(
                data[i:i+stack_factor].reshape(
                    [flight_len, param_len])
                )
        
    return np.array(data_reconstructed)

我收到以下错误:

ValueError: cannot reshape array of size 0 into shape (1500,77)

但如果我在控制台中运行 for 循环而不将其作为函数传递:

data_reconstructed = []
    for i in range(0, len(data_clean), stack_factor):
        if i<len(data_clean):
            data_reconstructed.append(
                data[i:i+stack_factor].reshape(
                    [flight_len, param_len])
                )

它按预期工作。这是为什么呢?

【问题讨论】:

  • data_clean 定义在哪里?
  • 如果 data 已经是一个 numpy 数组,你应该在 numpy 中重塑(可能先裁剪)而不是创建列表然后转换为数组
  • @Learningisamess 'data_clean' 是一个形状为 (300, 50, 77) 的 numpy 数组,reshape 是 (10, 1500, 77)。您能否详细说明如何在不使用列表而仅使用重塑的情况下将这个元素堆叠在一起?

标签: python-3.x numpy numpy-ndarray


【解决方案1】:

在重塑时,如果您保持相同的数据连续性并且只是重塑框,则可以使用重塑数据

data_reconstructed = data_clean.reshape((10,1500,77))

如果您要将连续性从一个轴更改为另一个轴,则需要事先添加轴的排列https://numpy.org/doc/stable/reference/generated/numpy.transpose.html

【讨论】:

  • 是否使用这种方法维护了数据的顺序?就我而言,我需要将 data_clean 的每 30 个元素堆叠在一起,因为它们属于同一类别。
  • 此数据是自动编码器的输出,我将完整序列 (1500) 分解为 30 个较小的序列 (50),以避免在模型中存储太多信息。但是为了比较结果,我需要通过尊重顺序来重建原始序列。
  • 这就是我想要达到的目标。通过查看您的示例,并假设您的 data_clean 数组是 C 顺序(而不是 Fortran 顺序),您不会更改连续性,因此您只需要重塑。
猜你喜欢
  • 2020-10-15
  • 2021-08-02
  • 2021-03-20
  • 2021-03-21
  • 2018-10-22
  • 2019-11-29
  • 2020-01-29
  • 2021-07-18
相关资源
最近更新 更多