python - how to normalize array numpy? -


i have norm 1 numpy array. looking equivalent version of function

def normalize(v):     norm=np.linalg.norm(v)     if norm==0:         return v     return v/norm 

is there in skearn or numpy? function works in situation v 0 vector.

if you're using scikit-learn can use sklearn.preprocessing.normalize:

import numpy np sklearn.preprocessing import normalize  x = np.random.rand(1000)*10 norm1 = x / np.linalg.norm(x) norm2 = normalize(x[:,np.newaxis], axis=0).ravel() print np.all(norm1 == norm2) # true 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -