- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
题
请用外行的话帮助理解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.
A string integer specifies which axis to stack multiple commaseparated arrays along. A string of two comma-separated integersallows indication of the minimum number of dimensions to force eachentry into as the second integer (the axis to concatenate along isstill the first integer).
A string with three comma-separated integers allows specification ofthe axis to concatenate along, the minimum number of dimensions toforce the entries to, and which axis should contain the start of thearrays which are less than the specified number of dimensions. Inother words the third integer allows you to specify where the 1’sshould be placed in the shape of the arrays that have their shapesupgraded. By default, they are placed in the front of the shape tuple.The third argument allows you to specify where the start of the arrayshould be instead. Thus, a third argument of ‘0’ would place the 1’sat the end of the array shape. Negative integers specify where in thenew shape tuple the last dimension of upgraded arrays should beplaced, so the default is ‘-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]]
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,)
andshape(1, 3)
.np.r_
needs to add a 1 to its shape tuple tomake 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 1needs to be placed in the shape tuple of the 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
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_
很聪明,我不确定它是否已经老化。
stack
和
expand_dims
是后来添加的。
r_
更有值(value)的是什么?是它能够将切片符号转换为
arange
或
linspace
调用。
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
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) 的错误。
start of the array shape
对于正数,“数组形状的结尾”对于负数”。
n
ndim 数组放置在
m
中ndim 结果,默认值为前导 1。
n
在右边。第三个数字改变了
n
靠左。
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/
在 numpy 中 documents : >>> np.r_['0,2,0', [1,2,3], [4,5,6]] array([[1], [2], [3],
在 Pandas 中,我创建了一个元组列表,表示围绕给定索引点集的一系列行: mask = df.loc[df['Illustration']=='Example'].index idxlist =
在哪种情况下,使用像 numpy.r_ 或 numpy.c_ 这样的对象比使用像 concatenate 或 vstack 这样的函数更好(更有效,更合适)? 我试图理解程序员写的代码,例如: ret
Numpy.r_、.c_ 和 .s_ 是我遇到的唯一在方括号而不是圆括号中接受参数的 Python 函数。为什么会这样?这些功能有什么特别之处吗?我可以创建自己的使用括号的函数吗(不是我想要的;只是好
我在函数 r_ 的 numpy 文档中阅读了以下内容: A string integer specifies which axis to stack multiple comma separated
根据 numpy.r_ 上的 numpy/scipy 文档 here ,它是“不是函数,所以不带参数”。 如果不是函数,那么numpy.r_等“函数”的正确说法是什么? 最佳答案 我会争辩说,r_ i
以下代码取自 numpy function base on github sa = sort(a[i:i+block]) n += np.r_[sa.searchsorted(bins[:-1], '
题 请用外行的话帮助理解numpy.r_['1,2,0', array]中的第三个字符串整数是什么是以及它是如何工作的。 numpy 文档说明了下面的第三个整数,但无法弄清楚它到底是什么。 which
玩 NumPy 串联和范围构建对象 r_ 我无意中发现了以下行为:显然,无论是实数、虚数还是真复数,一个复数步骤的绝对值都取为类似 linspace 的步骤。 >>> import numpy as
Numpy中提供了concatenate,append, stack类(包括hsatck、vstack、dstack、row_stack、column_stack),r_和c_等类和函数用于数组拼接
情况 我有代表双 channel 音频的二维数组。我想创建一个函数,在任意位置返回该数组的切片(例如仅语音部分)。当我将值显式写入 np.r_ 时,我已经知道如何执行此操作: 示例数据 arr = n
我是一名优秀的程序员,十分优秀!