【问题标题】:How to fix One-hot encoding error - IndexError?如何修复 One-hot 编码错误 - IndexError?
【发布时间】:2019-12-09 02:03:40
【问题描述】:

目前,我正在开发一个包含 LSTM 的深度学习模型,以训练人体运动的关节,但在 one-hot 编码过程中,我不断收到错误消息。 我检查了几个网站的说明,但无法用我的代码/数据解决差异:

import pandas as pd
import numpy as np

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

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

这会产生以下形状:

  • 关键点 = (63564, 77)
  • x = (63564, 75)
  • y = (63564,)

关节的所有关键点都在 x 中,y 包含我想要训练的所有标签,它们是三个不同的(文本)标签。可以忽略数据集的第一列,因为它只包含帧号。 因此,我被建议使用 one-hot enconding 稍后使用 categorical_entrop:

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

但是在应用这个时,我在最后一行得到错误:

> 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

我认为它与我的 y 索引有关,但它只是 1 列...那我错过了什么?

【问题讨论】:

  • 你试过了吗:y = keypoints.iloc[:,-1]。作为忠告,请记住 Python 从 0 开始,所以 76 将是 75。
  • 感谢您的回复 --> 我尝试了y = keypoints.iloc[:,-1],但它对我来说保持了相同的 y 形状和输出,并且对于 one-hot 编码器也有相同的错误。您对 Python 从 0 开始的建议,但数字 0 是我的数据集中的“帧”,所以我想排除该列进行训练。

标签: python scikit-learn one-hot-encoding


【解决方案1】:

您还需要将 y 数据重塑为 2D,类似于 x 数据。第二个维度的长度应为 1,即您可以这样做:

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

【讨论】:

    猜你喜欢
    • 2018-01-29
    • 2017-10-24
    • 2020-07-18
    • 1970-01-01
    • 2021-08-13
    • 2017-06-21
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多