- 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/
我正在尝试学习 Fortran,并且看到了很多不同的定义,我想知道他们是否正在尝试完成同样的事情。以下有什么区别? 整数*4 整数(4) 整数(kind=4) 最佳答案 在 Fortran >=90
我以前从未编程过,最近(1 周前)才开始学习!第一门类(class)是函数式编程,使用 Haskell。 我有一项学校作业,我想通过删除一两个步骤来改进它,但我遇到了一个讨厌的错误。 基本上,我创建了
给定以下GraphQL请求和变量: 请求: query accounts($filter:AccountFilter, $first_:String, $skip_:Int) { accounts
我已经搜索了 StackOverflow,但找不到关于如何检查计算器应用程序的数字输入正则表达式的答案,该计算器应用程序将检查每个 keyup 的以下格式(jquery key up): 任何整数,例
类似于我上一篇致歉的文章,但没有那么长篇大论。基本上我想知道当每次重绘调用只重绘屏幕的一小部分时,优化重绘到 JFrame/JPanel 的最佳选择是什么。 此外,除了重绘重载之外,我并不是 100%
所以在我的教科书中有一个使用 f# 的递归函数的例子 let rec gcd = function | (0,n) -> n | (m,n) -> gcd(n % m,m);; 使用此功能,我的教科书
我有一个数据结构,例如表达式树或图形。我想添加一些“测量”功能,例如depth和 size . 如何最好地键入这些函数? 我认为以下三个变体的用处大致相同: depth :: Expr -> Int
这样写比较好 int primitive1 = 3, primitive2 = 4; Integer a = new Integer(primitive1); Integer b = new Inte
我是 Java 8 新手,想根据键对 Map 进行排序,然后在值内对每个列表进行排序。 我试图寻找一种 Java 8 方法来对键和值进行排序。HashMap>映射 map.entrySet().str
这就是我的目标... vector ,int> > var_name (x, pair (y),int>); 其中 x 是 vector var_name 的大小,y 是对内 vector 的大小。
这里是 an answer to "How do I instantiate a Queue object in java?" , Queue is an interface. You can't i
这个问题在这里已经有了答案: Weird Integer boxing in Java (12 个答案) Why are autoboxed Integers and .getClass() val
我们可以使用 C++ STL 做这样的事情吗?如果是,我将如何初始化元素?我试图这样做,但没有成功。 pair,vector>p; p.first[0]=2; 最佳答案 Can we do som
您好,我正在尝试为百分比和整数数组中的数字找到索引。假设 arraynum = ['10%','250','20%','500'] 并且用户发送一个值 15%,这个数字在哪个范围内居住?我可以使用这段
我与三列有关系:ProductName、CategoryID 和 Price。我需要选择仅那些价格高于给定类别中平均产品价格的产品。(例如,当apple(ProductName)是fruit(Cate
我已经坚持了一段时间,我正在尝试将一些数据配对在一起。这是我的代码。 #include #include using namespace std; int main() { pair data(
我收到错误:'(Int, Int)' 与 'CGPoint' 不相同 如何将 (Int, Int) 转换为 CGPoint let zigzag = [(100,100), (100,150)
我在 .cpp 文件中发现了以下代码。我不理解涉及头文件的构造或语法。我确实认识到这些特定的头文件与 Android NDK 相关。但是,我认为这个问题是关于 C++ 语法的一般问题。这些在某种程度上
我将这些输入到 Scala 解释器中: val a : Integer = 1; val b : Integer = a + 1; 我收到消息: :5: error: type mismatch;
C++:vector>v(size);当我试图打印出值时显示 0 作为值,但是当未声明 vector 大小时它显示正确的输出?为什么这样?例如: int x; cin>>x; vector>v(x);
我是一名优秀的程序员,十分优秀!