Issue
I have a 2D NumPy array of shape (20,87)
, now I want to append multiple arrays of such type such that after appending let's say 100 of those my final shape would be (100,20,87)
.
I tried normal appending by using numpy.append()
but it did not work.
Edit: I have attached the link to my colab notebook.
This is my code:
audio_sr = []
x_train = []
y_train = []
x_test = []
y_test = []
for data in path:
ipd.Audio(data)
i = 0
label = 0
while i <= 120 :
audio, sr = librosa.load(data, offset=i, duration=2)
audio_sr.append((audio, sr))
i += 2
label += 1
all_mel_db = []
for audio, sr in audio_sr:
mel_spec = librosa.feature.melspectrogram(audio, sr = sr, n_mels = 20)
mel_db = librosa.power_to_db(mel_spec, ref = np.max)
all_mel_db.append(mel_db)
x_train = []
x_test = np.array(x_test)
x_train = np.dstack(all_mel_db).reshape((len(all_mel_db), 20, 87))
And I got this error
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 87 and the array at index 121 has size 66
on using np.append() after reshaping, I got
for mel_db in all_mel_db:
mel_db = np.reshape(1, mel_db.shape[0], mel_db.shape[1])
x_train = np.append(x_train, mel_db)
Error:
Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead
And on simply converting to a NumPy array:
x_train = np.array(all_mel_db)
I got:
ValueError: could not broadcast input array from shape (20,87) into shape (20)
Solution
You can use the numpy.dstack
to produce output of shape (20, 87, N)
by:
a = np.random.rand(20, 87)
b = np.random.rand(20, 87)
c = np.random.rand(20, 87)
d = np.random.rand(20, 87)
collection = np.dstack([a, b, c, d])
collection.shape # out: (20, 87, 4)
collection = np.dstack([a, b, c, d]).reshape((4, 20, 87))
collection.shape # out: (4, 20, 87)
Cheers.
Answered By - Michael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.