FOR DEVELOPERS

How to Slice NumPy Arrays for Machine Learning Using Python

How to Slice NumPy Arrays for Machine Learning Using Python

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.

NumPy array slicing

Installation and import

Use the following command to install NumPy on your computer :

Numpy array slicing.webp

To import the library, use:

numpy installation and import.webp

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.

  • One-dimensional array

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.

Slicing NumPy Arrays.webp

  • Two-dimensional array

In this type of array, elements are stored in rows and columns which represent a matrix.

NumPy slice 2d array.webp

  • Three-dimensional array

This type of array comprises 2-D matrices as its elements.

python 2d array indexing.webp

Slicing 1-D arrays

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:

slicing arrays python.webp

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).

Indexing and Slicing NumPy Arrays.webp

Slicing 2-D arrays

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.

NumPy slicing and indexing.webp

The last index is excluded.

NumPy array indexing

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.

NumPy 1-D array indexing

You need to pass the index of that element as shown below, to access the 1-D array.

Slicing of a NumPy 2d array.webp

The output shows 10 is at index 0.

NumPy 2-D array indexing

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.

Working with Numpy Arrays.webp

Conclusion

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.

Author

  • How to Slice NumPy Arrays for Machine Learning Using Python

    Mary Kariuki

    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.

Frequently Asked Questions

It is a fundamental package in Python that’s used for working with arrays.

Slicing is extracting elements from an array while indexing is accessing the elements in an array.

It means Numerical Python.

You can slice multiple elements by specifying the index range, i.e., the starting value and the ending value. This can be done in one dimension by slicing any of the elements from that array. For instance, the second, third or even the fifth element

NumPy arrays are faster compared to Python lists since they are stored in contiguous memory locations unlike lists which are stored in non-contiguous memory locations.

To slice a column that has all its rows, we include a semicolon at the starting index which indicates that all rows in that column are selected. Then, we specify the ending index which represents the column we want to slice.

View more FAQs
Press

Press

What’s up with Turing? Get the latest news about us here.
Blog

Blog

Know more about remote work. Checkout our blog here.
Contact

Contact

Have any questions? We’d love to hear from you.

Hire remote developers

Tell us the skills you need and we'll find the best developer for you in days, not weeks.