Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
General Array Creation Functions | NumPy Basics
Ultimate NumPy

General Array Creation FunctionsGeneral Array Creation Functions

NumPy also has array creation functions that can automatically create an array of a given shape (dimensions). Here are the most common ones:

  • zeros();
  • ones();
  • full().

There are also functions that create random arrays of given shapes; however, we'll discuss them separately in the next chapter.

zeros()

The name of this function speaks for itself: it creates an array of zeros of a given shape. The shape of the array is specified via the shape parameter and can either be an integer (size of a 1D array) or a tuple of integers for higher-dimensional arrays.

Let’s take a look at an example:

As you can see, we can also specify the dtype parameter in the same way we did for other types of arrays.

Note

numpy.zeros() is often used as a placeholder to initialize arrays of a given shape, which will be later filled with other values. Therefore, be careful when explicitly specifying the dtype.

ones()

This function is similar to the zeros() function; however, instead of an array of zeros, it creates an array of ones. Without further ado, let’s see it in action:

In terms of syntax, everything here is the same as with the zeros() function.

Note

numpy.ones() is also often used as a placeholder to initialize arrays of a given shape, so be cautious with the dtype as well.

full()

The numpy.full() function is similar to the functions mentioned above; however, it has a second parameter, fill_value, to specify the value to fill the array with. Its first parameter, shape, can be either an integer or a tuple of integers:

More Applications

All of these functions have more use cases than simply being placeholders. They are quite often used directly in mathematical operations in linear algebra. They can be applied in various fields of machine and deep learning, such as image processing.

If you want to explore more about these functions, feel free to refer to their documentation: zeros, ones, and full.

Task

  1. Create a one-dimensional array of zeros with a size of 5 and assign it to zeros_array_1d.
  2. Create a two-dimensional array of zeros with a shape of 2x4 and assign it to zeros_array_2d.
  3. Create a one-dimensional array of ones with a size of 3 and assign it to ones_array_1d.
  4. Create a two-dimensional array of ones with a shape of 2x3 and assign it to ones_array_2d.
  5. Create a two-dimensional array of sevens with a shape of 2x2 and assign it to sevens_array_2d.

Everything was clear?

Section 1. Chapter 6
toggle bottom row
course content

Course Content

Ultimate NumPy

General Array Creation FunctionsGeneral Array Creation Functions

NumPy also has array creation functions that can automatically create an array of a given shape (dimensions). Here are the most common ones:

  • zeros();
  • ones();
  • full().

There are also functions that create random arrays of given shapes; however, we'll discuss them separately in the next chapter.

zeros()

The name of this function speaks for itself: it creates an array of zeros of a given shape. The shape of the array is specified via the shape parameter and can either be an integer (size of a 1D array) or a tuple of integers for higher-dimensional arrays.

Let’s take a look at an example:

As you can see, we can also specify the dtype parameter in the same way we did for other types of arrays.

Note

numpy.zeros() is often used as a placeholder to initialize arrays of a given shape, which will be later filled with other values. Therefore, be careful when explicitly specifying the dtype.

ones()

This function is similar to the zeros() function; however, instead of an array of zeros, it creates an array of ones. Without further ado, let’s see it in action:

In terms of syntax, everything here is the same as with the zeros() function.

Note

numpy.ones() is also often used as a placeholder to initialize arrays of a given shape, so be cautious with the dtype as well.

full()

The numpy.full() function is similar to the functions mentioned above; however, it has a second parameter, fill_value, to specify the value to fill the array with. Its first parameter, shape, can be either an integer or a tuple of integers:

More Applications

All of these functions have more use cases than simply being placeholders. They are quite often used directly in mathematical operations in linear algebra. They can be applied in various fields of machine and deep learning, such as image processing.

If you want to explore more about these functions, feel free to refer to their documentation: zeros, ones, and full.

Task

  1. Create a one-dimensional array of zeros with a size of 5 and assign it to zeros_array_1d.
  2. Create a two-dimensional array of zeros with a shape of 2x4 and assign it to zeros_array_2d.
  3. Create a one-dimensional array of ones with a size of 3 and assign it to ones_array_1d.
  4. Create a two-dimensional array of ones with a shape of 2x3 and assign it to ones_array_2d.
  5. Create a two-dimensional array of sevens with a shape of 2x2 and assign it to sevens_array_2d.

Everything was clear?

Section 1. Chapter 6
toggle bottom row
some-alt