传递给numpy.einsum()的下标是什么意思?

UCU110

我正在尝试理解python代码,该代码numpy.einsum()用于将4维numpy数组转换为2维A或3维数组。传递给的下标numpy.einsum()如下:

Mat1 = np.einsum('aabb->ab', A) 

Mat2 = np.einsum('abab->ab', A)

Mat3 = np.einsum('abba->ab', A) 

T1 = np.einsum('abcb->abc' A)

T2 = np.einsum('abbc->abc', A)

例如,在遵循了(理解NumPy的einsum)和(Python-Sum 4D数组的回答之后,我试图numpy.sum()用来理解上述下标的含义,Mat1 = np.sum(A, axis=(0,3))但是我无法复制得到的结果numpy.einsum()有人可以解释这些下标的解释方式numpy.einsum()吗?

CrafterKolyan

我建议您阅读Wikipedia上的Einstein符号

这是您问题的简短答案:

np.einsum('aabb->ab', A)

手段:

res = np.empty((max_a, max_b), dtype=A.dtype)
for a in range(max_a):
  for b in range(max_b):
    res[a, b] = A[a, a, b, b]
return res

简短说明:
aabb表示索引及其相等性(请参阅A[a, a, b, b]);
->ab表示形状是(max_a, max_b)并且您不需要在这两个索引上有两个具有和。(如果他们c也是,那么您应该将所有内容加起来,c因为后面没有显示->


您的其他示例:

np.einsum('abab->ab', A)

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b), dtype=A.dtype)
for a in range(max_a):
  for b in range(max_b):
    res[a, b] = A[a, b, a, b]
return res
np.einsum('abba->ab', A) 

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b), dtype=A.dtype)
for a in range(max_a):
  for b in range(max_b):
    res[a, b] = A[a, b, b, a]
return res
np.einsum('abcb->abc', A)

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b, max_c), dtype=A.dtype)
for a in range(max_a):
  for b in range(max_b):
    for c in range(max_c):
      res[a, b, c] = A[a, b, c, b]
return res
np.einsum('abbc->abc', A)

# Same as (by logic, not by actual code)

res = np.empty((max_a, max_b, max_c), dtype=A.dtype)
for a in range(max_a):
  for b in range(max_b):
    for c in range(max_c):
      res[a, b, c] = A[a, b, b, c]
return res

一些代码来检查它是否真实:

import numpy as np


max_a = 2
max_b = 3
max_c = 5

shape_1 = (max_a, max_b, max_c, max_b)
A = np.arange(1, np.prod(shape_1) + 1).reshape(shape_1)

print(A)
print()
print(np.einsum('abcb->abc', A))
print()

res = np.empty((max_a, max_b, max_c), dtype=A.dtype)
for a in range(max_a):
  for b in range(max_b):
    for c in range(max_c):
      res[a, b, c] = A[a, b, c, b]

print(res)
print()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当传递给 singleArgViewModelFactory 时 ::MainViewModel 是什么意思

[...,None]在Numpy中是什么意思?

在NumPy Python中是什么意思?

将带有单词“ stub”的块传递给let in rspec是什么意思?

Django Rest Framework 传递给 ModelSerializer 的参数是什么意思?

通过参数将值传递给程序是什么意思?

通过ldflags选项传递给go命令时,w标志是什么意思?

将两个别名传递给typedef struct是什么意思?

通过引用将指针参数传递给函数是什么意思?

当作为提供者传递给DateTime.TryParseExact时,“ null”是什么意思?

numpy:这个einsum表示什么意思,还有其他选择吗?

传递参数是什么意思?在C#中

Systemd 命令末尾传递的选项是什么意思?

Apple 通过视图传递@ObjectBinding 是什么意思?

“按价值传递参考”是什么意思?

Gradle传递依赖中的“ all * .exclude”是什么意思?

调用numpy数组时,“ [[]]”是什么意思?

X [:,:,::,i]在numpy中是什么意思?

替换在numpy.random.choice中是什么意思?

numpy整形中的-1是什么意思?

numpy中的赋值中arr [:]是什么意思?

创建numpy数组时dtype = object是什么意思?

numpy.polyfit的额外结果是什么意思?

numpy数组中的“-”值是什么意思?

'TypeError: 'numpy.ndarray' object is not callable' 是什么意思?

Numpy文档中提到的“ sum product”是什么意思?

numpy.array(value)是什么意思?

numpy数组(这个答案是什么意思?)

numpy 库中的“NPY_INLINE”是什么意思?