标签编码:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效索引

问题描述 投票:0回答:1

我正在尝试运行神经网络。我有一些与此错误相关的问题,因为我正在尝试将分类输入转换为数字普通输入,所以我使用了以下代码:

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, KFold
from sklearn.preprocessing import StandardScaler, LabelEncoder
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from sklearn.metrics import r2_score

# Load your dataset
data = pd.read_excel('Teste JMP.xlsx')

# Separate the input features from the target outputs
X = data.drop(['Brookfield', 'Marsh'], axis=1).values
y = data[['Brookfield', 'Marsh']].values

# Apply label encoding to categorical columns
label_encoder = LabelEncoder()
X['Test'] = label_encoder.fit_transform(X['Test'])

因此,我得到了标题中的错误。有人可以帮助我吗?

我虽然问题是索引类型或数据框/numpy 形式,但是当我将“Marsh”切换为数字时,错误是这样的:

TypeError:编码器要求其输入统一为字符串或数字。得到 ['float', 'int', 'str']

python neural-network label-encoding
1个回答
0
投票

您可以尝试这个新片段,它应该可以工作。如果没有,请告诉我😆!

import pandas as pd
from sklearn.model_selection import train_test_split, KFold
from sklearn.preprocessing import StandardScaler, LabelEncoder
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from sklearn.metrics import r2_score

# Load your dataset
data = pd.read_excel('Teste JMP.xlsx')

# Separate the input features from the target outputs
X = data.drop(['Brookfield', 'Marsh'], axis=1).values
y = data[['Brookfield', 'Marsh']].values

# Convert 'Test' column to string type
X['Test'] = X['Test'].astype(str)

# Apply label encoding to categorical columns
label_encoder = LabelEncoder()
X['Test'] = label_encoder.fit_transform(X['Test'])```
© www.soinside.com 2019 - 2024. All rights reserved.