Basic Array Creation
A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of the 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, and only them.
Note
You should create NumPy arrays only from lists in all the tasks throughout our course.
1234567import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3, 2, 6, 1]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3, 2, 6, 1)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
Specifying Data Type
The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype
parameter:
1234567import 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.
The most 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.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 3.7
Basic Array Creation
Свайпніть щоб показати меню
A NumPy array is an efficient, multidimensional container for storing and manipulating large datasets of the 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, and only them.
Note
You should create NumPy arrays only from lists in all the tasks throughout our course.
1234567import numpy as np # Creating an array from list array_from_list = np.array([1, 2, 3, 2, 6, 1]) # Creating an array from tuple array_from_tuple = np.array((1, 2, 3, 2, 6, 1)) print(f'Array from list: {array_from_list}') print(f'Array from tuple: {array_from_tuple}')
Specifying Data Type
The data type of the array elements is defined implicitly; however, you can specify it explicitly with the dtype
parameter:
1234567import 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.
The most 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.
Дякуємо за ваш відгук!