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
course content

Зміст курсу

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

General 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:

123456789101112
import numpy as np # Сreating a 1D array of zeros with 5 elements zeros_1d = np.zeros(5) print(zeros_1d) print('-' * 16) # Сreating a 1D array of zeros with specifying dtype zeros_1d_int = np.zeros(5, dtype=np.int8) print(zeros_1d_int) print('-' * 16) # Сreating a 2D array of zeros of shape 5x3 zeros_2d = np.zeros((5, 3)) print(zeros_2d)

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:

123456789101112
import numpy as np # Сreating a 1D array of ones with 5 elements ones_1d = np.ones(5) print(ones_1d) print('-' * 16) # Сreating a 1D array of ones with specifying dtype ones_1d_int = np.ones(5, dtype=np.int8) print(ones_1d_int) print('-' * 16) # Сreating a 2D array of ones of shape 5x3 ones_2d = np.ones((5, 3)) print(ones_2d)

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:

1234567
import numpy as np # Сreate an array of fours of size 5 array_fours_1d = np.full(5, 4) # Сreate an array of fives of shape 4x2 array_fives_2d = np.full((4, 2), 5) print(f'1D fours array: {array_fours_1d}') print(f'2D fives array:\n{array_fives_2d}')

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.

Завдання

  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.

Завдання

  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.

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row

General 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:

123456789101112
import numpy as np # Сreating a 1D array of zeros with 5 elements zeros_1d = np.zeros(5) print(zeros_1d) print('-' * 16) # Сreating a 1D array of zeros with specifying dtype zeros_1d_int = np.zeros(5, dtype=np.int8) print(zeros_1d_int) print('-' * 16) # Сreating a 2D array of zeros of shape 5x3 zeros_2d = np.zeros((5, 3)) print(zeros_2d)

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:

123456789101112
import numpy as np # Сreating a 1D array of ones with 5 elements ones_1d = np.ones(5) print(ones_1d) print('-' * 16) # Сreating a 1D array of ones with specifying dtype ones_1d_int = np.ones(5, dtype=np.int8) print(ones_1d_int) print('-' * 16) # Сreating a 2D array of ones of shape 5x3 ones_2d = np.ones((5, 3)) print(ones_2d)

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:

1234567
import numpy as np # Сreate an array of fours of size 5 array_fours_1d = np.full(5, 4) # Сreate an array of fives of shape 4x2 array_fives_2d = np.full((4, 2), 5) print(f'1D fours array: {array_fours_1d}') print(f'2D fives array:\n{array_fives_2d}')

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.

Завдання

  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.

Завдання

  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.

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row

General 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:

123456789101112
import numpy as np # Сreating a 1D array of zeros with 5 elements zeros_1d = np.zeros(5) print(zeros_1d) print('-' * 16) # Сreating a 1D array of zeros with specifying dtype zeros_1d_int = np.zeros(5, dtype=np.int8) print(zeros_1d_int) print('-' * 16) # Сreating a 2D array of zeros of shape 5x3 zeros_2d = np.zeros((5, 3)) print(zeros_2d)

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:

123456789101112
import numpy as np # Сreating a 1D array of ones with 5 elements ones_1d = np.ones(5) print(ones_1d) print('-' * 16) # Сreating a 1D array of ones with specifying dtype ones_1d_int = np.ones(5, dtype=np.int8) print(ones_1d_int) print('-' * 16) # Сreating a 2D array of ones of shape 5x3 ones_2d = np.ones((5, 3)) print(ones_2d)

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:

1234567
import numpy as np # Сreate an array of fours of size 5 array_fours_1d = np.full(5, 4) # Сreate an array of fives of shape 4x2 array_fives_2d = np.full((4, 2), 5) print(f'1D fours array: {array_fours_1d}') print(f'2D fives array:\n{array_fives_2d}')

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.

Завдання

  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.

Завдання

  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.

Все було зрозуміло?

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:

123456789101112
import numpy as np # Сreating a 1D array of zeros with 5 elements zeros_1d = np.zeros(5) print(zeros_1d) print('-' * 16) # Сreating a 1D array of zeros with specifying dtype zeros_1d_int = np.zeros(5, dtype=np.int8) print(zeros_1d_int) print('-' * 16) # Сreating a 2D array of zeros of shape 5x3 zeros_2d = np.zeros((5, 3)) print(zeros_2d)

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:

123456789101112
import numpy as np # Сreating a 1D array of ones with 5 elements ones_1d = np.ones(5) print(ones_1d) print('-' * 16) # Сreating a 1D array of ones with specifying dtype ones_1d_int = np.ones(5, dtype=np.int8) print(ones_1d_int) print('-' * 16) # Сreating a 2D array of ones of shape 5x3 ones_2d = np.ones((5, 3)) print(ones_2d)

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:

1234567
import numpy as np # Сreate an array of fours of size 5 array_fours_1d = np.full(5, 4) # Сreate an array of fives of shape 4x2 array_fives_2d = np.full((4, 2), 5) print(f'1D fours array: {array_fours_1d}') print(f'2D fives array:\n{array_fives_2d}')

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.

Завдання

  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.

Секція 1. Розділ 6
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt