0
import pandas as pd
data=pd.read_csv("tesdata.csv")
data
x=data.iloc[:,0:2].values
y=data.iloc[:,2].values
from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
x=sc.fit_transform(x)
y=sc.fit_transform(y.reshape(1,-1))
from keras.models import Sequential
from keras.layers import Dense

model=Sequential()
model.add(Dense(2, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',optimizer='Adam',metrics=['accuracy'])
model.fit(x,y.reshape(1,-1),epochs=30,batch_size=21)

ValueError:数据基数不明确:

x 尺寸:21 y 尺寸:1

请提供具有相同第一个维度的数据。

我的数据

4

1 回答 1

1

我已经使用示例csv文件之一复制了相同的问题。

我发现你不需要standardizereshape这里的标签。检查以下代码片段:

x=data.iloc[:,0:7].values
y=data.iloc[:,7].values

from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
x=sc.fit_transform(x)

#y=sc.fit_transform(y.reshape(1,-1))  # Need not to standardize the label

from keras.models import Sequential
from keras.layers import Dense

model=Sequential()
model.add(Dense(2, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error',optimizer='Adam',metrics=['accuracy'])
model.fit(x,y,epochs=10,batch_size=21)   #put y instead of y.reshape(1,-1)

输出:

Epoch 1/10
159/159 [==============================] - 4s 8ms/step - loss: 97.8257 - accuracy: 3.0120e-04
Epoch 2/10
159/159 [==============================] - 1s 9ms/step - loss: 94.2889 - accuracy: 3.0120e-04
Epoch 3/10
159/159 [==============================] - 1s 8ms/step - loss: 90.9619 - accuracy: 3.0120e-04
Epoch 4/10
159/159 [==============================] - 1s 8ms/step - loss: 89.7787 - accuracy: 3.0120e-04
Epoch 5/10
159/159 [==============================] - 1s 8ms/step - loss: 89.5762 - accuracy: 3.0120e-04
Epoch 6/10
159/159 [==============================] - 1s 7ms/step - loss: 89.5045 - accuracy: 3.0120e-04
Epoch 7/10
159/159 [==============================] - 1s 8ms/step - loss: 89.4718 - accuracy: 3.0120e-04
Epoch 8/10
159/159 [==============================] - 1s 7ms/step - loss: 89.4559 - accuracy: 3.0120e-04
Epoch 9/10
159/159 [==============================] - 1s 7ms/step - loss: 89.4472 - accuracy: 3.0120e-04
Epoch 10/10
159/159 [==============================] - 1s 9ms/step - loss: 89.4420 - accuracy: 3.0120e-04
<keras.callbacks.History at 0x7f59f0b1c7d0>
于 2021-11-26T19:58:24.050 回答