NumPy Cheat Sheet

NumPy Cheat Sheet

NumPy Cheat Sheet will guide you to work on NumPy with basics and advanced topics. Cheat Sheet for students, engineers, and professionals.


NumPy is a Python library for numeric and scientific computing. It stands for numerical python and consists of multi-dimensional array objects.


To install NumPy, use the PIP package manager. Install Python and PIP, and then use PIP to install the NumPy Python library:

Create NumPy Arrays

To create Numpy arrays, use numpy.arrays() method in Python:

  • Create a Numpy array by passing a tuple:
  • Create a Numpy array by passing a list:

Dimensions of NumPy Arrays

Dimensions of an array in NumPy are also called the Rank of an Array. Check how many dimensions an array has used with the numpy.ndarray.ndim:

  • Create a Zero-dimensional NumPy array:
  • Create a One-dimensional NumPy array:
  • Create a Two-dimensional NumPy array:

Initialize NumPy Arrays

To Initialize NumPy arrays, use the numpy.zeros() method. Let us see how to initialize numpy arrays with zeros:


Array Indexing

Array indexing is accessing array elements. In Numpy, access an array element using the index number. The 0th index is element 1 and the flow goes on as shown below:

  •   index 0 – element 1
  •   index 1 – element 2
  •   index 2 – element 3
  •   index 3 – element 4

Let us see some examples:

  • Access the 1st element (index 0) from a One-Dimensional array:
  • Accessing 1st dimension elements from a 2D array:
  • Access the last element from a 1D array with negative indexing:

Array Slicing

Slicing the array in a range from start to end is what we call Array Slicing. The following is the syntax to slice NumPy arrays. Here, start is the beginning index (include), and the end is the last index (excluded):

Let us see some examples:

  • Slicing from index 1 to 3 (3 is excluded):
  • Slicing from index 5 to last:

NumPy Datatypes

Python Numpy supports the following data types:

  • b – boolean
  • u – unsigned integer
  • f – float
  • c – complex float
  • m – timedelta
  • M – datetime
  • O – object
  • S – string
  • U – unicode string

Let us see some examples:

  • Get the datatype of a Numpy array with strings:
  • Convert string to integer:


NumPy Array Shape

In Python Numpy, the number of elements in each dimension of an array is called the shape. To get the shape of a Numpy array, use the numpy.shape attribute in Python:

  • Check the shape of a Zero-Dimensional NumPy array:
  • Check the shape of a One-Dimensional NumPy array:

Reshape a NumPy Array

To reshape the number of dimensions, use the numpy.reshape() method in Python. Reshape allows you to edit the number of elements in a dimension. Let us reshape dimensions from 1D to 2D:


Iterate Numpy Arrays

Iteration displays each element of an array. We will see how to iterate through Numpy arrays using loops. Let us iterate a One-Dimensional Numpy array:


Joining NumPy Arrays

We can join two or more arrays in a single new array:


Join arrays in Numpy using the stack methods, such as:

  • stack(): Join arrays
  • hstack(): Stack along rows
  • vstack(): Stack along columns
  • dstack(): Stack along depth
  • column_stack(): Stack according to columns

Split NumPy Array

To split an array, use the array_split() method. Following is the syntax:

Here, arr_name is the name of the array, split_num is the count of splits.

Let us see how to split a 1d array:


Search an array for a value

In a Numpy array, we can search for a specific value using the numpy.where() method. This method returns the index where the specific element is found. Let us see an example:


Sort Arrays

The numpy.sort() function is used in Numpy to sort arrays in a sequence. This sequence can be ascending or descending. Let us see how to sort arrays:


Axes in Arrays

In Numpy arrays, the axis is the direction along the rows and columns. A two-dimensional numpy array has the following two corresponding axes:

Let us see an example to get the minimum value with axes:


Intersection of NumPy Arrays

If you want to get the intersection of two arrays, use the intersect1d() method in Numpy. Intersection means finding common elements between two arrays.  Let us see an example:


Difference between NumPy Arrays

To find the difference between two arrays, use the numpy.setdiff1d() method. This means if we are subtracting two arrays, then setdiff1d() will subtract the common elements from the 1st array and display the rest of the elements of the 1st array. Let us see an example:


Arithmetic operations between NumPy Arrays

Perform arithmetic operations on Numpy arrays:

  • Add Numpy Arrays: Use the sum() method to add arrays:
  • Subtract Numpy Arrays: Use the subtract() method to subtract one array from another:
  • Multiply Numpy Arrays: Use the multiply() method to multiply elements of one array to another:
  • Divide Numpy Arrays: The divide() method divides elements of one array with elements of another:

Scalar Operations on NumPy Arrays

Scalar operations on Numpy arrays include performing:

  • Addition Operation on Numpy Arrays: The Addition Operation adds a value to each element of a NumPy array:
  • Subtraction Operation on Numpy Arrays: The Subtraction operation is subtracting a value from each element of a NumPy array:
  • Multiplication Operation on Numpy Arrays: The Multiplication operation is multiplying a value to each element of a NumPy array:
  • Division Operation on Numpy Arrays: The Division operation is to divide a value from each element of a NumPy array:

Statistical Operations on NumPy Arrays

We can perform statistical operations, like mean, median, and standard deviation on NumPy arrays:

  • Mean: Mean is the average of the given values. Here, we will find the mean of the array elements using the mean() method:
  • Median: Median is the middle value of the given values. Here, we will find the median of the array elements using the median() method:
  • Standard Deviation: Standard Deviation is a measure of the amount of variation or dispersion of the given values. Here, we will find the standard deviation of the array elements using the std() method:

What’s next?

After completing NumPy, follow the below tutorials and learn Python Libraries:

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now

Numpy LCM and HCF
NumPy Online Quiz
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading