How to convert an array of indices to one-hot encoded NumPy array
Last Updated :
28 Apr, 2025
A very popular technique used in machine learning to transform categorical data into binary values of 0 and 1 is called the one-hot encoding technique. There are various circumstances when you need to use a one-hot encoded NumPy array rather than an array of indices, thus we can convert it using the arrange or LabelBinarizer function. In this article, we will discuss all the ways through which we can do the same.
Converting an array of indices to one-hot encoded NumPy array
- Using arange function
- Using LabelBinarizer function
- Using eye() function
Using the arange function
The function that is used to generate an array with evenly spaced values within a specified interval is called arange function(). In this way, we will see how we can convert an array of indices to a one-hot encoded NumPy array using arange function.
Example: The array of indices which is to be converted to one-hot encoded NumPy array is as follows:

This code performs one-hot encoding on a NumPy array called 'arr'. It creates a new array, 'encoded_arr', with the necessary dimensions for one-hot encoding. Then, it sets specific positions in 'encoded_arr' to 1 based on the values in 'arr'. This results in a one-hot encoded representation of the original array, where each unique value in 'arr' corresponds to a unique column in 'encoded_arr', and a 1 is placed in the column corresponding to the value in 'arr'.
Python3
import numpy as np
arr = np.array([4,7,2,9])
encoded_arr = np.zeros((arr.size, arr.max()+1), dtype=int)
encoded_arr[np.arange(arr.size),arr] = 1
print(encoded_arr)
Output:

Using LabelBinarizer function
The function which binarize labels in a one-vs-all fashion is known as LabelBinarizer function() in sklearn. In this way, we will see how we can convert an array of indices to one-hot encoded NumPy array using LabelBinarizer function.
Syntax: sklearn.preprocessing.LabelBinarizer(*, neg_label=0, pos_label=1, sparse_output=False)
Here,
- neg_label: It defines the value with which negative labels must be encoded.
- pos_label: It defines the value with which positive labels must be encoded.
- sparse_output: It defines a boolean value which defines whether array is in sparse CSR format.
Example: The array of indices which is to be converted to one-hot encoded NumPy array is as follows:

This code demonstrates one-hot encoding using the LabelBinarizer from scikit-learn. It first initializes a LabelBinarizer and fits it to a range of values from 0 to the maximum value in the input array 'arr'. Then, it transforms 'arr' into a one-hot encoded array called 'encoded_arr'. Each unique value in 'arr' corresponds to a unique column in 'encoded_arr', and a 1 is placed in the column corresponding to the value in 'arr'. The resulting 'encoded_arr' represents the one-hot encoded version of the original 'arr'.
Python3
import numpy as np
import sklearn.preprocessing
arr = np.array([4,7,2,9])
label_binarizer = sklearn.preprocessing.LabelBinarizer()
label_binarizer.fit(range(max(arr)+1))
encoded_arr = label_binarizer.transform(arr)
print('{0}'.format(encoded_arr))
Output:

Using eye() function
The array of indices which is to be converted to one-hot encoded NumPy array is as follows:
[1 3 2]
In this code we use the eye() function to generate the one hot encoding array for the input array. Here '1' is present at that particular position and at other positions '0' is present.
Python
import numpy as np
list = np.array([1, 3, 2])
print(list)
print(np.eye(4)[list])
Output:
[[0. 1. 0. 0.]
[0. 0. 0. 1.]
[0. 0. 1. 0.]]
Similar Reads
How to convert 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array.
2 min read
How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read
How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi
3 min read
How to convert a list and tuple into NumPy arrays? In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read
How to Convert a Dictionary into a NumPy Array In this article, we will learn how to convert a Python Dictionary into a numpy array which is more efficient for numerical operations and provides powerful tools for matrix and array manipulationsKey Steps to Convert a Dictionary to a NumPy ArrayUse dict.items(): This returns key-value pairs from th
3 min read
How to Convert images to NumPy array? Pictures on a computer are made of tiny dots called pixels. To work with them in Python, we convert them into numbers using a NumPy array is a table of numbers showing each pixelâs color. In this article, weâll learn how to do this using popular Python tools.Loading the images via Pillow LibraryLet
5 min read