Contenido del Curso
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()
.
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:
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 thedtype
.
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:
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 thedtype
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:
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.
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
.
¡Gracias por tus comentarios!
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:
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 thedtype
.
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:
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 thedtype
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:
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.
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
.
¡Gracias por tus comentarios!
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:
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 thedtype
.
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:
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 thedtype
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:
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.
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
.
¡Gracias por tus comentarios!
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:
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 thedtype
.
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:
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 thedtype
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:
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.
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
.