获取numpy数组中数组的索引

2024-09-26 18:09:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有光谱图。它是129行x n列。 我想把柱子“剪”成20根。我会这样做:

if spectrogram.shape[1] > 20:
                  for row in spectrogram:
                    i = spectrogram.index(row)
                    row = row[:20]
                    spectrogram[i] = row 

但是它抛出了一个使用.index()的错误,所以我尝试使用.where(),正如我在SOF上看到的那样,但是出现了另一个错误:

AttributeError: 'numpy.ndarray' object has no attribute 'where'

我该怎么办?你知道吗


Tags: innumpyforindexif错误光谱where
1条回答
网友
1楼 · 发布于 2024-09-26 18:09:22

您应该能够在没有循环的情况下获取所需的切片(每当您试图在numpy数组上循环时,通常有更好的方法)。你知道吗

spectrogram[:, :20]

下面是一个简化的示例:给定一个5x10数组,只取每行的前5个,就得到一个5x5数组:

import numpy as np
a = np.arange(50).reshape(5, 10)
a[:, :5]

结果

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]])

相关问题 更多 >

    热门问题