Leverage Turing Intelligence capabilities to integrate AI into your operations, enhance automation, and optimize cloud migration for scalable impact.
Advance foundation model research and improve LLM reasoning, coding, and multimodal capabilities with Turing AGI Advancement.
Access a global network of elite AI professionals through Turing Jobs—vetted experts ready to accelerate your AI initiatives.
NumPy, which stands for Numerical Python, is a library that is partially written in Python. It is used for working with arrays, which are fundamental data structures that can hold more than one value at a time. In Python, lists can be used when working with arrays but there are limitations, such as slow speed. NumPy arrays help solve the problem. They are faster as they are stored in a continuous place in a memory. In this article, we’ll explore NumPy array slicing, which involves taking some elements from one given index to another given index.
Use the following command to install NumPy on your computer :
To import the library, use:
Note: np in the command is used as an alias.
Types of arrays
There are three types of arrays that are commonly used: one-dimensional arrays, two-dimensional arrays, and three-dimensional arrays. Let’s examine them briefly.
This is a type of array where elements are stored linearly, i.e., elements are just one row of values and can be accesssed by specifying the index of the element.
In this type of array, elements are stored in rows and columns which represent a matrix.
This type of array comprises 2-D matrices as its elements.
When slicing an array, you pass the starting index and the ending index which are separated by a full colon. You use the square brackets as shown below.
arr[start:end]
arr is the variable name of the array.
Here’s an example for better understanding:
The starting index is 1 and the ending index is 5. Therefore, you will slice from the second element since indexing in the array starts from 0 up to the fourth element.
Note: The result above includes the start index and excludes the end index.
Negative slicing
The minus operator is used to refer to an index from the end of an array; you slice an array from the end instead of from the start.
Example: Slice from index 4 (from the end) to index 2 (from the end).
To slice a 2-D array in NumPy, you have to specify row index and column index which are separated using a comma as shown below.
arr[1, 1:4]
The part before the comma represents the row while the part after the comma represents the column.
Example: Slice elements from index 1 to 4 from the second row.
The last index is excluded.
Array indexing refers to accessing an array element using an index number that starts from 0. The difference between indexing and slicing is that with the former, you simply access the element. With slicing, you extract specific elements from the array.
You need to pass the index of that element as shown below, to access the 1-D array.
The output shows 10 is at index 0.
To access the 2-D array, you need to use commas to separate the integers which represent the dimension and the index of the element. The first integer represents the row and the other represents the column.
NumPy is an important library that’s widely used in Python to work with arrays. As seen, to use NumPy, you need to install the library and then import it. It can be used in many ways, such as to slice and index arrays. It can slice either 1-D or 2-D arrays by extracting elements from the original array. It can also access the elements in an array, either from the start or from the end of the array.
Mary Kariuki is an upcoming machine learning specialist who has a great passion in machine learning. She is a technical writer in various public forums where she has written several blogs related to machine learning using python.