Kaggle competition: Leaf Classification
<load_from_csv><EOS> 
class Data_Clean(object):
def __init__(self):
self.numerical_data, self.num_test_data = self.read_numerical_data()
self.id, self.species, self.num_train, self.test_id, self.test_num = self.split_numerical_data()
def split_numerical_data(self):
id = self.numerical_data.pop('id')
 species = self.numerical_data.pop('species')
 species = LabelEncoder().fit(species ).transform(species)
 species = to_categorical(species, num_classes=99)
 self.numerical_data = StandardScaler().fit(self.numerical_data ).transform(self.numerical_data)
 test_id = self.num_test_data.pop('id')
 num_test_data = StandardScaler().fit(self.num_test_data ).transform(self.num_test_data)
 return id, species, self.numerical_data, test_id, num_test_data
def read_numerical_data(self):
root = ".. /input"
data = pd.read_csv('%s/train.csv' %root)
 test_data = pd.read_csv('%s/test.csv' %(root))
return data, test_data
def run(self):
return self.species, self.num_train, self.test_num, self.test_id
class CNN(object):
def __init__(self, num_train, num_vali, species_train, species_vali, num_test, test_id):
self.num_train = num_train
self.num_vali = num_vali
self.species_train = species_train
self.species_vali = species_vali
self.num_test = num_test
self.test_id = test_id
def define_CNN(self):
model = Sequential()
model.add(Dense(512, activation='relu', input_dim=192))
model.add(Dropout(0.25))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(99, activation='softmax'))
self.model = model
def RMSprop(self, batch_size, epoch):
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-8, decay=0.0)
 self.model.compile(optimizer=optimizer, loss = 'categorical_crossentropy', metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
 self.model.fit(self.num_train, self.species_train, batch_size=batch_size, epochs=epoch,
validation_data=(self.num_vali, self.species_vali), verbose=2, callbacks=[learning_rate_reduction])
 def make_predict(self):
ypred_prob = self.model.predict(self.num_test)
 root = ".. /input"
labels = sorted(pd.read_csv(os.path.join(root, 'train.csv')).species.unique())
 ypred = pd.DataFrame(ypred_prob, index = self.test_id, columns=labels)
 print(ypred.head(2))
fp = open('submit.csv', 'w')
 fp.write(ypred.to_csv())
 def run(self):
self.define_CNN()
self.RMSprop(batch_size = 128, epoch=100)
 self.make_predict()
if __name__ == '__main__':
data_clean = Data_Clean()
species, num, num_test, test_id = data_clean.run()
species_train, species_vali, num_train, num_vali = train_test_split(species, num, test_size=0.1, random_state=26)
 cnn = CNN(num_train, num_vali,species_train, species_vali, num_test, test_id)
 cnn.run()