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

book
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().

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.

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

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

ones()

This function is similar to the zeros() function, but instead of an array of zeros, it creates an array of ones.

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

full()

The numpy.full() function is similar to the functions mentioned above, but 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}')
copy

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.

Завдання
test

Swipe to show code editor

  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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

book
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().

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.

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

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

ones()

This function is similar to the zeros() function, but instead of an array of zeros, it creates an array of ones.

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

full()

The numpy.full() function is similar to the functions mentioned above, but 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}')
copy

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.

Завдання
test

Swipe to show code editor

  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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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