np.delete np.s_。np_s有什么特别之处?

时间:2022-02-06 23:07:04

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

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

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)

or

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

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。s_和np是一样的。index_exp除了它不返回元组之外,但在Python代码的任何地方都可以使用它们。

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函数时,它指示您可以使用[1,2,3]之类的方法来删除整个数组中的特定索引。那么是什么阻止我使用类似于从数组中删除某些行或列的东西呢?

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...

我只是假设这种类型的索引在np.delete中被读取为其他内容,所以你需要使用np。s_是为了指定,但我无法理解它到底是什么,因为当我尝试第二段代码时,它只返回“无效语法”。这很奇怪,因为这个代码可以工作……

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

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.

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

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.

只要对我刚才说的函数做一些澄清和修正就可以了,因为很多都是我的观点,这些文档并不能解释我想要理解的所有东西。我想我只是想得太多了,但是我想要真正理解它,如果有人能解释的话。

1 个解决方案

#1


8  

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.

删除不是做任何独特或特别的事情。它只返回原始数组的一个副本,其中缺少一些项。大多数代码只是对输入进行解释,以便进行复制。

What you are asking about is the obj parameter

您要问的是obj参数

obj : slice, int or array of ints

块:块、int或int的数组

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.

简而言之,np。s_允许您使用熟悉的:语法提供一个切片。x:y表示法不能用作函数参数。

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

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

因此,使用s_删除将删除一系列值,即6 8 10,3到5。

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

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.

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

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

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

片代替s_起作用;事实上,这就是s_生产的

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

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.

这是可行的,但产生的结果是不同的,因为x[3:6]与range(3,6)不一样。而且,np.delete与list delete不一样。它通过索引而不是匹配值来删除。

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。index_exp失败的原因与np.delete(x, slice(3,6))一样。1,[1],(1,)都是有效的,并删除一个项目。甚至连字符串“1”也能工作。删除这个参数,在这个级别上,期望可以变成一个整数。obj.astype(凭)。(slice(None))不是一个片,而是一个1项元组。所以它在删除代码的不同位置被处理。这是由删除调用产生的类型错误,与SyntaxError非常不同。理论上,delete可以从tuple中提取切片并像s_案例中那样进行,但开发人员没有考虑这种变化。

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

对代码的快速研究表明,np.delete使用了两种不同的复制方法——按片和布尔蒙版。如果obj是一个切片,就像我们的例子一样(对于1d数组):

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

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

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

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

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.

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

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

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


From the lib/index_tricks.py source file:

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

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

maketuple业务仅适用于只有单个项、片或索引的情况。

#1


8  

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.

删除不是做任何独特或特别的事情。它只返回原始数组的一个副本,其中缺少一些项。大多数代码只是对输入进行解释,以便进行复制。

What you are asking about is the obj parameter

您要问的是obj参数

obj : slice, int or array of ints

块:块、int或int的数组

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.

简而言之,np。s_允许您使用熟悉的:语法提供一个切片。x:y表示法不能用作函数参数。

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

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

因此,使用s_删除将删除一系列值,即6 8 10,3到5。

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

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.

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

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

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

片代替s_起作用;事实上,这就是s_生产的

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

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.

这是可行的,但产生的结果是不同的,因为x[3:6]与range(3,6)不一样。而且,np.delete与list delete不一样。它通过索引而不是匹配值来删除。

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。index_exp失败的原因与np.delete(x, slice(3,6))一样。1,[1],(1,)都是有效的,并删除一个项目。甚至连字符串“1”也能工作。删除这个参数,在这个级别上,期望可以变成一个整数。obj.astype(凭)。(slice(None))不是一个片,而是一个1项元组。所以它在删除代码的不同位置被处理。这是由删除调用产生的类型错误,与SyntaxError非常不同。理论上,delete可以从tuple中提取切片并像s_案例中那样进行,但开发人员没有考虑这种变化。

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

对代码的快速研究表明,np.delete使用了两种不同的复制方法——按片和布尔蒙版。如果obj是一个切片,就像我们的例子一样(对于1d数组):

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

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

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

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

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.

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

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

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


From the lib/index_tricks.py source file:

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

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

maketuple业务仅适用于只有单个项、片或索引的情况。