-1

I have a function that takes 2 parameters and returns a shape of an array. The output should be like this: array(\[\[A\], \[B\], \[B\]\]), but I got it like this: array(\[\[A, B, C\]\]).

What is the methoud that I can use to return it like this: array(\[\[A\], \[B\], \[B\]\])

For example, initialise_array(3,1) will return an array of dimensions (3,1) that can look like this:

array([[0.37454012], [0.95071431], [0.73199394]])

My code:

def initialise_array(n_features, n_observations): 
    shape = np.random.rand(n_features, n_observations) 
    n_shape = shape.reshape(n_observations, n_features) 
    return n_shape

The output looks like array([[0.85546058, 0.70365786, 0.47417383]]) but I need to match the this array([[0.37454012], [0.95071431], [0.73199394]])

funie200
  • 3,688
  • 5
  • 21
  • 34
Sa'adoon
  • 9
  • 4
  • just do `return np.array([[A], [B], [B]])`. But you want to return B twice and discard C? –  Mar 22 '22 at 15:22
  • What 2 parameters? And how is array([[A], [B], [B]]) the shape of the arraay? This question makes zero sense, also no code .... – Josip Juros Mar 22 '22 at 15:24
  • @JosipJuros ,@Sembei Norimaki def initialise_array(n_features,n_observations): shape = np.random.rand(n_features,n_observations) n_shape = shape.reshape(n_observations,n_features) return n_shape the output looks like array([[0.85546058, 0.70365786, 0.47417383]]) but I need to match the this array([[0.37454012], [0.95071431], [0.73199394]]) – Sa'adoon Mar 22 '22 at 15:28
  • @mohammadsadoun You gotta be shitting me. You couldnt edit the question and post that code in it? – Josip Juros Mar 22 '22 at 15:32

1 Answers1

1
a=np.array([[1,2,3]])
print(repr(a))

a=a[0]
print(repr(a))

a=np.reshape(a,np.shape(a)+(1,))
print(repr(a))

Output :

array([[1, 2, 3]])
array([1, 2, 3])
array([[1],
       [2],
       [3]])

You can also do it by other ways :
Transforming a row vector into a column vector in Numpy

a[:,np.newaxis]
a.reshape(-1,1)
David Rouyre
  • 68
  • 1
  • 6
  • Congrats on your first answer! where you did provide answer to the OP, it would good to add a little explanation why does this work as expected. – Guy Mar 22 '22 at 22:03