np.linalg.norm()
computes the norm of a NumPy array according to an order, ord
, which specifies the metric by which the norm takes. For example, if we are given an array
[𝑥1,...,𝑥𝑛]
with numbers 𝑥𝑖xi then we can compute the Frobenius Norm
or more commonly called the 2-norm
by doing:
In NumPy you can also use np.linalg.norm()
to compute the norm of a matrix, or a matrix's columns or rows, treating each as their own array.
example:
1 import numpy as np 2 from numpy.linalg import norm 3 np.set_printoptions(threshold='nan') 4 5 a1 = np.array([1,2,3]) 6 a2 = np.array([0,0,-3]) 7 testa = np.array([[ 1.76405235, 0.40015721, 0.97873798], 8 [ 2.2408932 , 2.2677152 , -0.57712067], 9 [ 0.95008842, 0.79873121, -0.68033952], 10 [ 0.4105985 , 0.55464207, 0.77393398]]) 11 12 testb = np.array([[ 1.76405235, 0.40015721, 0.97873798], 13 [ 2.2408932 , 2.2677152 , -0.57712067], 14 [ 0.95008842, 0.79873121, -0.68033952], 15 [ 0.4105985 , 0.55464207, 0.77393398]]) 16 dist=lambda x, y: norm(x - y, ord=1) 17 18 print np.linalg.norm([2,-1,3,-4], np.inf) # returns 2, 19 20 print np.linalg.norm(a1 - a2, ord=1) # returns 2,
Refer:
作者:虚生 出处:https://www.cnblogs.com/dylancao/ 以音频和传感器算法为核心的智能可穿戴产品解决方案提供商 ,提供可穿戴智能软硬件解决方案的设计,开发和咨询服务。 勾搭热线:邮箱:1173496664@qq.com weixin:18019245820 市场技术对接群:347609188 |
![]() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战