top of page

Index, Slice and Reshape NumPy Arrays for Machine Learning

Sometimes, when we working on Machine learning datasets then we need to slice, index or reshape datasets as per given requirements. It is an important part of prepare data in datasets, in this blog we will learn all types of slicing in machine learning so that we can easily handle datasets for ML or data science tasks.

How slicing datasets?

Machine learning data is present as an array format, which means it is in 1 D array, 2 D array, etc. In python, it represents using NumPy arrays.

In this tutorial, we will be working with the Numpy array to extract data from datasets.

Reading Data

Data is read from different ways like list data, CSV format or excel.

But in this, we will work with the list and CSV file.

List data: 1D array

# one dimensional example

>>> from numpy import array

# list of data

>>> data = [1, 2, 3, 4, 5]

# array of data

>>> data = array(data) >>> print(data)

List data: 2D array

>>> from numpy import array

# list of data

>>> data = [[1, 2],

[3, 4],

[5, 6]]

# array of data

>>> data = array(data)

>>> print(data)

>>> print(type(data))

Array Index

Once your data is represented using a NumPy array, you can access it using indexing.

1-Dimensional Indexing

Index data can be access using given below print statements for 1-D Numpy array

# index data

>>> print(data[0])

>>> print(data[4])

2-Dimensional Indexing

# index data

>>> print(data[0,0])

Array Slicing

If you not know about ML and start to read it then first it is a very tuff task for beginners that how to slicing datasets as per your requirements.

To do this use colon operator ‘:’ with afromand to index before and after the column respectively.


>>> data[from:to]

>>> print(data[:])

1-Dimensional Slicing

>>> print(data[0:1])

>>> print(data[-2:])

2-Dimensional Slicing

In these two colon-separated by comma and the first one for row and second one column.


>>> data[:, :-1]


>>> data[:, -1]

You can take own data and try these itself and get output if you need other help then comments below the comments section so we can reply to your comments and make something is missing then we modifying blog.

We will always provide the best services for any projects if you need help with any projects then please contact us here.

21 views0 comments

Recent Posts

See All


bottom of page