python - 检查输入时出错:预期 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) 的数组
解决方案
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.
推荐阅读
- python - 按日期元素过滤元组列表并返回元组
- python - 饼图大小、标签、距离
- javascript - 我必须单击两次才能使用 Sortable.init() 初始化排序表
- python - 如何在多个文件上使用“导入”
- regex - 删除前缀和后缀并留下中间字符串
- stripe-payments - 条纹 webhook 混淆
- amazon-web-services - AWS Data Pipeline Dynamo 到 Redshift
- dart - 为什么“window.sessionStorage['foo'] ??='test'”在 Dart 中不起作用?
- mongodb - MongoDb - 授予用户访问特定数据库的权限
- javascript - 单击时如何更改按钮类?