0

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?

4
  • Seems like you need basic indexing, here are the docs Commented Jun 18, 2019 at 7:33
  • when it comes to numpy, you have much better choices than iteration. in this case, you can slice like spectrogram[:, :20] Commented Jun 18, 2019 at 7:33
  • For where do it this way: 'numpy.where(array==item)' Commented Jun 18, 2019 at 7:33
  • Thank you guys, gonna accept the answer in the bottom. But your works too :) Commented Jun 18, 2019 at 7:37

1 Answer 1

1

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]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.