np.delete和np.s_。 np_s有什么特别之处? [英] np.delete and np.s_. What's so special about np_s?

查看:593
本文介绍了np.delete和np.s_。 np_s有什么特别之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白为什么常规索引不能用于np.delete。是什么让np.s_如此特别?

I don't really understand why regular indexing can't be used for np.delete. What makes np.s_ so special?

例如使用此代码,用于删除此数组的某些行..

For example with this code, used to delete the some of the rows of this array..

inlet_names = np.delete(inlet_names, np.s_[1:9], axis = 0)

为什么我不能简单地使用常规索引并且做.. ..

Why can't I simply use regular indexing and do..

inlet_names = np.delete(inlet_names, [1:9], axis = 0)

inlet_names = np.delete(inlet_names, inlet_names[1:9], axis = 0)

从我可以收集的内容来看,np.s_与np.index_exp相同,除了它不返回元组,但两者都可以在Python代码中的任何地方使用。

From what I can gather, np.s_ is the same as np.index_exp except it doesn't return a tuple, but both can be used anywhere in Python code.

然后当我查看np.delete函数时,它表示你可以使用类似 [1,2,3] 删除整个数组中的那些特定索引。那么什么阻止我使用类似的东西删除数组中的某些行或列?

Then when I look into the np.delete function, it indicates that you can use something like [1,2,3] to delete those specific indexes along the entire array. So whats preventing me from using something similar to delete certain rows or columns from the array?

我只是假设这种类型的索引在np中被读取为其他内容.delete所以你需要使用np.s_来指定,但是我无法深入了解它究竟是什么,因为当我尝试第二段代码时它只返回无效语法。这很奇怪,因为这段代码有效......

I'm simply assuming that this type of indexing is read as something else in np.delete so you need to use np.s_ in order to specify, but I can't get to the bottom of what exactly it would be reading it as because when I try the second piece of code it simply returns "invalid syntax". Which is weird because this code works...

inlet_names = np.delete(inlet_names, [1,2,3,4,5,6,7,8,9], axis = 0)   

所以我猜答案可能是np.delete只接受您要删除的索引列表。并且np._s返回您为切片指定的索引列表。

So I guess the answer could possibly be that np.delete only accepts a list of the indexes that you would like to delete. And that np._s returns a list of the indexes that you specify for the slice.

可以对我刚才所说的关于函数的任何内容使用一些澄清和一些更正可能是错的,因为很多这只是我的看法,文件并没有完全解释我试图理解的一切。我想我只是在思考这个,但我想真正理解它,如果有人可以解释它。

Just could use some clarification and some corrections on anything I just said about the functions that may be wrong, because a lot of this is just my take, the documents don't exactly explain everything that I was trying to understand. I think I'm just overthinking this, but I would like to actually understand it, if someone could explain it.

推荐答案

np.delete 没有做任何独特或特殊的事情。它只返回原始数组的副本,但缺少一些项目。大多数代码只是解释输入以准备制作此副本。

np.delete is not doing anything unique or special. It just returns a copy of the original array with some items missing. Most of the code just interprets the inputs in preparation to make this copy.

您要问的是 obj 参数


obj:slice,int或整数数组

obj : slice, int or array of ints

简单来说, np.s _ 允许您使用熟悉的语法提供切片。 x:y 表示法不能用作函数参数。

In simple terms, np.s_ lets you supply a slice using the familiar : syntax. The x:y notation cannot be used as a function parameter.

让我们尝试你的选择(你提到这些在结果和错误,但他们埋在文本中):

Let's try your alternatives (you allude to these in results and errors, but they are buried in the text):

In [213]: x=np.arange(10)*2   # some distinctive values

In [214]: np.delete(x, np.s_[3:6])
Out[214]: array([ 0,  2,  4, 12, 14, 16, 18])

所以删除 with s _ 删除一系列值,即 6 8 10 ,第3到第5个。

So delete with s_ removes a range of values, namely 6 8 10, the 3rd through 5th ones.

In [215]: np.delete(x, [3:6])
  File "<ipython-input-215-0a5bf5cc05ba>", line 1
    np.delete(x, [3:6])
                   ^
SyntaxError: invalid syntax

为什么会出错?因为 [3:4] 是一个索引表达式。 np.delete 是一个函数。即使 s _ [[3:4]] 也有问题。 np.delete(x,3:6)也不好,因为Python只接受索引中的语法上下文,它自动将其转换为 slice 对象。请注意,这是一个语法错误,这是解释程序在进行任何计算或函数调用之前捕获的内容。

Why the error? Because [3:4] is an indexing expression. np.delete is a function. Even s_[[3:4]] has problems. np.delete(x, 3:6) is also bad, because Python only accepts the : syntax in an indexing context, where it automatically translates it into a slice object. Note that is is a syntax error, something that the interpreter catches before doing any calculations or function calls.

In [216]: np.delete(x, slice(3,6))
Out[216]: array([ 0,  2,  4, 12, 14, 16, 18])

切片可以代替取值_ ;实际上这就是 s _ 生成

A slice works instead of s_; in fact that is what s_ produces

In [233]: np.delete(x, [3,4,5])
Out[233]: array([ 0,  2,  4, 12, 14, 16, 18])

列表也有效,但它的工作方式不同(见下文)。

A list also works, though it works in different way (see below).

In [217]: np.delete(x, x[3:6])
Out[217]: array([ 0,  2,  4,  6,  8, 10, 14, 18])

这样可行,但产生的结果不同,因为 x [3:6] 范围(3,6)不同。 np.delete 也不像列表删除那样工作。它按索引删除,而不是通过匹配值删除。

This works, but produces are different result, because x[3:6] is not the same as range(3,6). Also the np.delete does not work like the list delete. It deletes by index, not by matching value.

np.index_exp 失败的原因与<$ c相同$ c> np.delete(x,(slice(3,6),)) 1 [1] (1,)是全部有效并删除一个项目。即使'1',该字符串也可以使用。 delete 解析此参数,并且在此级别,期望可以转换为整数的内容。 obj.astype(INTP)(切片(无),)不是切片,它是1项元组。所以它在 delete 代码的不同位置处理。这是 TypeError delete 调用产生的,与 SyntaxError 。理论上 delete 可以从元组中提取切片并按 s _ 情况继续,但开发人员没有选择考虑这种变化。

np.index_exp fails for the same reason that np.delete(x, (slice(3,6),)) does. 1, [1], (1,) are all valid and remove one item. Even '1', the string, works. delete parses this argument, and at this level, expects something that can be turned into an integer. obj.astype(intp). (slice(None),) is not a slice, it is a 1 item tuple. So it's handled in a different spot in the delete code. This is TypeError produced by something that delete calls, very different from the SyntaxError. In theory delete could extract the slice from the tuple and proceed as in the s_ case, but the developers did not choose to consider this variation.

对代码的快速研究表明, np.delete 使用2种不同的复制方法 - 通过切片和布尔掩码。如果 obj 是一个切片,就像在我们的例子中那样(对于1d数组):

A quick study of the code shows that np.delete uses 2 distinct copying methods - by slice and by boolean mask. If the obj is a slice, as in our example, it does (for 1d array):

out = np.empty(7)
out[0:3] = x[0:3]
out[3:7] = x[6:10]

但是 [3,4,5] (而不是切片)它确实:

But with [3,4,5] (instead of the slice) it does:

keep = np.ones((10,), dtype=bool)
keep[[3,4,5]] = False
return x[keep]

结果相同,但采用不同的构造方法。 x [np.array([1,1,1,0,0,0,1,1,1,1],bool)] 做同样的事情。

Same result, but with a different construction method. x[np.array([1,1,1,0,0,0,1,1,1,1],bool)] does the same thing.

事实上,像这样的布尔索引或屏蔽比 np.delete 更常见,并且通常同样强大。

In fact boolean indexing or masking like this is more common than np.delete, and generally just as powerful.

来自 lib / index_tricks.py 源文件:

index_exp = IndexExpression(maketuple=True)
s_ = IndexExpression(maketuple=False)

它们是同一件事的略有不同的版本。两者都只是便利功能。

They are slighly different versions of the same thing. And both are just convenience functions.

In [196]: np.s_[1:4]
Out[196]: slice(1, 4, None)
In [197]: np.index_exp[1:4]
Out[197]: (slice(1, 4, None),)
In [198]: np.s_[1:4, 5:10]
Out[198]: (slice(1, 4, None), slice(5, 10, None))
In [199]: np.index_exp[1:4, 5:10]
Out[199]: (slice(1, 4, None), slice(5, 10, None))

仅当存在单个项目,切片或索引时, maketuple 业务才适用。

The maketuple business applies only when there is a single item, a slice or index.

这篇关于np.delete和np.s_。 np_s有什么特别之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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