范數(shù)的歸一化



程序代碼與測試驗證
import numpy as np
def normalize(v,axis,order):
? ? l2=np.linalg.norm(v,axis=axis,ord=order,keepdims=True)
? ? l=v/l2
? ? return l
c=np.array([[1,2,3,10,19],[4,5,6,17,28],[11,21,7,45,8]])
print(normalize(c,axis=0,order=1),'\n')
print(normalize(c,axis=1,order=1),'\n')
print(normalize(c,axis=0,order=2),'\n')
print(normalize(c,axis=1,order=2),'\n')

print(c/np.linalg.norm(c,ord=1,axis=0,keepdims=True),'\n')
print(c/np.linalg.norm(c,ord=1,axis=1,keepdims=True),'\n')
print(c/np.linalg.norm(c,ord=2,axis=0,keepdims=True),'\n')
print(c/np.linalg.norm(c,ord=2,axis=1,keepdims=True),'\n')

標(biāo)簽: