python matrix operation with numpy -


i have python array such as:

[[1], [2], [3], [4] ]  want make to: [ [1 0 0 0],   [2 0 0 0 ],   [3 0 0 0],   [4 0 0 0] ] 

what python way this? suppose use numpy.

create row vector using numpy.eye.

>>> import numpy np >>> = np.array([[1],[2],[3],[4]]) >>> b = np.eye(1, 4) >>> b array([[ 1.,  0.,  0.,  0.]] >>> c = * b >>> c array([[ 1.,  0.,  0.,  0.],        [ 2.,  0.,  0.,  0.],        [ 3.,  0.,  0.,  0.],        [ 4.,  0.,  0.,  0.]]) 

concatenation of zeros might faster since example uses matrix multiplication achieve required result , not allocation of required size.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -