【问题标题】:Why does the Numpy Diag function behaves weird?为什么 Numpy Diag 函数的行为很奇怪?
【发布时间】:2015-02-15 20:00:02
【问题描述】:

diag 函数不会将结果保存到变量中。

import numpy as np
A = np.random.rand(4,4)
d = np.diag(A)

print d
# above gives the diagonal entries of A

# let us change one entry
A[0, 0] = 0

print d
# above gives updated diagonal entries of A

diag 函数为什么会以这种方式运行?

【问题讨论】:

    标签: python numpy diagonal


    【解决方案1】:

    np.diagview 返回到原始数组。这意味着以后对原始数组的更改会反映在视图中。 (不过,好处是该操作比创建副本要快得多。)

    请注意,这只是某些 numpy 版本中的行为。在其他情况下,会返回一份副本。

    要“冻结”结果,你可以像d = np.diag(A).copy()一样复制它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 2014-02-05
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 2019-12-19
      相关资源
      最近更新 更多