Contenido del Curso
Ultimate NumPy
Ultimate NumPy
Basic Array Creation
A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of homogeneous (same) data types. Although they are similar to Python lists, they are more memory-efficient and allow for high-performance mathematical and numerical operations.
Now, it’s time to create your first NumPy arrays. The most straightforward way to do this is by using the array()
function, passing either a list
or a tuple
as its argument.
Note
You should create NumPy arrays only from lists in all the tasks throughout our course.
Let’s take a look at an example:
import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
As you can see, these two NumPy arrays are equal.
Specifying Data Type
The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype
parameter:
import numpy as np # Creating an integer array without specifying dtype array_1 = np.array([1, 2, 3]) # Creating an integer array with setting dtype to 1-byte integer array_2 = np.array([1, 2, 3], dtype=np.int8) print(f'First array dtype: {array_1.dtype}') print(f'Second array dtype: {array_2.dtype}')
The first integer array uses the default int64
data type, which is an 8-byte integer. The second array uses int8
, a 1-byte integer.
Other common NumPy data types include numpy.float16
, numpy.float32
, and numpy.float64
, which store 2-byte, 4-byte, and 8-byte floating point numbers, respectively.
Using int8
for small integers is more memory-efficient. However, be careful when choosing the data type (dtype
) to avoid overflow if the numbers are too large for the specified type.
Here is a list of possible data types in NumPy:
Swipe to show code editor
Create a 1D array named float_array
:
- Use the correct function to create a NumPy 1D array;
- Create an array with two elements (use a
list
as the first argument):1.32
,4.6
in this order; - Set the data type of its elements to
np.float16
via specifying the second argument.
¡Gracias por tus comentarios!
Basic Array Creation
A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of homogeneous (same) data types. Although they are similar to Python lists, they are more memory-efficient and allow for high-performance mathematical and numerical operations.
Now, it’s time to create your first NumPy arrays. The most straightforward way to do this is by using the array()
function, passing either a list
or a tuple
as its argument.
Note
You should create NumPy arrays only from lists in all the tasks throughout our course.
Let’s take a look at an example:
import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
As you can see, these two NumPy arrays are equal.
Specifying Data Type
The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype
parameter:
import numpy as np # Creating an integer array without specifying dtype array_1 = np.array([1, 2, 3]) # Creating an integer array with setting dtype to 1-byte integer array_2 = np.array([1, 2, 3], dtype=np.int8) print(f'First array dtype: {array_1.dtype}') print(f'Second array dtype: {array_2.dtype}')
The first integer array uses the default int64
data type, which is an 8-byte integer. The second array uses int8
, a 1-byte integer.
Other common NumPy data types include numpy.float16
, numpy.float32
, and numpy.float64
, which store 2-byte, 4-byte, and 8-byte floating point numbers, respectively.
Using int8
for small integers is more memory-efficient. However, be careful when choosing the data type (dtype
) to avoid overflow if the numbers are too large for the specified type.
Here is a list of possible data types in NumPy:
Swipe to show code editor
Create a 1D array named float_array
:
- Use the correct function to create a NumPy 1D array;
- Create an array with two elements (use a
list
as the first argument):1.32
,4.6
in this order; - Set the data type of its elements to
np.float16
via specifying the second argument.
¡Gracias por tus comentarios!
Basic Array Creation
A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of homogeneous (same) data types. Although they are similar to Python lists, they are more memory-efficient and allow for high-performance mathematical and numerical operations.
Now, it’s time to create your first NumPy arrays. The most straightforward way to do this is by using the array()
function, passing either a list
or a tuple
as its argument.
Note
You should create NumPy arrays only from lists in all the tasks throughout our course.
Let’s take a look at an example:
import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
As you can see, these two NumPy arrays are equal.
Specifying Data Type
The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype
parameter:
import numpy as np # Creating an integer array without specifying dtype array_1 = np.array([1, 2, 3]) # Creating an integer array with setting dtype to 1-byte integer array_2 = np.array([1, 2, 3], dtype=np.int8) print(f'First array dtype: {array_1.dtype}') print(f'Second array dtype: {array_2.dtype}')
The first integer array uses the default int64
data type, which is an 8-byte integer. The second array uses int8
, a 1-byte integer.
Other common NumPy data types include numpy.float16
, numpy.float32
, and numpy.float64
, which store 2-byte, 4-byte, and 8-byte floating point numbers, respectively.
Using int8
for small integers is more memory-efficient. However, be careful when choosing the data type (dtype
) to avoid overflow if the numbers are too large for the specified type.
Here is a list of possible data types in NumPy:
Swipe to show code editor
Create a 1D array named float_array
:
- Use the correct function to create a NumPy 1D array;
- Create an array with two elements (use a
list
as the first argument):1.32
,4.6
in this order; - Set the data type of its elements to
np.float16
via specifying the second argument.
¡Gracias por tus comentarios!
A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of homogeneous (same) data types. Although they are similar to Python lists, they are more memory-efficient and allow for high-performance mathematical and numerical operations.
Now, it’s time to create your first NumPy arrays. The most straightforward way to do this is by using the array()
function, passing either a list
or a tuple
as its argument.
Note
You should create NumPy arrays only from lists in all the tasks throughout our course.
Let’s take a look at an example:
import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
As you can see, these two NumPy arrays are equal.
Specifying Data Type
The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype
parameter:
import numpy as np # Creating an integer array without specifying dtype array_1 = np.array([1, 2, 3]) # Creating an integer array with setting dtype to 1-byte integer array_2 = np.array([1, 2, 3], dtype=np.int8) print(f'First array dtype: {array_1.dtype}') print(f'Second array dtype: {array_2.dtype}')
The first integer array uses the default int64
data type, which is an 8-byte integer. The second array uses int8
, a 1-byte integer.
Other common NumPy data types include numpy.float16
, numpy.float32
, and numpy.float64
, which store 2-byte, 4-byte, and 8-byte floating point numbers, respectively.
Using int8
for small integers is more memory-efficient. However, be careful when choosing the data type (dtype
) to avoid overflow if the numbers are too large for the specified type.
Here is a list of possible data types in NumPy:
Swipe to show code editor
Create a 1D array named float_array
:
- Use the correct function to create a NumPy 1D array;
- Create an array with two elements (use a
list
as the first argument):1.32
,4.6
in this order; - Set the data type of its elements to
np.float16
via specifying the second argument.