JeBo
JeBo

Reputation: 197

How to fix One-hot encoding error - IndexError?

Currently I'm working on a Deep learning model containing LSTM to train on joints for human movement(s), but during the one-hot encoding process I keep getting an error. I've checked several websites for instructions, but unable to solve the difference with my code/data:

import pandas as pd
import numpy as np

keypoints = pd.read_csv('keypoints.csv')

X = keypoints.iloc[:,1:76]
y = keypoints.iloc[:,76]

Which results in the followwing shapes:

All the keypoints of the joints are in x and y contains all the labels I want to train on, which are three different (textual) labels. The first column of the dataset can be ignored, cause it contained just frame numbers. Therefor I was advised to use one-hot enconding to use categorical_entropy later on:

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
le = LabelEncoder()
y = le.fit_transform(y)
ohe = OneHotEncoder(categorical_features = [0])
y = ohe.fit_transform(y).toarray()

But when applying this, I get the error on the last line:

> Traceback (most recent call last):
  File "LSTMPose.py", line 28, in <module>
    y = ohe.fit_transform(y).toarray()
  File "C:\Users\jebo\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\preprocessing\_encoders.py", line 624, in fit_transform
    self._handle_deprecations(X)
  File "C:\Users\jebo\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\preprocessing\_encoders.py", line 453, in _handle_deprecations
    n_features = X.shape[1]
IndexError: tuple index out of range

I assumed it has something to with my y index, but it is just 1 column... so what am I missing?

Upvotes: 1

Views: 521

Answers (1)

a_guest
a_guest

Reputation: 36249

You need to reshape your y-data to be 2D as well, similar to the x-data. The second dimension should have length 1, i.e. you can do:

y = ohe.fit_transform(y[:, None]).toarray()

Upvotes: 2

Related Questions