numpy-解释numpy.r的第三个字符串整数[';字符串整数';,数组] 问题:

numpy-解释numpy.r的第三个字符串整数[';字符串整数';,数组] 问题:,numpy,Numpy,请用外行的话帮助理解numpy.r['1,2,0',array]中的第三个字符串整数是什么,以及它是如何工作的 numpy文档说明了第三个整数,如下所示,但无法确定它到底是什么 哪个轴应包含小于指定维度数的数组的开始。换句话说,第三个整数允许您指定1在已升级其形状的阵列形状中的位置 字符串整数指定要堆叠多个逗号的轴 分离阵列沿。由两个逗号分隔的整数组成的字符串 允许指示强制每个组件的最小尺寸数 作为第二个整数输入(要连接的轴为 仍然是第一个整数) 带有三个逗号分隔整数的字符串允许指定 要连接的

请用外行的话帮助理解
numpy.r['1,2,0',array]
中的第三个字符串整数是什么,以及它是如何工作的

numpy文档说明了第三个整数,如下所示,但无法确定它到底是什么

哪个轴应包含小于指定维度数的数组的开始。换句话说,第三个整数允许您指定1在已升级其形状的阵列形状中的位置

字符串整数指定要堆叠多个逗号的轴 分离阵列沿。由两个逗号分隔的整数组成的字符串 允许指示强制每个组件的最小尺寸数 作为第二个整数输入(要连接的轴为 仍然是第一个整数)

带有三个逗号分隔整数的字符串允许指定 要连接的轴,要连接的最小尺寸数 强制输入,并哪个轴应包含 小于指定维数的数组。In 换句话说,第三个整数允许您指定1的位置 应以具有其形状的阵列的形状放置 升级。默认情况下,它们位于形状元组的前面。 第三个参数允许您指定数组的起始位置 应改为。因此,“0”的第三个参数将放置1的 在阵列形状的末尾。负整数指定 新形状元组升级数组的最后一个维度应为 放置,因此默认值为“-1”

如果没有第三个整数,该行为是有意义的。框定二维形状并沿第一轴或第二轴放置阵列元素

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]]
中的答案涉及到零件,但不确定它完全说明了什么。在执行
np.newaxis
时,它看起来像是一个开关,用于使形状
(1,3)
或其转置
(3,1)

np.r.['0,2,-1',[1,2,3],[4,5,6]]
具有形状
(3,)
和 形状
(1,3)
np.r
需要将1添加到其形状元组中,以 将其设置为
(1,3)
(3,1)
。这就是0,2,-1中的
-1发挥作用的地方。它基本上决定了额外1的位置
需要放置在数组的形状元组中


更新 根据hpaulj的答案,将第三个整数视为移位运算符要容易得多

加号(+)将原始形状从展开形状的最高尺寸侧移动到较低尺寸侧

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
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
负(-)将原始形状从展开形状中的最低尺寸侧移动到较高尺寸侧

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
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
我可以在相对于2d数组的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)
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)
但是使用
r
我无法获得(2,1,3)版本:

但是,使用
r\u
我可以添加多个新轴:

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)
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\u
很聪明,但我不确定它是否老化良好<代码>堆栈
和扩展_dims已在后面添加。
r\uu
中更有价值的是它能够将切片表示法转换为
arange
linspace
调用

不要忘记,您可以在以下位置学习代码:

np.lib.index_tricks.AxisConcatenator
看起来第二个值是用
np.array(…,ndmin)
处理的,第三个值是用
转置处理的。代码有点复杂

编辑
ndmin
根据需要添加前导尺寸

In [82]: np.array([1,2,3], ndmin=3).shape
Out[82]: (1, 1, 3)
因此,这对应于
r\uu
中的默认值(-1)

默认情况下,它们放置在形状元组的前面

因此,第三个参数“0”将把1放在数组形状的末尾

在这两种情况下,数组维度都从0开始

第三个参数允许您指定数组的起始位置

因此,从1开始(即以1开头):

从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
“-3”产生(3,1,1),超过另一个,并产生(2,3)的误差

当关注原始维度在扩展数组中出现的位置时,第三个数字最有意义,而不是关注添加1的位置。数组形状的
开始部分
用于正片,而“数组形状的结束部分`用于负片”

换句话说,当一个
n
ndim数组被放置在
m
ndim结果中时,默认值具有前导1。
n
位于右侧。第三个数字向左移动
n

让我们通过将a(2,)放在a(5,)中来测试它:


归根结底,
r
只是一个
np.concatenate
。该字符串参数允许您微调它扩展维度的方式以及在哪个轴上连接。默认情况下,它类似于
hstack
。我推荐
np.stack
的频率比
r\uu
高。使用
r
,但不要因为没有完全理解它而觉得失去了任何东西。将1d(3,)数组扩展到2d有两个选项,(3,1)和(1,3)。0执行一个,1(和-1)执行另一个。尝试扩展到3或4d可能会提供进一步的见解(使用(3,1,1)、(1,3,1)、(1,1,3)备选方案)为什么“使用r_我无法获得(2,1,3)版本”或“ValueError:轴与数组不匹配”导致
np.r_['0,3,1',np.ones((2,3))
?为什么
np.r_u['0,4,1',np.ones((2,3))
在展开形状的最高和最低处添加axix作为
(
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
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)
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)