【问题标题】:ValueError: x and y must have same first dimension, but have shapes (165,) and (166,)ValueError:x 和 y 必须具有相同的第一维,但具有形状 (165,) 和 (166,)
【发布时间】:2022-01-13 10:57:58
【问题描述】:

我正在遍历音频文件名列表,加载它们,计算 STE 和 RMSE,然后绘制所述值。但是,大约 20% 的文件会偶尔出现错误消息。

ValueError: x and y must have same first dimension, but have shapes (165,) and (166,)
ValueError: x and y must have same first dimension, but have shapes (240,) and (241,)

以此类推。

我查看了this question,我相信问题可能类似,因为我在下面代码的第 2 行对plt.plot 的调用中也使用了方括号。但是,将其更改为括号会引发语法错误。我也觉得奇怪的是,>1000 个样本中只有 20% 受到影响,并且形状似乎增加了 1。

这是元数据问题吗?究竟是什么导致了这个问题?

frames = range(len(energy))
t = librosa.frames_to_time(frames, sr=SAMPLE_RATE, hop_length=HOP_LENGTH)

plt.figure(figsize=(15, 5))
plt.plot(t[:len(rmse)], rmse_max_scaled, color='b')
plt.plot(t, energy_max_scaled, 'r--')
librosa.display.waveplot(sample, sr=SAMPLE_RATE, alpha=0.4)
plt.legend(('RMSE', 'Energy'))

【问题讨论】:

  • @JohanC 我已经用t 值的定义编辑了我的问题,该值是从帧计算的时间。我不确定您所说的一对一通信是什么意思。我尝试了您的建议,但错误仍然存​​在。同样使用 plt.plot(t, rmse_max_scaled, color='b')plt.plot(t, energy_max_scaled, 'r--') 可以在有效的样本上获得相同的图表,但 20% 仍然会抛出上述错误。
  • 你的问题是没有足够的t 值。您需要与对应的 y 值一样多的 t 值。所以例如frames = range(len(rmse)) 第一个情节。第二个情节frames = range(len(energy_max_scaled))rmse_max_scaledenergy_max_scaled 的长度不同,这有点可疑。
  • @JohanC 感谢您清理问题。我能够编写解决方案。

标签: python python-3.x matplotlib plot librosa


【解决方案1】:

@JohanC 指出的问题是没有足够的t 值作为对应的y 值。重新格式化代码如下解决了这个问题。

plt.figure(figsize=(15, 5))

frames = range(len(rmse))
t = librosa.frames_to_time(frames, sr=SAMPLE_RATE, hop_length=HOP_LENGTH)
plt.plot(t[:len(rmse)], rmse_max_scaled, color='b')

frames = range(len(energy))
t = librosa.frames_to_time(frames, sr=SAMPLE_RATE, hop_length=HOP_LENGTH)
plt.plot(t[:len(energy)], energy_max_scaled, 'r--')

librosa.display.waveplot(sample, sr=SAMPLE_RATE, alpha=0.4)
plt.legend(('RMSE', 'Energy'))

【讨论】:

    【解决方案2】:

    这是因为 x 和 y 的长度相同,并且没有足够的 t 值。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2021-06-30
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多