微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python中多维矩阵的多维索引

如何解决Python中多维矩阵的多维索引

我想使用多维索引矩阵来访问另一个多维矩阵。 我的问题是,由于广播问题(形状不匹配),诸如np.newaxis之类的方法无法正常工作。

我的数据矩阵的形状为(5001、3、240、16)。

import numpy as np

# n_examples,n_channels,n_pictures,n_Meta_information
data = np.ones((5001,3,240,16))

# select randomly 32 examples
batch_size = 32
possible_indices = np.arange(5001,dtype=np.int)
random_example_indices = np.random.choice(possible_indices,size=batch_size,replace=True)


# select all three channels
n_channels = 3
channel_indices = np.arange(n_channels)
#channel_indices = np.expand_dims(channel_indices,axis=0)
#channel_indices = np.repeat(channel_indices,batch_size,axis=0)

final_pictures_indices = []
for random_sample_idx in range(batch_size):
    # select a random start index and take 120 successive indices
    # is the same for all three channels
    start_index = np.random.randint(0,max(1,240 - 120 + 1))
    end_index = start_index + 120
    pictures_indices = np.arange(start_index,end_index,dtype=np.int)
    final_pictures_indices.append(pictures_indices)

# batch_size x n_pictures
final_pictures_indices = np.array(final_pictures_indices)


# should have the shape: (32,120,16)
result = data[random_example_indices[:,np.newaxis],channel_indices,final_pictures_indices].shape
print(result)

不幸的是,我遇到了以下错误

result = data[random_example_indices[:,final_pictures_indices].shape
IndexError: shape mismatch: indexing arrays Could not be broadcast together with shapes (32,1) (3,) (32,120)

我还尝试将所有索引信息融合到一个矩阵中,但是我遇到的问题是无法堆叠具有不同形状的矩阵。

感谢您的每一个提示

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。