如何修复 One-hot 编码错误 - IndexError?
How to fix One-hot encoding error - IndexError?
目前我正在研究一个包含 LSTM 的深度学习模型来训练人体运动的关节,但在单热编码过程中我不断出错。
我已经检查了几个网站的说明,但无法解决我 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]
这导致以下翼形:
- 关键点 = (63564, 77)
- x = (63564, 75)
- y = (63564,)
关节的所有关键点都在x和y中包含了我要训练的所有标签,这是三个不同的(文本)标签。数据集的第一列可以忽略,因为它只包含帧数。
因此,我被建议使用 one-hot enconding 稍后使用 categorical_entropy:
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 数据重塑为二维,类似于 x 数据。第二个维度的长度应为 1,即你可以这样做:
y = ohe.fit_transform(y[:, None]).toarray()
目前我正在研究一个包含 LSTM 的深度学习模型来训练人体运动的关节,但在单热编码过程中我不断出错。 我已经检查了几个网站的说明,但无法解决我 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]
这导致以下翼形:
- 关键点 = (63564, 77)
- x = (63564, 75)
- y = (63564,)
关节的所有关键点都在x和y中包含了我要训练的所有标签,这是三个不同的(文本)标签。数据集的第一列可以忽略,因为它只包含帧数。 因此,我被建议使用 one-hot enconding 稍后使用 categorical_entropy:
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 数据重塑为二维,类似于 x 数据。第二个维度的长度应为 1,即你可以这样做:
y = ohe.fit_transform(y[:, None]).toarray()