Зміст курсу
Ultimate NumPy
Ultimate 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()
.
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.
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)
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.
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)
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:
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.
Swipe to show code editor
- Create a one-dimensional array of zeros with a size of
5
and assign it tozeros_array_1d
. - Create a two-dimensional array of zeros with a shape of
2x4
and assign it tozeros_array_2d
. - Create a one-dimensional array of ones with a size of
3
and assign it toones_array_1d
. - Create a two-dimensional array of ones with a shape of
2x3
and assign it toones_array_2d
. - Create a two-dimensional array of sevens with a shape of
2x2
and assign it tosevens_array_2d
.
Дякуємо за ваш відгук!
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.
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)
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.
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)
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:
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.
Swipe to show code editor
- Create a one-dimensional array of zeros with a size of
5
and assign it tozeros_array_1d
. - Create a two-dimensional array of zeros with a shape of
2x4
and assign it tozeros_array_2d
. - Create a one-dimensional array of ones with a size of
3
and assign it toones_array_1d
. - Create a two-dimensional array of ones with a shape of
2x3
and assign it toones_array_2d
. - Create a two-dimensional array of sevens with a shape of
2x2
and assign it tosevens_array_2d
.
Дякуємо за ваш відгук!