Creating an empty array with a given shape

To create a 3 x 2 array of doubles, use the following code:

x = np.empty((3,2))

This creates the following array:

array([[ 0.,  0.],
[ 0., 0.],
[ 0., 0.]])

Notice that the array is not actually empty, but filled with arbitrary values. The dtype of the array is, by default, float64. A different type can be specified, as in the following example:

x = np.empty((2,4), dtype=np.int16)

This produces the following array:

array([[0, 0, 0, 0],
[1, 0, 0, 0]], dtype=int16)
It is important to notice that empty() will not initialize the array elements. The values will just be whatever is present in memory when the array is created. If you want an initialized array, use a function such as zeros().
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset