- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想获取一个数组的行和并将输出放入另一个数组的对角线中。出于性能原因,我想使用 np.sum
函数的 out
参数。
mat1 = np.array([[0.5, 0.5],[0.6, 0.4]])
mat2 = np.zeros([2,2])
mat3 = np.zeros([2,2])
如果我想将mat1
的行总和放入mat2
的第一行,我可以这样做:
np.sum(mat1, axis=1, out = mat2[0])
mat2
#array([[ 1., 1.],
# [ 0., 0.]])
但是,如果我想将总和放入 mat3
的对角线索引中,我似乎无法这样做。
np.sum(mat1, axis=1, out = mat3[np.diag_indices(2)])
mat3
#array([[ 0., 0.],
# [ 0., 0.]])
当然,以下方法有效,但我想使用np.sum
的out
参数
mat3[np.diag_indices(2)] = np.sum(mat1, axis=1)
mat3
#array([[ 1., 0.],
# [ 0., 1.]])
有人可以解释一下 out
参数不接受数组对角线索引作为有效输出的行为吗?
最佳答案
NumPy 有两种类型的索引:基本索引和高级索引。
基本索引是指当索引表达式仅使用整数、切片、...
和 None
(又名 np.newaxis
)。这可以完全通过偏移量和步幅的简单操作来实现,因此当基本索引返回数组时,结果数组始终是原始数据的 View 。写入 View 会写入原始数组。
当您使用数组进行索引时,如 mat3[np.diag_indices(2)]
中所示,您将获得高级索引。高级索引不能以返回原始数据 View 的方式完成;它总是从原始数组中复制数据。这意味着当您尝试使用副本作为 out
参数时:
np.sum(mat1, axis=1, out = mat3[np.diag_indices(2)])
数据被放入副本中,但原始数组不受影响。
<小时/>我们现在应该能够使用 np.diagonal
来实现此目的,但即使文档说 np.diagonal
的输出可以在 NumPy 中写入1.10,使其可写的相关功能是still in limbo 。最好不要为此使用 out
参数:
mat3[np.diag_indices(2)] = np.sum(mat1, axis=1)
关于python - 将 numpy 函数的输出放入数组的对角线中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35068638/
我有一个 6x6 数组,并且希望始终获取接下来的四个值。举个例子: 0----- -1---- --2--- ---3-- ----4- 所以我想得到所有对角线的 (0+1+2+3) 和 (1+2+3
我想遍历一个已转换为一维的二维方阵。 问题是我想遍历它,就像我在对角条中遍历原始 2D 一样。 该数组是对角数组,我最初使用一维的 malloc 创建它,以避免分配太多内存。 数组的大小: int T
“给定一个 2D 字符数组和一个字符串。 查找特定字符串是否出现在矩阵的对角线上。 private static boolean diagonalContains(char[][] grid,Stri
我有一个由 nxn 矩阵组成的 njxnj 矩阵。我想提取 nxn 矩阵的对角 j block 。即我想提取对角线(对于 n = 2,j = 4): 最有效的方法是什么? 最佳答案 要为元素建立索引,
这是一个 self 回答的问题。给定两个数据框, x 0 1 0 1 2 1 3 4 y 0 1 2 0 5 6 7 1 8 9 X 2 Y Z 0 x 和
我试图让我的程序打印出不等于幻方规则的行、列或对角线,例如,如果矩阵是 1 9 5 2 4 3 6 8 7 第 1 行 [2, 4, 3] 不起作用 第 2 行 [6, 8, 7] 不起作用 第 0
所以我有一个像这样的 4x4 矩阵 |0 1 2 3 -+------- 0|0 1 3 6 1|2 4 7 a 2|5 8 b d 3|9 c e f 并且我是按照其中的十六进制字符指定的顺序遍历
什么是获取正方形DataFrame的对角线的有效方法。我希望结果是一个 Series 和一个 MultiIndex 有两个级别,第一个是 DataFrame 的索引,第二个级别是DataFrame 的
问题:我正在尝试在 SwiftUI 中以矩形呈现对角线线性渐变。 我实现了一个标准的多点线性渐变,它在呈现为正方形时效果很好,但是当我将框架更改为矩形时,它有一些奇怪的行为,看起来更水平,或者有一些奇
我目前正在尝试找到一种在 C# for Excel 中使用 VSTO 的方法,以使用 C# 代码在单元格中绘制对角线。但我在网上找不到任何人甚至试图这样做。 有谁知道如何实现这一目标? 谢谢 (对不起
我需要删除图像中的线条,这最终是一个表格。我找到了一种删除水平线和垂直线的方法: convert 1.jpg -type Grayscale -negate -define morphology:co
我有一个如下所示的矩阵: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A',
如何在Python中按如下方式堆叠矩阵,使得父矩阵的元素在子矩阵的相同 block 对角点处形成 block 对角线。例子:我有四个矩阵 AA,AB,BA,BB 我想制作如附图所示的矩阵。 最佳答案
我在做一些统计。 我有数据框: tag a b c d e f a 5 2 3 2 0 1 b 2 4 3 2 0 1
我最近做了一个 Connect4 游戏,当我的 Connect4 向右对角线连接时,我的 Connect4 没有赢得游戏。并且它只适用于某些组合,当它连接到左边的对角线时。坐标:- 左上角:(0,0)
在 numpy 中有什么方法可以获取对角数组的引用吗?我希望我的数组对角线除以某个因子谢谢 最佳答案 如果 X 是你的数组,c 是因子, X[np.diag_indices_from(X)] /= c
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个形状为 (m*n, m*n) 的张量,我想提取一个大小为 (n, m*n) 的张量,其中包含对角线上大小为 n*n 的 m 个块。例如: >>> a tensor([[1, 2, 0, 0],
我目前正在使用 matplotlib/pyplot 绘制 3d 对象,如下所示: fig = plt.figure().gca(projection='3d') plot = fig.plot_sur
好的,让我们考虑一个 64 位的数字,它的位组成一个 8x8 的表。 例如 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1
我是一名优秀的程序员,十分优秀!