1

While generating a linspace array in Numpy we get an array of the form (len(array), ), i.e. it doesn't have any 2nd dimension. How do I generate a similar array and initialize it using Numpy zeros? Because it takes a 2nd argument, like 1, so I get (len(array), 1) while initializing, which I wanted to avoid if possible.

Eg. np.linspace(0,10,5) = [0, 2.5, 5, 7.5, 10] ; It's array dimension is (5, ).

On the other hand, a zeros array is defined as np.zeros((5,1)) and our output is a vector [0 0 0 0 0] ^ (Transpose). I wanted to be a flat array not like a vector.

Is there a way?

2
  • 1
    It is not entirely clear to me what you want to achieve. Please updated with an example with input and expected output. Commented Mar 13, 2017 at 13:05
  • 1
    @WillemVanOnsem Is it fine now? Commented Mar 13, 2017 at 13:17

1 Answer 1

2

your first argument (5,1) is defining the shape of the array as a 5x1 explicitly 2d shape. Just pass (5,), or more explicitly as follows:

import numpy as np
z = np.zeros(shape=(5,), dtype=float)
print(z)
print(z.shape)

output is:

[ 0.  0.  0.  0.  0.]
(5,)
Sign up to request clarification or add additional context in comments.

1 Comment

Use shape=5 or shape=(5,). The 2nd is a 1 element tuple.

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.