【问题标题】:Python: Array indexing all the values in first index with LibrosaPython:使用 Librosa 对第一个索引中的所有值进行数组索引
【发布时间】:2021-02-11 10:22:56
【问题描述】:

我正在尝试获取数组的第一个值,但是,当我尝试打印 bandwidth 第一个数组值时,它会在第一个索引中显示数组的所有值,而不是只是第一个值本身。

如果我尝试打印数组的第二个索引,则会出现错误:

index 1 is out of bounds for axis 0 with size 1

所以通过使用下面的代码打印第一个值:

import librosa
import librosa.display
import numpy as np

y, sr = librosa.load(filename)
onset_env = librosa.onset.onset_strength(y, sr=sr)
tempo = librosa.beat.tempo(onset_env, sr=sr)
sampleRate = librosa.get_samplerate(filename)
durationValue = librosa.get_duration(y, sr=sr)
duration = round(durationValue, 2)
bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)

print("Tempo: " + str(int(tempo)))
print("Sample Rate: " + str(int(sampleRate)))
print("Duration: " + str(duration))
print("Bandwidth: " + str(bandwidth[0]))

它为我提供了以下包含所有值的打印日志(而不仅仅是第一个值本身):

Tempo: 135
Sample Rate: 44100
Duration: 58.99
Bandwidth: [1696.55727753 1607.42642476 1626.93942805 ...  585.00089368 1148.95518802
3082.65000084]

【问题讨论】:

    标签: python arrays numpy librosa


    【解决方案1】:

    我发现我遇到的问题是一个 Numpy 数组,所以我想在第二个值上指定索引值,所以我改变了:

    print("Bandwidth: " + str(bandwidth[0]))
    

    print("Bandwidth: " + str(bandwidth[0][0]))
    

    打印日志返回一个奇异值的对象:

    Tempo: 135
    Sample Rate: 44100
    Duration: 58.99
    Bandwidth: 1626.93942804681
    

    我仍在学习 Numpy 数组,但据我了解(这很可能是错误的)是我需要先选择哪个数组,然后从该选定数组中选择哪个索引值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-17
      • 2020-01-07
      • 2020-03-29
      • 2019-05-05
      • 2022-01-14
      • 2013-07-01
      相关资源
      最近更新 更多