numpy - numpy.r_ ['string integer' 的第三个字符串整数的解释,数组]

标签 numpy


请用外行的话帮助理解numpy.r_['1,2,0', array]中的第三个字符串整数是什么是以及它是如何工作的。
numpy 文档说明了下面的第三个整数,但无法弄清楚它到底是什么。

which axis should contain the start of the arrays which are less than the specified number of dimensions. In other words the third integer allows you to specify where the 1’s should be placed in the shape of the arrays that have their shapes upgraded.


numpy.r_

A string integer specifies which axis to stack multiple comma separated arrays along. A string of two comma-separated integers allows indication of the minimum number of dimensions to force each entry into as the second integer (the axis to concatenate along is still the first integer).

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. In other words the third integer allows you to specify where the 1’s should be placed in the shape of the arrays that have their shapes upgraded. By default, they are placed in the front of the shape tuple. The third argument allows you to specify where the start of the array should be instead. Thus, a third argument of ‘0’ would place the 1’s at the end of the array shape. Negative integers specify where in the new shape tuple the last dimension of upgraded arrays should be placed, so the default is ‘-1’.


如果没有第三个整数,这种行为是有道理的。框出一个 2D 形状并沿第 1 轴或第 2 轴放置阵列元素。
print(np.r_['0,2', [1,2,3], [4,5,6]])
print(np.r_['1,2', [1,2,3], [4,5,6]])
---
[[1 2 3]
 [4 5 6]]
[[1 2 3 4 5 6]]
但是,不确定第三个整数如何转换形状。出现'1,2,0''0,2,1' 的转置, 和 '1,2,1''0,2,0'但不知道这是如何发生的。
print(np.r_['0,2', [1,2,3], [4,5,6]])
print()
print(np.r_['0,2,0', [1,2,3], [4,5,6]])
print(np.r_['0,2,1', [1,2,3], [4,5,6]])
---
[[1 2 3]
 [4 5 6]]

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

[[1 4]
 [2 5]
 [3 6]]
[[1 2 3 4 5 6]]
答案在Understanding the syntax of numpy.r_() concatenation正在触及该部分,但不确定它完全在说什么。好像是做形状的开关(1, 3)或其转置 (3, 1)当做np.newaxis .

np.r_['0,2,-1', [1,2,3], [[4,5,6]]] has shape (3,) and shape (1, 3). np.r_ needs to add a 1 to its shape tuple to make it either (1,3) or (3,1). That is where the -1 in 0,2,-1 comes into play. It basically decides where the extra 1 needs to be placed in the shape tuple of the array.



更新
根据 hpaulj 的回答,将第三个整数视为 容易得多。换类运算符(operator)。
Plus(+) 正在将原始形状从展开形状的最高维度侧移动到较低维度。
print(np.r_['0,5,0',np.ones((2,3))].shape)  # 0 places the original shape to the highest dimension side.
print(np.r_['0,5,1',np.ones((2,3))].shape)  # shift 1 to the right from highest to lower dimension.
print(np.r_['0,5,2',np.ones((2,3))].shape)  # shift 2 to the right from highest to lower dimension.
print(np.r_['0,5,3',np.ones((2,3))].shape)  # shift 3 to the right from highest to lower dimension.
print(np.r_['0,5,4',np.ones((2,3))].shape)  # Cannot shift shape (2, 3) further than 3 in 5 dimension shape.
---
(2, 3, 1, 1, 1)
(1, 2, 3, 1, 1)
(1, 1, 2, 3, 1)
(1, 1, 1, 2, 3)
ValueError: axes don't match array
Minus(-) 正在将原始形状从最低维度侧移动到扩展形状中的更高维度。
print(np.r_['0,5,-1',np.ones((2,3))].shape)  # -1 places the original shape at the lowest dimension side.
print(np.r_['0,5,-2',np.ones((2,3))].shape)  # shift 1 to the left from lowest to higher dimension.
print(np.r_['0,5,-3',np.ones((2,3))].shape)  # shift 2 to the left from lowest to higher dimension.
print(np.r_['0,5,-4',np.ones((2,3))].shape)  # shift 3 to the left from lowest to higher dimension.
print(np.r_['0,5,-5',np.ones((2,3))].shape)  # Cannot shift shape (2, 3) further than 3 in 5 dimension shape.
---
(1, 1, 1, 2, 3)
(1, 1, 2, 3, 1)
(1, 2, 3, 1, 1)
(2, 3, 1, 1, 1)
ValueError: axes don't match array

最佳答案

np.stack我可以在相对于二维数组的 3 个位置添加一个新轴。使用单个元素元组突出显示新轴:

In [37]: np.stack((np.ones((2,3)),),0).shape     # before
Out[37]: (1, 2, 3)
In [38]: np.stack((np.ones((2,3)),),1).shape     # mid
Out[38]: (2, 1, 3)
In [39]: np.stack((np.ones((2,3)),),2).shape     # after
Out[39]: (2, 3, 1)
但与 r_我无法获得 (2,1,3) 版本:
In [40]: np.r_['0,3',np.ones((2,3))].shape
Out[40]: (1, 2, 3)
In [41]: np.r_['0,3,0',np.ones((2,3))].shape
Out[41]: (2, 3, 1)
In [42]: np.r_['0,3,1',np.ones((2,3))].shape
Out[42]: (1, 2, 3)
但与 r_我可以添加多个新轴:
In [43]: np.r_['0,4',np.ones((2,3))].shape
Out[43]: (1, 1, 2, 3)
In [44]: np.r_['0,4,0',np.ones((2,3))].shape
Out[44]: (2, 3, 1, 1)
In [45]: np.r_['0,4,1',np.ones((2,3))].shape
Out[45]: (1, 2, 3, 1)
In [46]: np.r_['0,4,2',np.ones((2,3))].shape
Out[46]: (1, 1, 2, 3)
newaxis我可以实现所有这些优点:
In [48]: np.ones((2,3))[:,None,:,None].shape
Out[48]: (2, 1, 3, 1)
最新expand_dims允许我做同样的事情:
In [51]: np.expand_dims(np.ones((2,3)),(1,3)).shape
Out[51]: (2, 1, 3, 1)
虽然 r_很聪明,我不确定它是否已经老化。 stackexpand_dims是后来添加的。 r_更有值(value)的是什么?是它能够将切片符号转换为 arangelinspace调用。
不要忘记您可以在以下位置研究代码:
np.lib.index_tricks.AxisConcatenator
看起来第二个值是用 np.array(..., ndmin) 处理的,以及带有 transpose 的第三个值.代码有点复杂。
编辑ndmin根据需要添加前导尺寸
In [82]: np.array([1,2,3], ndmin=3).shape
Out[82]: (1, 1, 3)
所以这对应于 r_ 中的默认 (-1) 值.

By default, they are placed in the front of the shape tuple

In [105]: np.r_['0,3',[1,2,3]].shape
Out[105]: (1, 1, 3)
In [106]: np.r_['0,3,-1',[1,2,3]].shape
Out[106]: (1, 1, 3)

Thus, a third argument of ‘0’ would place the 1’s at the end of the array shape.

In [111]: np.r_['0,3,0',[1,2,3]].shape
Out[111]: (3, 1, 1)
In [112]: np.r_['0,3,0',np.ones((2,3))].shape
Out[112]: (2, 3, 1)
在这两个数组中,数组维度都从 0 开始

The third argument allows you to specify where the start of the array should be instead


因此从 1 开始(即有一个前导 1):
In [113]: np.r_['0,3,1',[1,2,3]].shape
Out[113]: (1, 3, 1)
In [114]: np.r_['0,3,1',np.ones((2,3))].shape
Out[114]: (1, 2, 3)
并从 2 开始:
In [115]: np.r_['0,3,2',[1,2,3]].shape
Out[115]: (1, 1, 3)

In [116]: np.r_['0,3,2',np.ones((2,3))].shape
Traceback (most recent call last):
  File "<ipython-input-116-88fa40ceb503>", line 1, in <module>
    np.r_['0,3,2',np.ones((2,3))].shape
  File "/usr/local/lib/python3.8/dist-packages/numpy/lib/index_tricks.py", line 395, in __getitem__
    newobj = newobj.transpose(axes)
ValueError: axes don't match array
它无法在 3d 数组中的 2 处开始 (2,3) 维,因此出现错误。尝试转置由 ndmin 生成的 3d 数组时发生错误.它可能应该在构建 axes 时发现这个错误。争论,而允许 transpose失败。

Negative integers specify where in the new shape tuple the last dimension of upgraded arrays should be placed

In [117]: np.r_['0,3,-2',[1,2,3]].shape            # 3 at -2
Out[117]: (1, 3, 1)
In [118]: np.r_['0,3,-2',np.ones((2,3))].shape     # 3 at -2
Out[118]: (2, 3, 1)
'-3' 产生 (3,1,1),在另一个之上,并产生 (2,3) 的错误。
当关注扩展数组中原始维度出现的位置时,这第三个数字最有意义,而不是关注它添加 1 的位置。 start of the array shape对于正数,“数组形状的结尾”对于负数”。
换句话说,当 n ndim 数组放置在 m 中ndim 结果,默认值为前导 1。 n在右边。第三个数字改变了 n靠左。
让我们通过将 (2,) 放入 (5,) 来测试:
In [121]: np.r_['0,5',np.ones((2,3))].shape
Out[121]: (1, 1, 1, 2, 3)                      # all the way right
In [122]: np.r_['0,5,0',np.ones((2,3))].shape
Out[122]: (2, 3, 1, 1, 1)                      # all the way left
In [123]: np.r_['0,5,1',np.ones((2,3))].shape
Out[123]: (1, 2, 3, 1, 1)
In [124]: np.r_['0,5,2',np.ones((2,3))].shape
Out[124]: (1, 1, 2, 3, 1)
In [125]: np.r_['0,5,-3',np.ones((2,3))].shape    
Out[125]: (1, 2, 3, 1, 1)                        # same as [123]
In [126]: np.r_['0,5,-2',np.ones((2,3))].shape
Out[126]: (1, 1, 2, 3, 1)

关于numpy - numpy.r_ ['string integer' 的第三个字符串整数的解释,数组],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65526672/

相关文章:

Python:从 csv 读取时间步长到数组:使用 numpy 后处理模型数据;

python - 如何在Python中从多列中的行组中找到2个最大值,并在输出中显示其行索引和列索引

python - 查找范围内众数的方法

python - 使用 Pandas 或 Numpy 的 n 维滑动窗口

python - numpy 中的 R summary() 等价物

python - TypeError : 'numpy.ndarray' object is not callable in my code

python - 高效处理 Python 列表中的重复项

python - Numpy 结构化数组无法进行基本的 numpy 操作

python - 如何更改满足特定条件的数据框中的第一个值

numpy - 自定义 seaborn 联合图中的轴标签