ValueError: expected dense_1_input to have shape (None, 4) but got (78,2)

ValueError: expected dense_1_input to have shape (None, 4) but got (78,2)

我根本不了解数组的形状或如何确定训练数据的时期和批量大小。我的数据有 6 列,第 0 列是自变量 - 一个字符串,第 1-4 列是深度神经网络输入,第 5 列是输入的二进制结果。我有 99 行数据。 我想了解如何消除此错误。

#Importing Datasets
dataset=pd.read_csv('TestDNN.csv')
x = dataset.iloc[:,[1,5]].values # lower bound independent variable to upper bound in a matrix (in this case up to not including column5)
y = dataset.iloc[:,5].values # dependent variable vector
#Splitting data into Training and Test Data
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.2, random_state=0)


#Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test=sc.transform(x_test)

# PART2 - Making ANN, deep neural network

#Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

#Initialising ANN
classifier = Sequential()

#Adding the input layer and first hidden layer
classifier.add(Dense(activation= 'relu', input_dim =4, units=2, 
kernel_initializer="uniform"))#rectifier activation function
#Adding second hidden layer
classifier.add(Dense(activation= 'relu', units=2, 
kernel_initializer="uniform")) #rectifier activation function
#Adding the Output Layer
classifier.add(Dense(activation= 'sigmoid', units=1, 
kernel_initializer="uniform"))

#Compiling ANN - stochastic gradient descent
classifier.compile(optimizer='adam', loss='binary_crossentropy',metrics=
['accuracy'])

#Fit ANN to training set

#PART 3 - Making predictions and evaluating the model

#Fitting classifier to the training set
classifier.fit(x_train, y_train, batch_size=32, epochs=5)#original batch is 
10 and epoch is 100

问题出在 x 定义上。这一行:

x = dataset.iloc[:,[1,5]].values 

... 告诉 pandas 仅取第 1 列和第 5 列 ,因此它的形状为 [78, 2]。您可能意味着在第 5 列之前采用 all 列:

x = dataset.iloc[:,:5].values