使用Keras和Jupyter重塑数据

时间:2018-08-13 19:33:40

标签: python tensorflow keras

我正在尝试使用Keras(tensorflow后端)和Jupyter笔记本为二进制分类训练基线ANN模型。 代码如下:

array=df6.values
X= array[:,0:384]
Y = array[:,385]

from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

seed = 7
np.random.seed(seed)
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)

def create_baseline():
    model = Sequential()
    model.add(Dense(60, input_dim=60, kernel_initializer='normal', activation='relu'))
    model.add(Dense(10, kernel_initializer='normal', activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

estimator = KerasClassifier(build_fn=create_baseline, epochs=100, batch_size=5, verbose=0)
kfold = StratifiedKFold(n_splits=2, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, encoded_Y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))

最后是以下错误:

ValueError: Error when checking input: expected dense_5_input to have shape (None, 60) but got array with shape (8, 384)

我的数据集也有18行和385列 我想知道如何正确地重塑以正确估计结果。非常感谢!

1 个答案:

答案 0 :(得分:1)

input_dim = 384

此参数表示您输入的形状,即X