为什么矩阵的范数和Python Numpy中数组的范数不同?

2024-03-28 11:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用numpy来得到向量的范数。通过使用数组或矩阵,我得到了不同的结果。你知道吗

如果a=np.数组([3,4]),结果正确:n1=7,n2=5,ninf=4。你知道吗

如果a=np.矩阵('3 4')结果不正确:n1=4,n2=5,ninf=7

有人能告诉我下面的代码有什么问题吗?谢谢。你知道吗

import numpy as np
a = np.matrix('3 4')

n1 = np.linalg.norm(a,ord=1)
n2 = np.linalg.norm(a,ord=2)
ninf = np.linalg.norm(a,ord = np.inf)

print(repr(n1)) # should be 7
print(repr(n2)) # should be 5
print(repr(ninf)) # should be 4

Tags: numpynormnp矩阵数组be向量print