Question 1 - C1W1, just curious

Question from Jupyter Lab1;
Is there a difference between np.zeros() and np.empty()?
D. np.empty() outputs an uninitialized array, but np.zeros() outputs an initialized array of value zero.

I get the idea, particularly from a memory allocation perspective, but when I run for example something like this.

empt_arr = np.empty(3) == np.zeros(3)
print(empt_arr)
Result: [True True True]

I originally considered the possibility that during a conditional, uninitialized elements are automcatically initialized as 0. However, when I tried the following;

empt_arr = np.empty(3) == np.empty(3)
print(empt_arr)
Result: [False False False]

That didn’t seem to be the case, can someone explain to me what the difference here is please?

Hi @Kenta_Desu

Here is the link to the reference menu. The object returned by np.empty() is initialised to none, whilst object returned by np.zeros() is initialised to zeros.

3 Likes

thank you @Kic

Seems I wasn’t quite understanding how NumPy gets memory from the memory allocator. The unitialized array examples with floats as its elements from your link helped.
So, when np.empty gets memory, it interprets the bytes in that string as a float, and gets some number.

That to me would explain why np.empty() == np.empty() is False, but then why is np.empty() == np.zeros() True? Guessing it could be a coincidence, but not 100%.

Sorry all, turns out it was just a coincidence in a specific Jupyter notebook I was using. When replicated locally and on an online compiler, the .empty == .zeros indeed returned False on some occassions. So, it was a coincidence.