0
0
NumPydata~5 mins

np.empty() for uninitialized arrays in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.empty() do in NumPy?

np.empty() creates a new array of a given shape and type, but does not initialize the entries. The values in the array are random and depend on the memory state.

Click to reveal answer
beginner
Why should you be careful when using np.empty()?

Because the array is uninitialized, it contains whatever data was already in memory. This means the values are unpredictable and should not be used before setting them explicitly.

Click to reveal answer
beginner
How is np.empty() different from np.zeros()?

np.zeros() creates an array filled with zeros, initializing all values. np.empty() creates an array without initializing values, so it is faster but contains garbage data.

Click to reveal answer
beginner
Show an example of creating an uninitialized 2x3 array using np.empty().
<pre>import numpy as np
arr = np.empty((2, 3))
print(arr)</pre><p>This prints a 2 by 3 array with random values from memory.</p>
Click to reveal answer
intermediate
When is it useful to use np.empty()?

It is useful when you want to create an array quickly and will fill all values later, so you don't waste time initializing with zeros or other values.

Click to reveal answer
What does np.empty((3,3)) return?
AA 3x3 array filled with zeros
BA 3x3 array filled with ones
CA 3x3 array with uninitialized random values
DAn error because values are not specified
Why might np.empty() be faster than np.zeros()?
ABecause it does not initialize the array values
BBecause it fills the array with zeros faster
CBecause it uses less memory
DBecause it creates a smaller array
What should you do before using values from an array created with np.empty()?
AConvert it to a list
BNothing, values are ready to use
CCall <code>np.zeros()</code> on it
DFill or assign values explicitly
Which of these is a correct way to create an uninitialized array of shape (4,)?
A<code>np.empty(4)</code>
BAll of the above
C<code>np.empty([4])</code>
D<code>np.empty((4,))</code>
If you want an array initialized with zeros, which function should you use instead of np.empty()?
Anp.zeros()
Bnp.full()
Cnp.random()
Dnp.ones()
Explain what np.empty() does and why its output values are unpredictable.
Think about memory allocation and initialization.
You got /4 concepts.
    Describe a situation where using np.empty() is better than np.zeros().
    Consider when initialization is not needed.
    You got /4 concepts.