0

This is a simple question. I am trying to concatenate a list to a numpy nd array, but I cannot figure out how to do it.

import numpy as np
t = list(range(2))
a = np.random.rand(2,4)

t = np.array(t)
result = np.concatenate(t,a)

>> ValueError: all the input arrays must have same number of dimensions
1
  • np.random.rand(2,4) generate a 2 by 4 random array. Is that what you want? If so, what is the desired concatenation? Commented Feb 6, 2018 at 17:33

1 Answer 1

1

You can concatenate an array of shape (2,) and an array of shape (2, 4) like this:

import numpy as np

t = list(range(2))
a = np.random.rand(2,4)
t = np.array(t)

result = np.hstack((t[:, None],a))

# array([[ 0.        ,  0.54789168,  0.60478776,  0.03923133,  0.51515207],
#        [ 1.        ,  0.52476824,  0.030079  ,  0.95157973,  0.99690973]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.