Reputation: 21
I have a spectrogram. It's 129 rows x n columns. I want to "cut" the columns to 20. And I would do something like this:
if spectrogram.shape[1] > 20:
for row in spectrogram:
i = spectrogram.index(row)
row = row[:20]
spectrogram[i] = row
But it throws out an error using .index()
, so I tried using .where()
as I saw here on SOF but another error occurred:
AttributeError: 'numpy.ndarray' object has no attribute 'where'
How should I do?
Upvotes: 0
Views: 109
Reputation: 92461
You should be able to just take the slice you want without the loop (whenever you're tempted to loop over a numpy array, there's usually a better way).
spectrogram[:, :20]
Here's a simplified example: given a 5x10 array, take just the first 5 of each row giving you a 5x5 array:
import numpy as np
a = np.arange(50).reshape(5, 10)
a[:, :5]
result
array([
[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[20, 21, 22, 23, 24],
[30, 31, 32, 33, 34],
[40, 41, 42, 43, 44]])
Upvotes: 1