Kaggle competition: Leaf Classification
sample_submission = pd.read_csv('{}/sample_submission.csv'.format(BASE_DIR))
sample_submission.head()<define_variables>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical
from keras.callbacks import EarlyStopping
test_ids = os.listdir('{}/test/test'.format(BASE_DIR))<predict_on_test>
data = pd.read_csv('.. /input/leaf-classification/train.csv')
 parent_data = data.copy()
ID = data.pop('id')
 y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape)
 X = StandardScaler().fit(data ).transform(data)
 print(X.shape)
 y_cat = to_categorical(y)
 print(y_cat.shape )
def test_gen() :
for test_id in test_ids:
yield read_image(test_id, base_dir='{}/test/test'.format(BASE_DIR), transformation=True)
 test_ds = tf.data.Dataset.from_generator(test_gen, output_types=(tf.float32)).batch(BATCH_SIZE)
 multiple_test_predictions = [
model.predict_generator(test_ds)
 for i in range(10)
 ]
test_predictions = np.mean(np.array(multiple_test_predictions), axis=0 )<prepare_output>
model = Sequential()
model.add(Dense(1500,input_dim=192, kernel_initializer = 'uniform', activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1500, activation='sigmoid'))
model.add(Dropout(0.1))
model.add(Dense(99, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics = ["accuracy"])
 early_stopping = EarlyStopping(monitor='val_loss', patience=280)
 history = model.fit(X,y_cat,batch_size=192,
epochs=800 ,verbose=0, validation_split=0.1, callbacks=[early_stopping])
submission = pd.DataFrame({'id': test_ids, 'has_cactus': test_predictions.flatten() })
 submission.head()<save_to_csv>
print("train/val loss ratio: ", min(history.history['loss'])/min(history.history['val_loss']))
submission.to_csv('submission.csv', index = False, header = True )<import_modules>
test = pd.read_csv('.. /input/leaf-classification/test.csv')
 index = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)
 yPred = model.predict_proba(test)
 yPred = pd.DataFrame(yPred,index=index,columns=sorted(parent_data.species.unique()))
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv() )