Python np.r_ 做什么(numpy)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30597869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:40:20  来源:igfitidea点击:

What does np.r_ do (numpy)?

pythonnumpy

提问by Philipp

Following code is taken from here

以下代码取自here

sa = sort(a[i:i+block])
n += np.r_[sa.searchsorted(bins[:-1], 'left'),
           sa.searchsorted(bins[-1], 'right')]

So I know that searchsorted finds the position in the array sawhere the elements of binswould have to be inserted in order to keep sasorted (leftgives the index left of where we would insert the value and rightthe right index). What I don't understand is the whole construction around it meaning what is

所以我知道 searchsorted 在数组sa中找到bins必须插入的元素以保持sa排序的位置(left给出我们将插入值right的位置左侧的索引和右侧索引)。我不明白的是围绕它的整个结构意味着什么

np.r_[array,array]

What is np.r_?

什么是np.r_

采纳答案by omerbp

What it does is row-wise merging. This posthas some nice example:

它所做的是按行合并。这篇文章有一些很好的例子:

>>>V = array([1,2,3,4,5,6 ])
>>>Y = array([7,8,9,10,11,12])
>>>np.r_[V[0:2],Y[0],V[3],Y[1:3],V[4:],Y[4:]]
array([ 1,  2,  7,  4,  8,  9,  5,  6, 11, 12])

Read more about it in this , and in the documentationof numpy.

在 this 和numpy的文档中阅读更多关于它的信息

回答by vivek

numpy.r_[array[], array[]]

This is used to concatenate any number of array slices along row (first) axis. This is a simple way to create numpy arrays quickly and efficiently.

这用于沿行(第一)轴连接任意数量的数组切片。这是一种快速有效地创建 numpy 数组的简单方法。

For instance, to create an array from two different arrays by selecting the elements of your choice, we'll have to assign the sliced values to a new varaible and use concatenation method to join them along an axis.

例如,要通过选择您选择的元素从两个不同的数组创建一个数组,我们必须将切片值分配给一个新变量,并使用串联方法沿轴连接它们。

>>> a = np.arange(9).reshape(3,3)
>>> b = np.arange(10,19).reshape(3,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> b
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

I want to create a new 2-D array, with 2*2 elements ([4,5,14,15]) then, I'll have to do the following,

我想创建一个新的二维数组,包含 2*2 个元素 ([4,5,14,15]) 然后,我必须执行以下操作,

>>> slided_a = a[1,1:3]
>>> sliced_b = b[1,1:3]
>>> new_array = np.concatenate((sliced_a, sliced_b), axis = 0) 

As this is clearly an inefficient way because, as the number of elements that are to be included in the new array increases, the temporary variables that are assigned to store the sliced values increases.

由于这显然是一种低效的方法,因为随着要包含在新数组中的元素数量的增加,分配给存储切片值的临时变量也会增加。

This is where we use np.r_

这是我们使用 np.r_ 的地方

>>> c = np.r_[a[1,1:3],b[1,1:3]]
array([ 4,  5, 14, 15])

Likewise, if we want to create a new array by stacking the sliced values in 2nd axis, we can use np.c_

同样,如果我们想通过在第二个轴上堆叠切片值来创建一个新数组,我们可以使用 np.c_

>>> c = np.c_[a[1,1:3],b[1,1:3]]
array([[ 4, 14],
       [ 5, 15]])