Large array generation with numpy

I am new to python and numpy (I'm more used to R) and play with creating arrays and want to create a tall array where the first column is just a range with a custom step and the second column is unif random between 0 and 1.

I came up with below, but it seems very awkward and not particularly readable. Are there more efficient ways to achieve one result on one line?

import numpy as np

1stcol = np.array(np.arange(1,20,0.5), ndmin=2)
2ndcol = np.array(np.random.uniform(0,1,np.shape(d)[1]), ndmin=2)
tallmat = np.transpose(np.concatenate((d,e),axis=0))
+4
source share
2 answers

1stcol is not a valid variable name.

In terms of efficiency, this is hard to beat.

In [159]: d = np.array(np.arange(1,20,0.5), ndmin=2)
     ...: e = np.array(np.random.uniform(0,1,np.shape(d)[1]), ndmin=2)
     ...: tallmat = np.transpose(np.concatenate((d,e),axis=0))

Simpler expressions for dand e:

d = np.arange(1,20,0.5)[None,:]
e = np.random.uniform(0,1,d.shape)

You can build simpler 1d arrays

In [160]: a = np.arange(1,20,0.5)
     ...: b = np.random.uniform(0,1,np.shape(d)[1])

, , / . , . , "", , .

1d

np.column_stack((a,b))
np.c_[a,b]
np.stack((a,b),axis=-1)
np.array((a,b).T

d e - (N, 1) :

In [171]: d = np.arange(1,20,0.5)[:,None]
In [172]: e = np.random.uniform(0,1,d.shape)
In [173]: tallmat = np.concatenate((d,e), axis=1)
+1

, , np.vstack, np.row_stack np.dstack -

np.vstack((d,e)).T
np.row_stack((d,e)).T
np.dstack((d,e))[0]

1D np.column_stack, :

d = np.arange(1,20,0.5)
e = np.random.uniform(0,1,np.shape(d)[1])
tallmat = np.column_stack((d,e))
+3

Source: https://habr.com/ru/post/1672357/


All Articles