国米preting numpy.where结果 [英] Interpreting numpy.where results

查看:195
本文介绍了国米preting numpy.where结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对什么 numpy.where 的结果意味着困惑,以及如何用它来索引到一个数组。

I'm confused by what the results of numpy.where mean, and how to use it to index into an array.

有一个看看下面的code样品:<​​/ P>

Have a look at the code sample below:

import numpy as np
a = np.random.randn(10,10,2)
indices = np.where(a[:,:,0] > 0.5)

我期待指数数组为2 - 暗淡包含where条件为真指数。我们可以看到,

I expect the indices array to be 2-dim and contain the indices where the condition is true. We can see that by

indices = np.array(indices)
indices.shape  # (2,120)

因此​​,它看起来像指数充当某种扁平阵列上,但我不能够准确找出如何。更令人混淆,

So it looks like indices is acting on the flattened array of some sort, but I'm not able to figure out exactly how. More confusingly,

a.shape  # (20,20,2)
a[indices].shape # (2,120,20,2)

问:

如何索引我的阵列 np.where 的实际输出增长的该数组的大小?这里发生了什么?

How does indexing my array with the output of np.where actually grow the size of the array? What is going on here?

推荐答案

您是在一个错误的假设基础的分度: np.where 返回一些可以立刻使用对于高级索引(这是 np.ndarrays元组)。但是你把它转换为numpy的数组(所以它现在是一个 np.ndarray np.ndarrays )。

You are basing your indexing on a wrong assumption: np.where returns something that can be immediatly used for advanced indexing (it's a tuple of np.ndarrays). But you convert it to a numpy array (so it's now a np.ndarray of np.ndarrays).

所以

import numpy as np
a = np.random.randn(10,10,2)
indices = np.where(a[:,:,0] > 0.5)
a[:,:,0][indices] 
# If you do a[indices] the result would be different, I'm not sure what
# you intended.

让你由 np.where 中的元素。如果转换指数 np.array 它会触发索引的另一种形式(的看到numpy的文档),并在警告消息本节 文档 变得非常重要。这就是为什么它会增加你的阵列的总大小的原因。

gives you the elements that are found by np.where. If you convert indices to a np.array it triggers another form of indexing (see this section of the numpy docs) and the warning message in the docs gets very important. That's the reason why it increases the total size of your array.

什么 np.where 意味着一些额外的信息:你得到包含 N 数组的元组。 N 是输入数组的维数。因此,满足条件的第一个元素的索引 [0] [0],[1] [0],... [N] [0] ,而不是 [0] [0],[0] [1],... [0] [N] 。所以你的情况,你有(2,120),这意味着你有2个尺寸和120点发现

Some additional information about what np.where means: You get a tuple containing n arrays. n is the number of dimensions of the input array. So the first element that satisfies the condition has index [0][0], [1][0], ... [n][0] and not [0][0], [0][1], ... [0][n]. So in your case you have (2, 120) meaning you have 2 dimensions and 120 found points.

这篇关于国米preting numpy.where结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆