0

Is it possible to create a 2-dimensional NumPy array with 1 row and 2 columns (row vector)?

This is what I'm doing (from the documentation), but I'd like to know if it's possible to do it in one (easier) step:

X_new2 = np.array([8.5,156])
X_new2 = X_new2[np.newaxis, :]

I've also tried:

X_new2 = np.array([[8.5], [156]])

But this is returning a column instead.

2 Answers 2

2

You can use the following syntax to achieve the same result as in your example:

X_new2 = np.array([[8.5,156]])

(Notice the extra [ and ] to make the array the correct shape.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, those square brackets are hard to understand, I mean, when to open and close them and when to use them at all - I have hard time to get the logic
[[8.5,156]] by itself is a list containing one item, another list. The array display also mirrors the nested list. X_new2.tolist() actually returns a (nested) list.
1

try this:

y = np.expand_dims(x, axis=0)
print(y.shape)

1 Comment

thanks, but I get an error (x is unknown)

Your Answer

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