Home > Software engineering >  automatic ndarray concatenation along with a loop
automatic ndarray concatenation along with a loop

Time:04-28

I have the following code implementation to perform the quantile computation over the numpy array.

x1 = np.random.rand(10,3,2)
for i in np.arange(0.1,0.2,0.05):
    x= np.quantile(x1, i,axis=0)
    print(x.shape)

it can be seen that the shape of the x array generated during the loop is (3, 2). What I need is to concatenate these generated x array together. For this case, I want to have a resulting concatenated array with shape (2,3,2). How to perform this kind of concatenation.

CodePudding user response:

You can pass all the quantiles you want to compute to np.quantile as a list or array-like:

In [2]: x = np.random.rand(10,3,2)

In [3]: np.quantile(x, np.arange(0.1, 0.2, 0.05), axis=0).shape
Out[3]: (2, 3, 2)

In [4]: np.quantile(x, np.arange(0.1, 0.2, 0.05), axis=0)
Out[4]:
array([[[0.25278165, 0.20236969],
        [0.34814603, 0.22170499],
        [0.12397841, 0.08714209]],

       [[0.29549464, 0.21931808],
        [0.35593299, 0.25053593],
        [0.17244185, 0.11209505]]])
  • Related