
This blog post introduces the NumPy library, covering how to import it, create one-dimensional, two-dimensional, and three-dimensional arrays, and perform basic operations such as checking size, shape, data type, and transposing arrays.
Welcome to the first part of our complete NumPy tutorial series. In this post, we will explore how to import the NumPy library and perform some basic operations. Let's dive in!
To start using NumPy, we first need to import it. The common convention is to import NumPy as np to avoid typing numpy repeatedly. Here’s how you can do it:
import numpy as np
Before we delve into NumPy arrays, it's essential to understand the difference between Python lists and NumPy arrays. A Python list can store one-dimensional data of any type, such as strings, booleans, integers, or floats. However, it is limited to one-dimensional data.
In contrast, NumPy allows us to create multi-dimensional arrays. This means we can store one-dimensional, two-dimensional, and even three-dimensional data in a single array.
To create a one-dimensional NumPy array, we use the following syntax:
array_1d = np.array([1, 2, 3, 4, 5])
print(array_1d)
For a two-dimensional array, we define an array of arrays:
array_2d = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(array_2d)
Similarly, a three-dimensional array is defined as an array of arrays of arrays:
array_3d = np.array([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]])
print(array_3d)
Once we have created our arrays, we can check their properties using various functions:
To check the type of an array, we can use the type() function:
print(type(array_1d)) # Output: <class 'numpy.ndarray'>
The size attribute returns the total number of elements in the array:
print(array_1d.size) # Output: 5
print(array_2d.size) # Output: 10
print(array_3d.size) # Output: 30
The shape attribute provides the dimensions of the array in terms of rows and columns:
print(array_1d.shape) # Output: (5,)
print(array_2d.shape) # Output: (2, 5)
print(array_3d.shape) # Output: (2, 2, 5)
To check the data type of the elements in the array, we use the dtype attribute:
print(array_1d.dtype) # Output: int64
You can also change the data type of an array. For example, if you want to interpret all elements as floats:
array_float = np.array([1.0, 2.0, 3.0], dtype=float)
print(array_float.dtype) # Output: float64
Transposing an array involves swapping its rows and columns. You can achieve this using the .T attribute:
transposed_array = array_2d.T
print(transposed_array)
This will convert rows into columns and vice versa, allowing for easier manipulation of data.
In this first part of the NumPy tutorial, we covered how to import the library, create one-dimensional, two-dimensional, and three-dimensional arrays, and perform basic operations such as checking size, shape, data type, and transposing arrays. Stay tuned for the next part, where we will explore more advanced features of NumPy. Keep coding and innovating!
Paste a YouTube link and let Magica create the key takeaways.
Summarize another video