似乎有很多similar questions,但我无法解决我的问题。
有一个数据集,它具有15个特征(列)和一个我要预测的相关二进制特征。
我做了所有准备工作:
features = df.iloc[:,:-1]
result = df.iloc[:,-1]
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features, result, 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)
检查X_train和y_train的大小:
X_train.shape
返回(3392,15) 还有
Y_train.shape
返回(3392,)
Y_train是一个数组:array([0,1,0,...,0,0,0])
然后我建立一个网络:
from keras.models import Sequential
from keras.layers import Dense
classifier = Sequential()
classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu', input_dim= X_train[0].shape)) # 8 units because (15 features + 1 to forecast) / 2
# Adding the second hidden layer
classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)
并收到一条错误消息: ValueError:检查输入时出错:预期density_1_input的形状为(11,),但数组的形状为(15,)
我不明白为什么它期望形状为11(此数字在我的代码中没有使用)。在此处发布的类似问题中,问题通常来自错误地指定训练集的大小(例如零而不是要素数量之多)。但我显然是通过编写将正确的数字传递给第一层
input_dim= X_train[0].shape
我也可以写为
input_dim= 15
具有相同的结果。
我在做什么错了?
P.S。我还认为问题出在y_train上,并且这样做:
y_train = y_train.reshape(3392,1)
但没有效果。
答案 0 :(得分:0)
更改此
X_train[0].shape
对此
X_train.shape[1]
获取矩阵中的列数。