Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<compute_test_metric>
start = time.time()
%pylab inline
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id')
 data.shape
data.describe()
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape)
 X = preprocessing.MinMaxScaler().fit(data ).transform(data)
 X = StandardScaler().fit(data ).transform(data)
 print(X.shape)
 X
y_cat = to_categorical(y)
 print(y_cat.shape)
 sss = StratifiedShuffleSplit(n_splits=10, test_size=0.1,random_state=12345)
 train_index, val_index = next(iter(sss.split(X, y)))
 x_train, x_val = X[train_index], X[val_index]
y_train, y_val = y_cat[train_index], y_cat[val_index]
print("x_train dim: ",x_train.shape)
 print("x_val dim: ",x_val.shape)
 print()
model1 = Sequential()
model1.add(Dense(600,input_dim=192, init='uniform', activation='relu'))
model1.add(Dropout(0.3))
model1.add(Dense(600, activation='sigmoid'))
model1.add(Dropout(0.3))
model1.add(Dense(99, activation='softmax'))
model1.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"])
 early_stopping = EarlyStopping(monitor='val_loss', patience=300)
 history = model1.fit(x_train, y_train,batch_size=192,nb_epoch=2500 ,verbose=0,
validation_data=(x_val, y_val),callbacks=[early_stopping])
 print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))
print('train_acc: ',max(history.history['acc']))
print('train_loss: ',min(history.history['loss']))
print("train/val loss ratio: ", min(history.history['loss'])/min(history.history['val_loss']))
plt.semilogy(history.history['loss'])
 plt.semilogy(history.history['val_loss'])
 plt.title('model1 loss')
 plt.ylabel('loss')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
plt.plot(history.history['acc'])
 plt.plot(history.history['val_acc'])
 plt.title('model1 accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
model2 = Sequential()
model2.add(Dense(1024,input_dim=192, init='glorot_normal', activation='relu'))
model2.add(Dropout(0.2))
model2.add(Dense(512, activation='sigmoid'))
model2.add(Dropout(0.2))
model2.add(Dense(99, activation='softmax'))
model2.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"])
 early_stopping = EarlyStopping(monitor='val_loss', patience=300)
 history = model2.fit(x_train, y_train,batch_size=192,nb_epoch=2500 ,verbose=0,
validation_data=(x_val, y_val),callbacks=[early_stopping])
 print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))
print('train_acc: ',max(history.history['acc']))
print('train_loss: ',min(history.history['loss']))
print("train/val loss ratio: ", min(history.history['loss'])/min(history.history['val_loss']))
plt.semilogy(history.history['loss'])
 plt.semilogy(history.history['val_loss'])
 plt.title('model2 loss')
 plt.ylabel('loss')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
plt.plot(history.history['acc'])
 plt.plot(history.history['val_acc'])
 plt.title('model2 accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
model3 = Sequential()
model3.add(Dense(600,input_dim=192, init='uniform', activation='relu'))
model3.add(Dropout(0.3))
model3.add(Dense(600, activation='sigmoid'))
model3.add(Dropout(0.3))
model3.add(Dense(99, activation='softmax'))
model3.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"])
 early_stopping = EarlyStopping(monitor='val_loss', patience=300)
 history = model3.fit(x_train, y_train,batch_size=192,nb_epoch=2500 ,verbose=0,
validation_data=(x_val, y_val),callbacks=[early_stopping])
 print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))
print('train_acc: ',max(history.history['acc']))
print('train_loss: ',min(history.history['loss']))
print("train/val loss ratio: ", min(history.history['loss'])/min(history.history['val_loss']))
plt.semilogy(history.history['loss'])
 plt.semilogy(history.history['val_loss'])
 plt.title('model3 loss')
 plt.ylabel('loss')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
plt.plot(history.history['acc'])
 plt.plot(history.history['val_acc'])
 plt.title('model3 accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
model4 = Sequential()
model4.add(Dense(768,input_dim=192, init='uniform', activation='relu'))
model4.add(Dropout(0.3))
model3.add(Dense(384, activation='sigmoid'))
model3.add(Dropout(0.3))
model4.add(Dense(99, activation='softmax'))
model4.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"])
 early_stopping = EarlyStopping(monitor='val_loss', patience=600)
 history = model4.fit(x_train, y_train,batch_size=192,nb_epoch=2500 ,verbose=0,
validation_data=(x_val, y_val),callbacks=[early_stopping])
 print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))
print('train_acc: ',max(history.history['acc']))
print('train_loss: ',min(history.history['loss']))
print("train/val loss ratio: ", min(history.history['loss'])/min(history.history['val_loss']))
plt.semilogy(history.history['loss'])
 plt.semilogy(history.history['val_loss'])
 plt.title('model4 loss')
 plt.ylabel('loss')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
plt.plot(history.history['acc'])
 plt.plot(history.history['val_acc'])
 plt.title('model4 accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.legend(['train', 'test'], loc='upper left')
 plt.show()
test = pd.read_csv('.. /input/test.csv')
 index = test.pop('id')
 test = preprocessing.MinMaxScaler().fit(test ).transform(test)
 test = StandardScaler().fit(test ).transform(test)
 yPred1 = model1.predict_proba(test)
 yPred2 = model2.predict_proba(test)
 yPred3 = model3.predict_proba(test)
 yPred4 = model4.predict_proba(test)
 yPred =(yPred1 + yPred2 + yPred3 + yPred4)/ 4.0
yPred = pd.DataFrame(yPred,index=index,columns=sort(parent_data.species.unique()))
yPred
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv())
 end = time.time()
print()
print(round(( end-start),2), "seconds" )