0
0
NumPydata~5 mins

np.ones() for one-filled arrays in NumPy - Cheat Sheet & Quick Revision

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

np.ones() creates a new array filled with ones. You specify the shape (size) of the array, and it returns an array where every element is 1.

Click to reveal answer
beginner
How do you create a 3x3 array filled with ones using np.ones()?

Use np.ones((3, 3)). The argument is a tuple that defines the shape: 3 rows and 3 columns.

Click to reveal answer
beginner
What data type does np.ones() use by default for the array elements?

By default, np.ones() creates an array of floats (floating-point numbers).

Click to reveal answer
intermediate
How can you create an integer array filled with ones using np.ones()?

Use the dtype parameter: np.ones((3, 3), dtype=int) creates a 3x3 array of integer ones.

Click to reveal answer
beginner
Why might you use np.ones() instead of manually creating an array of ones?

np.ones() is fast, simple, and efficient. It avoids loops and manual filling, making your code cleaner and faster.

Click to reveal answer
What does np.ones((2, 4)) produce?
AA 2x4 array filled with zeros
BA 4x2 array filled with ones
CA 2x4 array filled with ones
DA 2x4 array filled with random numbers
How do you specify the data type of the array elements in np.ones()?
AUsing the <code>shape</code> parameter
BUsing the <code>dtype</code> parameter
CUsing the <code>type</code> function after creation
DYou cannot specify data type
What is the default data type of elements created by np.ones()?
AString
BBoolean
CInteger
DFloat
Which of these is a correct way to create a 1D array of five ones?
AAll of the above
B<code>np.ones([5])</code>
C<code>np.ones((5,))</code>
D<code>np.ones(5)</code>
Why is np.ones() preferred over manually creating an array of ones with loops?
AIt is faster and cleaner
BIt is slower but easier
CIt uses more memory
DIt creates random values
Explain how to create a 2D array filled with ones using np.ones(). Include how to specify the shape and data type.
Think about the shape tuple and the dtype argument.
You got /4 concepts.
    Describe why using np.ones() is better than creating an array of ones with a for-loop in Python.
    Consider speed and code simplicity.
    You got /4 concepts.