2

In Python when using np.empty(), for example np.empty((3,1)) we get an array that is of size (3,1) but, in reality, it is not empty and it contains very small values (e.g., 1.7*(10^315)). Is possible to create an array that is really empty/have no values but have given dimensions/shape?

4
  • How do you define empty? Commented May 29, 2018 at 7:50
  • 1
    I think OP is looking for a variable that has shape but doesn't really have values. Commented May 29, 2018 at 7:52
  • Yes, what @Sraw said is what I mean by empty. Commented May 29, 2018 at 7:56
  • A numpy array always has the number of values implied by the shape. If np.empty confuses you, I suggest np.zerosinstead. Commented May 29, 2018 at 8:17

3 Answers 3

2

I'd suggest using np.full_like to choose the fill-value directly...

x = np.full_like((3, 1), None, dtype=object)

... of course the dtype you chose kind of defines what you mean by "empty"

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

Comments

1

I am guessing that by empty, you mean an array filled with zeros.

Use np.zeros() to create an array with zeros. np.empty() just allocates the array, so the numbers in there are garbage. It is provided as a way to even reduce the cost of setting the values to zero. But it is generally safer to use np.zeros().

Comments

1

I suggest to use np.nan. like shown below,

yourdata = np.empty((3,1)) * np.nan

(Or)

you can use np.zeros((3,1)). but it will fill all the values as zero. It is not intuitively well. I feel like using np.nan is best in practice.

Its all upto you and depends on your requirement.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.