这种numpy选择行为的背后是什么? [英] What is going on behind this numpy selection behavior?

查看:92
本文介绍了这种numpy选择行为的背后是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答此问题,其他人和我实际上认为是错误的,因为认为以下方法可行:

Answering this question, some others and I were actually wrong by considering that the following would work:

说一个有

test = [ [ [0], 1 ],
         [ [1], 1 ]
       ]
import numpy as np
nptest = np.array(test)

背后的原因是什么

>>> nptest[:,0]==[1]
array([False, False], dtype=bool)

一个人拥有

>>> nptest[0,0]==[1],nptest[1,0]==[1]
(False, True)


>>> nptest==[1]
array([[False,  True],
       [False,  True]], dtype=bool)

>>> nptest==1
array([[False,  True],
       [False,  True]], dtype=bool)

是导致尺寸下降的原因是简并性.

Is it the degeneracy in term of dimensions which causes this.

推荐答案

nptest是对象dtype的2D数组,每行的第一个元素是列表.

nptest is a 2D array of object dtype, and the first element of each row is a list.

nptest[:, 0]是对象dtype的一维数组,其每个元素都是列表.

nptest[:, 0] is a 1D array of object dtype, each of whose elements are lists.

当您执行nptest[:,0]==[1]时,NumPy不会对nptest[:,0]的每个元素与列表[1]进行逐元素比较.它从[1]创建尽可能高维的数组,生成一维数组np.array([1]),然后生成

When you do nptest[:,0]==[1], NumPy does not perform an elementwise comparison of each element of nptest[:,0] against the list [1]. It creates as high-dimensional an array as it can from [1], producing the 1D array np.array([1]), and then broadcasts the comparison, comparing each element of nptest[:,0] against the integer 1.

由于nptest[:, 0]中没有列表等于1,因此结果的所有元素均为False.

Since no list in nptest[:, 0] is equal to 1, all elements of the result are False.

这篇关于这种numpy选择行为的背后是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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