I am trying to cast my data into an ndarray. Although I get what I want but while testing I am not able to figure out why it gives me this output.
I get the output:
np.ndarray(shape = (2,1),buffer = np.array([1,2,3,4]))
array([[4.24399158e-314],
[8.48798317e-314]])
However for this block I am able to cast my random numbers the way I want.
def x_mat(n,m):
np.random.seed(0)
randoms = np.random.normal(size=n*m)
print(randoms)
rand_mat = np.ndarray(shape = (n,m),buffer = randoms)
return(rand_mat)
x_mat(5,2)
to give:
[ 1.76405235 0.40015721 0.97873798 2.2408932 1.86755799 -0.97727788
0.95008842 -0.15135721 -0.10321885 0.4105985 ]
array([[ 1.76405235, 0.40015721],
[ 0.97873798, 2.2408932 ],
[ 1.86755799, -0.97727788],
[ 0.95008842, -0.15135721],
[-0.10321885, 0.4105985 ]])
If someone can help me understand this behavior. It does the correct thing in the second case but I find the first case as something that is not straight forward. Why is the first case the way it is?
np.ndarray? Ordinary mortals don't normally need it.dtypeofrandomsisfloat, same as the default forndarray.np.matrixdoesn't add any computational power; never did. Its*multiply is matrix multiplication, but so isnp.dotandmatmul(and the@operator). It's just as easy to make 2dndarray- just use thenp.array([[1,2,3],[4,5,6]])syntax orreshape. You rarely needndarray.