首页 > 解决方案 > how to fix "index 3 is out of bounds for axis 1 with size 3" in one-hot encoding?

问题描述

I was working on one-hot encoding using python. but there is some problem when i run one-hot-encoding

def one_hot_encode(labels):
    n_labels = len(labels)
    n_unique_labels = len(np.unique(labels))
    one_hot_encode = np.zeros((n_labels,n_unique_labels))
    one_hot_encode[np.arange(n_labels), labels] = 1
    return one_hot_encode

this is what i used to running one-hot endcode

and the data is like this...

[3 3 3 3 3 2 2 2 2 2 1 1 1 1 1]

It occurs this error

"index 3 is out of bounds for axis 1 with size 3"

And i try another path...

change the part of code

one_hot_encode = np.zeros((n_labels,n_unique_labels+1))

This is running but it its not the 3 classes... The result is like this

array([[0., 0., 0., 1.],
   [0., 0., 0., 1.],
   [0., 0., 0., 1.],
   [0., 0., 0., 1.],
   [0., 0., 0., 1.],
   [0., 0., 1., 0.],
   [0., 0., 1., 0.],
   [0., 0., 1., 0.],
   [0., 0., 1., 0.],
   [0., 0., 1., 0.],
   [0., 1., 0., 0.],
   [0., 1., 0., 0.],
   [0., 1., 0., 0.],
   [0., 1., 0., 0.],
   [0., 1., 0., 0.]])

how do I fix this problem?

标签: pythonnumpy

解决方案


The error is raising from [3 3 3 3 3 2 2 2 2 2 1 1 1 1 1]. You have 3 in your mapping np.array which means in some position you are trying to equal index 3 to 1 but the problem is that maximum index in your mapping array is 2.

def one_hot_encode(labels):
    n_labels = len(labels) # this will give 15
    n_unique_labels = len(np.unique(labels)) # this will give 3
    one_hot_encode = np.zeros((n_labels,n_unique_labels)) # will create 15x3 matrix
    one_hot_encode[np.arange(n_labels), labels] = 1 # error here you try to map index 3 to 1 which does not exist
    return one_hot_encode

Just simply change your mapping array from [3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1] to [2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]


推荐阅读