我的结构化数据看起来像这样。
faults.head()
Fault DEALER FAILMODE FAILCODEMODE DAYS UNTIL FAILURE TERRITORY CODE DESIGN PHASE CODE PLANT ID CODE
0 CAMPAIGN/TRP 31057 CAMPAIGN BNRBC1 283.0 102 62 82
1 INTERMITTENT PROBL 24126 SPECIAL (NO FAILURE) XXIPNF 126.0 102 62 82
2 DSID #DSBCG2058 TAG #362783 EXHAUST SYSTEM. U... 0 CLOGGED, PLUGGED WITH FOREIGN MATERIAL, DIRT/D... USDVDR 118.0 102 62 82
3 INTERMITTENT PROBL 20943 SPECIAL (NO FAILURE) XXIPNF 97.0 102 62 82
4 CAMPAIGN 19134 CAMPAIGN USSCR1 315.0 102 62 82
我试图预测课程FAILMODE。 FAILMODE中只有122个唯一值。那些是我的课程。
基于行中的所有其他数据,我希望有一个单热矩阵,甚至类本身也是我的测试集上计算的产物。这是我目前的代码 -
from keras.models import Sequential
from keras.layers import Dense
Using Theano backend.
faults_testing = faults[:14843]
faults_training = faults[14844:]
model = Sequential()
model.add(Dense(len(faults.FAILMODE.unique()) + 20, input_dim=len(faults_training), init='uniform', activation='relu'))
model.add(Dense(len(faults_training), init='uniform', activation='relu'))
model.add(Dense(len(faults.FAILMODE.unique()), init='uniform', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
这是教程所说的地方 -
model.fit(X, Y, nb_epoch=len(faults_training), batch_size=10)
我不知道X或Y是什么,所以我只是尝试了以下内容 -
model.fit(faults_training['FAILMODE'], faults_testing['FAILMODE'], nb_epoch=len(faults_training), batch_size=10)
导致此错误 -
ValueError Traceback (most recent call last)
<ipython-input-54-e8765933cfb9> in <module>()
----> 1 model.fit(faults_training['FAILMODE'], faults_testing['FAILMODE'], nb_epoch=len(faults_training), batch_size=10)
ValueError: Error when checking model input: expected dense_input_1 to have shape (None, 34631) but got array with shape (34631L, 1L)
请彻底解答。谢谢!
答案 0 :(得分:0)
常规神经网络(包括Keras顺序模型)仅接受数据(X)的浮点数和标签/类(Y)的int或单热编码。因此,您需要转换数据集以符合要求。那你可能想做什么:
Y = to_categorical(Y)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33)
model.fit(X_train, Y_train, nb_epoch=100, batch_size=10)
训练模型。根据您对培训速度和准确性的预期,稍后调整nb_epoch
和batch_size
scores = model.evaluate(self, X_test, Y_test, batch_size=10)
您还可以查看此文章,了解如何将分类数据转换为数字http://fastml.com/converting-categorical-data-into-numbers-with-pandas-and-scikit-learn/