我试图采取对角矩阵,但不幸的是,我得到一个错误。我的代码是:
import numpy as np
a = np.array([1, 2, 3, 2, 1 ,2])
b = np.diagonal((a)*(a).T)
或者你可以这样做:
import numpy as np
a = np.array([1, 2, 3, 2, 1 ,2]).reshape(1,-1)
b = np.diagonal((a)*(a).T)
目标是使它成为一个二维矩阵,而不是像最初声明的那样是一个一维数组。
Try
import numpy as np
a = np.array([1, 2, 3, 2, 1 ,2])[np.newaxis]
b = np.diagonal((a)*(a).T)
参考:1D NumPy数组的转置