首页 > 解决方案 > 检查输入时出错:预期 input_18 有 3 个维度,但得到了形状为 (7, 210) 的数组

问题描述

我的原始数据集的形状为 (210,8),我试图将 7 个独立变量作为我的神经网络的输入,以查看它们属于哪个类/类别。类/类别是目标变量。

我已经分离了自变量并将它们作为数组存储在“df_test”中

df = pd.read_csv('https://raw.githubusercontent.com/siiddd/WheatSeeds/master/Wheat.csv')

features = ['Area', 'Perimeter', 'Compactness', 'Length of Kernel','Width of Kernel', 'Asymmetric Coeff.', 'Length of Kernel Groove']

dftoArray = df[features].to_numpy()
df_test = dftoArray.reshape(7,210)

model = keras.Sequential()

model.add(keras.Input(shape = (7, )))

model.add(keras.layers.Dense(500, activation = 'relu'))
model.add(keras.layers.Dense(1, activation = 'sigmoid'))

model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])

model.fit(df_test, df['Class'], epochs = 10, validation_split = 0.10)  

这给了我错误:

检查输入时出错:预期 input_18 有 3 个维度,但得到了形状为 (7, 210) 的数组

标签: pythontensorflowkerasneural-network

解决方案


I think you are making a mistake while reshaping the DataFrame. As you said, the data consists of 210 samples each having 8 features i.e the shape of the data must be ( 210 , 8 ). Now, after selecting the desired columns from the df you need to reshape your data to ( 210 , 7 ) and not ( 7 , 210 ). Make this change,

df_test = dftoArray.reshape( 210 , 1 )

Shapes ( 210 , 7 ) and ( 7 , 210 ) have a huge difference. The shape ( 7 , 210 ) refers to a dataset which consists of 7 samples with 210 features each. This is not the case.


推荐阅读