This tutorial aims to introduce the concept of NumPy arrays, how to create them, and how to use them for efficient data manipulation.
By the end of this tutorial, you will be able to understand what NumPy arrays are, create them, and manipulate data using various NumPy methods.
NumPy, short for Numerical Python, is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy, dimensions are called axes.
You can create an array with NumPy using the numpy.array()
function. The function takes one mandatory argument, which is a list or a tuple, and produces a NumPy array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
NumPy provides a large set of numeric datatypes that you can use to construct arrays. NumPy tries to guess a datatype when you create an array, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='S')
print(arr)
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
In this example, we import the numpy package and create an array of integers using the np.array()
function. The output will be [1 2 3 4 5]
.
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='S')
print(arr)
Here, we create an array of strings from a list of integers by specifying the dtype
as 'S'. The output will be [b'1' b'2' b'3' b'4']
.
In this tutorial, we learned what NumPy arrays are, how to create them, and how to manipulate data using NumPy. Next, you could learn more about NumPy's other functions, such as mathematical functions, and how to use them with arrays.
Create a NumPy array from a list of your choice and print it.
Create a NumPy array with explicit data type from a list of floats.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr)
This will print [10 20 30 40 50]
.
import numpy as np
arr = np.array([1.1, 2.2, 3.3, 4.4], dtype='int')
print(arr)
This will print [1 2 3 4]
as we explicitly specified the data type to be integer. Consequently, the decimal parts were truncated.