NumPy Cheat Sheet
Overview
Python is a versatile, powerful, and high-level programming language that allows us to do things from Web Development to Machine Learning. It is safe to say that Python is one of the most demanded languages when it comes to Software Engineering. Hence, mastering Python is one of the fundamentals for a budding Software Developer.
One of the most popular and extensive libraries in Python is the NumPy library. In this article, we will encapsulate the various functions and operations we can perform using NumPy in Python.
NumPy CheatSheet
As discussed above, Python's core library for scientific computing is called NumPy. It offers a multidimensional array object with outstanding performance as well as capabilities for interacting with these arrays.
In this NumPy cheat sheet, we will build a collection of tools and methods that NumPy users can utilize to carry out fundamental array manipulation and I/O operations.
Creating NumPy Arrays
The primary purpose of NumPy is to make working with linear data structures like arrays a whole easier. In this section, we will look at various methods to create and initialize an array using NumPy.
The array object in NumPy is the ndarray. By using the array() function, we can create and initialize arrays in NumPy.
Output
NumPy's array() method allows us to initialize multidimensional arrays as well. Apart from this, we can specify the data type of our array using the dtype parameter.
Output
Data Types
In Python, each value has a datatype. Since everything is an object in Python, variables and data types are both instances (objects) of the same classes. There are a lot of data types in Python, like int, float, complex, and boolean.
DataType | Description |
---|---|
int64 | Signed 64-bit integer types |
float32 | Standard double-precision floating-point |
complex | Complex numbers represented by 128 floats |
bool | Boolean type, storing TRUE and FALSE values |
Array Mathematics
We can perform standard mathematical expressions and operations using NumPy in Python, like addition, subtraction, squares, exponentials, sine-cosines, and a lot more.
-
Arithmetic Operators
We can use NumPy to perform arithmetic operations between one or more than one variables.
-
Comparision Operators
We can also use NumPy to compare two data types; element-wise comparison and array-wise comparison.
Output
Copying NumPy Arrays
Working with arrays has been made easy using NumPy. Amongst a plethora of operations, we can even copy an array using a function called copy().
Sorting NumPy Arrays
To sort an array is to put its elements in a particular order. These orders can range from increasing to alphabetical. Different algorithms have been developed to sort the array in various ways. In some circumstances, some of these kinds are more helpful than others.
To sort an array using NumPy, we use the sort() function.
Output
Subsetting, Slicing, and Indexing
Subsetting, slicing, and indexing in Python are used to retrieve an element/range of elements from multidimensional lists.
Subsetting refers to the method in which we specify the index of the element in a square bracket notation ([]).
Output
Slicing in NumPy can be referred to as the method in which we use the slice notation (:) to retrieve an element/element from a list. Think of it as slicing a pizza. If we want to take a slice of pizza, we would need to take a pizza roller and slice the part of the pizza that we need. Slicing works the same way!
Output
Indexing is the process of using advanced parameters to retrieve an element/element from a multidimensional list. There are two types of indexing, boolean and fancy
Boolean Indexing is the type of indexing in which we use a logical statement to retrieve an element/element from a multidimensional list. The logical statement inside the condition must return True or False.
Output
Fancy Indexing is quite similar to Boolean Indexing. The only difference is that in fancy indexing, we can specify the indices of the elements that we want to retrieve.
Output
Array Manipulation
The main purpose of NumPy is to provide functions that make it easier for developers to work with arrays. Here are some of the functions that help us manipulate arrays in a better way:
-
Addition and Deletion of elements from an array
To add/delete an element from an array, we use the append() and delete() functions, respectively. The append function adds an element at the end of a list, whereas the delete() function deletes a specific element.
Output
-
Inserting an Element at a particular index
With the help of insert() in NumPy, we can insert an element at any specific position in an array.
Output
-
Changing the size of the array
To change the shape of an array, we can use the resize() and reshape() functions. The ravel() function allows us to flatten a multidimensional array into a one-dimensional list.
Output
-
Transpose of an array
A matrix created by switching the number of rows and columns amongst themselves is called the transpose of a matrix. This concept is highly beneficial when it comes to calculating advanced computations like the inverse of a matrix.
We can find the transpose of any two-dimensional matrix by using the function transpose() provided by NumPy.
Output
-
Combining arrays
In NumPy, we can combine two or more two arrays in three ways; hstack(), vstack() and concatenate(). hstack() allows us to combine two arrays in a horizontal sense, vstack() combines two arrays in a vertical sense, and concatenate() combines elements row or column-wise.
Output
-
Splitting an Array
Using NumPy, we can easily split an array into any number of parts using the split() function. split() function takes in two arguments; the array to be split and the number of partitions we want.
Output
I/O
The communication between an information processing system, such as a computer, and the outside world, which may include a human or another information processing system, is known as input and output, or I/O. The data that is received by the system are called inputs, whereas the data sent from it is called output.
With the help of NumPy, we can work with files from our computers. Basic tasks like saving a file locally, loading the file on the disk, etc. can be performed. We can also perform these operations on various file types like text files.
- Saving and Loading on Disk
NumPy offers us a lot of functions to work with files in Python. The most popular functions that are used to save files are save(), savez().
The save() function in NumPy converts a linear data structure to a binary file and saves it in the disk of a computer (.npy format). Whereas the savez() function is used to save a collection of arrays into a single file in the .npz format.
Several functions are available in NumPy for working with files in Python. The most popular functions that are used to load files from memory are load() and loadtxt().
The load() function in NumPy helps us to load .npy or .npz files from our disk to our Python code editor. If it's a .npy file, then a single array is loaded, and if it's a .npz file, multiple arrays are returned.
- Saving and Loading Text Files
Just as we can save and load files from our disk, we can save and load text files using NumPy.
The savetxt() function helps us to convert a NumPy array into a text file and save it in the memory.
The loadtxt() function in NumPy helps us load text files from the memory to the Python compiler. Please note that the text file must contain array values only.
Conclusion
- In this NumPy cheat sheet, we covered the basic aspects of NumPy; a general-purpose package that offers a multidimensional array object with outstanding speed as well as capabilities for interacting with these arrays.
- This NumPy cheat sheet is a combination of the basic operations that can be performed using NumPy in Python. Some of them include I/O, Manipulation of Arrays, and Sorting.