0

I'm using linspace to generate an array of 100 elements, the result's shape is (100,) but I want to obtain an array with the following shape (100, 1). How can I do it? I tried reshape but it does not give the required result. Here is an exmaple :

import numpy as np
om1 = np.linspace(-50, 50, 100);
om = np.random.randn(100,1);
print(shape(om1))
print(shape(om))

Output :

>>> (100, )
(100, 1)
1
  • Your code isn't valid python, there is no shape function. Did you mean om1.shape? Commented Oct 21, 2020 at 10:22

2 Answers 2

1

Doesn't the numpy array have a reshape method? https://numpy.org/doc/stable/reference/generated/numpy.ndarray.reshape.html#numpy.ndarray.reshape

om1 = np.linspace(-50, 50, 100).reshape( (100, 1) )
Sign up to request clarification or add additional context in comments.

3 Comments

That works for me ! Thanks @xtofl ! I was using reshape in the wrong way ig...
You could say that. Since I have made the same mistake, I dare to say that np.array.reshape has been given the wrong name. Had it been np.array.reshaped, many frustrating developer-hours would have been saved.
No, I just typed om=reshape(len(om),1) instead of om=om.reshape(len(om),1) I didn't pay attention sorry!
0

You can use reshape to add the missing dimension without knowing the length of your array

om1.reshape(-1,1)

With -1 as one dimension the length of that dimension is inferred.

1 Comment

Thanks @MichaelSzczesny ! That's a useful information!

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.