简体   繁体   English

numpy.r_ 中的字符串是什么意思?

[英]What does the String mean in numpy.r_?

in numpy's documents :在 numpy 的文档中

>>> np.r_['0,2,0', [1,2,3], [4,5,6]]
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])

what does the third number mean in the string '0,2,0'?字符串“0,2,0”中的第三个数字是什么意思?

I haven't used the string parameter of r_ much; r_的字符串参数我没怎么用过; it's easier, for me, to work directly with concatanate and its variantes.对我来说,直接使用concatanate及其变体更容易。

But looking at the docs:但是查看文档:

A string with three comma-separated integers allows specification of the axis to concatenate along, the minimum number of dimensions to force the entries to, and which axis should contain the start of the arrays which are less than the specified number of dimensions.包含三个逗号分隔整数的字符串允许指定要连接的轴、强制条目的最小维数以及哪个轴应包含小于指定维数的数组的开头。

'0.2.0'
 axis = 0
 make it 2d
 start with 0d

In [79]: np.r_['0,2,0', [1,2,3], [4,5,6]]
Out[79]: 
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])

A concatenate equivalent连接等价物

In [80]: np.concatenate(([1,2,3], [4,5,6]))
Out[80]: array([1, 2, 3, 4, 5, 6])
In [81]: np.concatenate(([1,2,3], [4,5,6]))[:,None]
Out[81]: 
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])

Here I've concatenated on axis=0, and expanded to 2d after concatenate.在这里,我在轴 = 0 上进行了连接,并在连接后扩展为 2d。 But it sounds like r_ expands the dimensions of the elements first (but we can double check in the code).但听起来r_扩展了元素的维度(但我们可以仔细检查代码)。

In [83]: alist = ([1,2,3], [4,5,6]) 
In [86]: [np.expand_dims(a,1) for a in alist]
Out[86]: 
[array([[1],
        [2],
        [3]]), array([[4],
        [5],
        [6]])]
In [87]: np.concatenate(_, axis=0)
Out[87]: 
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])

I'm using expand_dims to make the inputs 2 d, and to add the new dimension after the first.我正在使用expand_dims使输入为 2 d,并在第一个维度之后添加新维度。 Having done that I can concatenate on axis 0.完成后,我可以在轴 0 上进行连接。

Note that the inputs to r_ could already be 2d, as in:请注意, r_的输入可能已经是 2d,如下所示:

np.r_['0,2,0',[1,2,3], [[4],[5],[6]]]
np.r_['0,2,0',[1,2,3], np.expand_dims([4,5,6],1)]
np.r_['0,2,0',[1,2,3], np.atleast_2d([4,5,6]).T]

The 3d number, if 1, turns the components into 3d 数,如果为 1,则将组件变成

In [105]: np.atleast_2d([4,5,6])
Out[105]: array([[4, 5, 6]])

In [103]: np.r_['0,2,1',[1,2,3],[4,5,6]]
Out[103]: 
array([[1, 2, 3],
       [4, 5, 6]])

Often if documentation is unclear I like to either dig into the code, or experiment with alternative inputs.通常,如果文档不清楚,我喜欢深入研究代码,或尝试替代输入。

In [107]: np.r_['1,2,1',[1,2,3], [4,5,6]]
Out[107]: array([[1, 2, 3, 4, 5, 6]])
In [108]: np.r_['1,2,0',[1,2,3], [4,5,6]]
Out[108]: 
array([[1, 4],
       [2, 5],
       [3, 6]])

Looking at the code, I see it uses查看代码,我看到它使用

array(newobj, copy=False, subok=True, ndmin=ndmin)

to expand the components to the desired ndmin .将组件扩展到所需的ndmin The 3d number is used to construct a transpose parameter. 3d 数字用于构造transpose参数。 Details are messy, but the effect is something like:细节比较乱,但是效果是这样的:

In [111]: np.array([1,2,3], ndmin=2)
Out[111]: array([[1, 2, 3]])
In [112]: np.array([1,2,3], ndmin=2).transpose(1,0)
Out[112]: 
array([[1],
       [2],
       [3]])

在此处输入图片说明

Thank you for all the answers!!!谢谢大家的回答!!! Now, I think I have a better understanding of this problem.现在,我想我对这个问题有了更好的理解。 So, I drawed the mind map.于是,我画了思维导图。 Please correct me if I'm wrong.如果我错了,请纠正我。 As a beginner of Python, I am trying my best to learn more and think more.作为Python的初学者,我正在努力学习更多,思考更多。 Lastly, I have to apologize for my poor English.😋最后,我必须为我糟糕的英语道歉。😋

'0,2,0' is a three comma-separated string. '0,2,0' 是三个逗号分隔的字符串。 When used with numpy.r_ each element respectively specifies:与 numpy.r_ 一起使用时,每个元素分别指定:

  1. Which dimension to use as the axis of concatenation哪个维度用作串联轴
  2. What is the minimum number of dimensions that each array needs to have.每个数组需要的最小维数是多少。 If an array has less dimensions than the number specified here then numpy.r_ upgrades the shape tuple of that array by adding 1s to its shape tuple.如果数组的维数小于此处指定的数字,则 numpy.r_ 会通过向其形状元组添加 1 来升级该数组的形状元组。
  3. How the extra dimensions need to be added if an upgrade is needed如果需要升级,如何添加额外的维度

For the third element you have two choices:对于第三个元素,您有两个选择:

  1. 0 or a positive integer 0 或正整数
  2. A negative integer负整数

If a 0 or a positive integer is used this specifies where in the dimension of the array's upgraded shape tuple should the original shape tuple begin.如果使用0或正整数,则指定原始形状元组应在数组升级后的形状元组的维度中的何处开始。 For example a 0 would indicate that the original shape tuple should begin at dimension 0 of the upgraded shape tuple.例如, 0表示原始形状元组应从升级后的形状元组的0维开始。

If a negative number is used the integer following the negative sign specifies where in the dimension of the array's upgraded shape tuple should the original shape tuple end.如果使用负数,则负号后面的整数指定原始形状元组应在数组升级后的形状元组的维度中的哪个位置。 For example -1 would indicate that the original shape tuple should end at the last dimension of the upgraded shape tuple.例如-1表示原始形状元组应在升级后的形状元组的最后一个维度结束。 -2 would indicate the same but at the second last dimension. -2表示相同,但​​在倒数第二个维度。

Finally let's apply the above to your example.最后,让我们将上述内容应用到您的示例中。

np.r_['0,2,0', [1,2,3], [4,5,6]]

The above contains two arrays.上面包含两个数组。 Each has a dimension (3,) which means that both are one-dimensional arrays.每个都有一个维度(3,) ,这意味着它们都是一维数组。 The second element in the comma-separated integer string is a 2 which means that each array needs to have a dimension of at least 2 .逗号分隔的整数字符串中的第二个元素是2 ,这意味着每个数组的维度至少需要2 Since both arrays are one-dimensional they both need to have one dimension added to their shape tuple.由于两个数组都是一维的,因此它们都需要将一维添加到它们的形状元组中。 The third element in the comma-separated integer string is a 0 which means that the original shape tuples, which is (3,) for both arrays, need to start at dimension 0 of the shape tuple of the upgraded arrays.逗号分隔的整数字符串中的第三个元素是0 ,这意味着原始形状元组(3,)两个数组的(3,)需要从升级数组的形状元组的第0维开始。 This is achieved by adding a 1 to the end of the original shape tuple.这是通过在原始形状元组的末尾添加1来实现的。 This upgrades both arrays to having a (3,1) shape tuple.这将两个数组都升级为具有(3,1)形状的元组。 Now since the first element of the comma-separated integer string is a 0 the upgraded arrays will be concatenated using dimension 0 as the axis of concatenation.现在,由于逗号分隔的整数字符串的第一个元素是0 ,升级后的数组将使用维度0作为连接轴进行连接。 This means that if dimension 0 is set aside for both upgraded arrays the remaining dimension(s) need to be identical.这意味着,如果为两个升级的阵列留出维度0则其余维度需要相同。 In this example if dimension 0 is set aside from both upgraded arrays only dimension 1 remains and for both upgraded arrays dimension 1 has a value of 1 .在此示例中,如果维度0与两个升级的数组分开,则仅保留维度1并且对于两个升级的数组,维度1的值为1 Therefore when concatenating the two upgraded arrays the result will have a shape tuple of (3+3,1) == (6,1) which is the shape of因此,当连接两个升级后的数组时,结果将具有 (3+3,1) == (6,1) 的形状元组,其形状为

array([[1],
   [2],
   [3],
   [4],
   [5],
   [6]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM