Trusted answers to developer questions

Vafa Batool

**One-hot encoding**Â is a very popular technique used in machine learning to convert categorical data, like red, blue, and green, into binary values of

NumPy arrays, like any other array, can be indexed based on the indices of the elements. A high-level representation of a NumPy array being converted into a one-hot encoded 2-D array is as follows:

One-hot encodingÂ creates a 2-D array whose number of rows is equal to the size of the original array and number of columns is equal to the max element in the 1-D array added to ^{st} index in row ^{th} index in row

Let's look at the step-by-step transformation of another simple example below:

Conversion of array of indices to one-hot encoded array

Now let's see the method in action in Python using NumPy. NumPy provides certain functions that make this process of transformation very efficient. Have a look at the code below and change the values to see how the conversion changes as a result.

import numpy as np #creating an array simple_array = np.array([0,2,1]) #creating a 2D array filled with 0's encoded_array = np.zeros((simple_array.size, simple_array.max()+1), dtype=int) #replacing 0 with a 1 at the index of the original array encoded_array[np.arange(simple_array.size),simple_array] = 1 print(encoded_array)

Code example showing conversion of an array into a one-hot encoded array

- Line 1: We import
`NumPy`

to use functions from this library. - Line 4: We declare a simple array of numbers.
- Line 7: We initialize a 2-D array of
$0$ 's using the`numpy.zeros`

function, which takes the shape**.**In the code above, both the rows and columns are equal to 3. The data type of the array is specified as`int`

. - Line 10: We use the
`numpy.arange`

function to create a range of integers using the size of the original array.

In the example above, `numpy.arange`

will return `[0 1 2]`

. This will be used to loop over the rows of the 2-D array. Each number in the original array is used as an index to add ^{th} index and so on.

RELATED TAGS

numpy

CONTRIBUTOR

Vafa Batool

RELATED COURSES

View all Courses

Keep Exploring

Related Courses