python 3.x - How can I do element-wise arithmetic on Numpy matrices? -


i using numpy's matlib style matrices particular algorithm. means multiplication operator * performs equivalent of ndarray's dot():

>>> import numpy.matlib nm >>> = nm.asmatrix([[1,1,1],[1,1,1],[1,1,1]]) >>> b = nm.asmatrix([[1,0,0],[0,1,0],[0,0,1]]) >>> * b matrix([[1, 1, 1],         [1, 1, 1],         [1, 1, 1]]) 

is there method perform element-wise arithmetic, * operator on ndarrays?

you use np.multiply:

>>> = np.matrix(np.random.rand(3,3)) >>> b = np.matrix(np.random.rand(3,3)) >>> * b matrix([[ 1.29029129,  0.53126365,  2.12109815],         [ 0.99370991,  0.55737572,  1.9167072 ],         [ 0.76268194,  0.43509462,  1.48640178]]) >>> np.asarray(a) * np.asarray(b) array([[ 0.67445535,  0.12609799,  0.7051103 ],        [ 0.00131878,  0.42079486,  0.5223201 ],        [ 0.65558303,  0.03020335,  0.16753354]]) >>> np.multiply(a, b) matrix([[ 0.67445535,  0.12609799,  0.7051103 ],         [ 0.00131878,  0.42079486,  0.5223201 ],         [ 0.65558303,  0.03020335,  0.16753354]]) 

it's little unusual want perform elementwise multiplication on same object you're performing matrix multiplication on, know that. :^) might worthwhile seeing if algorithm has nice np.einsum description, though.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -