In this article, you’ll learn how to perform 12 basic operations using NumPy.
Using These NumPy Examples
You can run the examples in this article by entering the code directly into the python interpreter. Launch it in interactive mode, from the command line, to do so.
You can also access a Python Notebook file containing the complete source code from this GitHub repository.
1. How to Import NumPy as np and Print the Version Number
You need to use the import keyword to import any library in Python. NumPy is typically imported under the np alias. With this approach, you can refer to the NumPy package as np instead of numpy.
Output:
2. How to Create a NumPy ndarray Object
The array object in NumPy is called ndarray. You can create the NumPy ndarray object using the array() method. The array() method accepts a list, tuple, or an array-like object.
Using a Tuple to Create a NumPy Array
Output:
Using a List to Create a NumPy Array
Output:
3. How to Create 0D, 1D, 2D, 3D, and N-Dimensional NumPy Arrays
0D Arrays
Each element of an array is a 0D array.
Output:
1D Arrays
Arrays that have 0D arrays as their elements are called 1D arrays.
Output:
2D Arrays
Arrays that have 1D arrays as their elements are called 2D arrays.
Output:
3D Arrays
Arrays that have 2D arrays (matrices) as their elements are called 3D arrays.
Output:
n-Dimensional Arrays
You can create an array of any dimension using the ndmin argument.
Output:
4. How to Check the Dimensions of an Array
You can find the dimensions of an array using the ndim attribute.
Output:
5. How to Access the Elements of 1D, 2D, and 3D Arrays
You can access an array element using its index number. For 2D and 3D arrays, you need to use comma-separated integers representing the index of each dimension.
Output:
Note: NumPy arrays also support negative indexing.
6. How to Check the Data Type of the NumPy Array Object
You can check the data type of the NumPy array object using the dtype property.
Output:
Note:
NumPy uses the following characters to represent the built-in data types:
i — integer (signed) b — boolean O — object S — string u — unsigned integer f — float c — complex float m — timedelta M — datetime U — unicode string V — raw data (void)
7. How to Change the Data Type of a NumPy Array
You can change the data type of a NumPy array using the astype(data_type) method. This method accepts the data type as a parameter and creates a new copy of the array. You can specify the data type using characters like ‘b’ for boolean, ‘i’ for integer, ‘f’ for float, etc.
Converting an Integer Array to a Float Array
Output:
Converting a Float Array to an Integer Array
Output:
8. How to Copy a NumPy Array Into Another Array
You can copy a NumPy array into another array using the np.copy() function. This function returns an array copy of the given object.
Output:
9. How to Find the Shape of a NumPy Array
The shape of an array refers to the number of elements in each dimension. You can find the shape of an array using the shape attribute. It returns a tuple whose elements give the lengths of the corresponding array dimensions.
Output:
10. How to Reshape a NumPy Array
Reshaping an array means changing its shape. Note that you cannot reshape an array to an arbitrary shape. The number of elements required for reshaping must be the same in both shapes.
Output:
In the above example, a 1D array is reshaped to a 2D array.
11. How to Flatten a NumPy Array
Flattening an array means converting a multidimensional array into a 1D array. You can flatten an array using reshape(-1).
Output:
Note: You can also flatten an array using other methods like numpy.ndarray.flatten() and numpy.ravel().
12. How to Sort a NumPy Array
You can sort a NumPy array using the numpy.sort() function.
Sorting 1D Array of Integers
Output:
Sorting 1D Array of Strings
Output:
Sorting 2D Array of Integers
Output:
Make Your Code Robust Using Built-In Methods and Functions
Python is one of the most popular programming languages. It’s used in various domains like web development, scientific and numeric applications, software development, and game development. It’s always good to know about built-in methods and functions in Python. They can shorten your code and increase its efficiency.