Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<feature_engineering>
import numpy as np
import pandas as pd
import tensorflow as tf
import math
def isrc_to_year(isrc):
if type(isrc)== str:
if int(isrc[5:7])> 17:
return int(isrc[5:7])//5
else:
return int(isrc[5:7])//5
else:
return np.nan
songs_extra['song_year'] = songs_extra['isrc'].apply(isrc_to_year)
 songs_extra.drop(['isrc', 'name'], axis = 1, inplace = True )<merge>
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_integer('num_classes', 99, 'Number of classes.')
 flags.DEFINE_integer('num_variables', 192, 'Number of variables.')
 flags.DEFINE_integer('hidden1', 2048, 'Number of units in hidden layer 1.')
 flags.DEFINE_integer('hidden2', 1024, 'Number of units in hidden layer 2.')
 flags.DEFINE_integer('num_epochs', 200, 'Number of learning epochs.')
 flags.DEFINE_integer('batch_size', 90, 'Batch size.')
 flags.DEFINE_float('keep_prob', 0.5, 'Keep probability for drop out.')
 flags.DEFINE_float('learning_rate', 0.001, 'Initial learning rate.' )
train = train.merge(songs_extra, on = 'song_id', how = 'left')
 test = test.merge(songs_extra, on = 'song_id', how = 'left' )<feature_engineering>
def inference(data, data_size, keep_prob):
with tf.name_scope('hidden1'):
weights = tf.Variable(tf.truncated_normal([data_size, FLAGS.hidden1],
stddev=1.0 / math.sqrt(float(data_size))), name='weights1')
 biases = tf.Variable(tf.zeros([FLAGS.hidden1]), name='biases1')
 hidden1 = tf.nn.relu(tf.matmul(data, weights)+ biases)
 hidden1_drop = tf.nn.dropout(hidden1, keep_prob, name='layer1_dropout')
 with tf.name_scope('hidden2'):
weights = tf.Variable(tf.truncated_normal([FLAGS.hidden1, FLAGS.hidden2],
stddev=1.0 / math.sqrt(float(FLAGS.hidden1))), name='weights2')
 biases = tf.Variable(tf.zeros([FLAGS.hidden2]), name='biases2')
 hidden2 = tf.nn.sigmoid(tf.matmul(hidden1_drop, weights)+ biases)
 hidden2_drop = tf.nn.dropout(hidden2, keep_prob, name='layer2_dropout')
 with tf.name_scope('softmax_linear'):
weights = tf.Variable(tf.truncated_normal([FLAGS.hidden2, FLAGS.num_classes],
stddev=1.0 / math.sqrt(float(FLAGS.hidden2))), name='weights')
 biases = tf.Variable(tf.zeros([FLAGS.num_classes]), name='biases')
 logits = tf.matmul(hidden2_drop, weights)+ biases
return logits
train['genre_ids'] = train['genre_ids'].str.split('|' ).str[0]<define_variables>
def loss(logits, labels):
labels = tf.to_int64(labels)
 cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='xentropy')
 loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
 return loss
temp_song_length = train['song_length']<drop_column>
def training(loss):
tf.summary.scalar(loss.op.name, loss)
 optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
 global_step = tf.Variable(0, name='global_step', trainable=False)
 train_op = optimizer.minimize(loss, global_step=global_step)
 return train_op
train.drop('song_length', axis = 1, inplace = True)
 test.drop('song_length',axis = 1 , inplace =True )<groupby>
def evaluation(logits, labels):
correct = tf.nn.in_top_k(logits, labels, 1)
 return tf.reduce_sum(tf.cast(correct, tf.int32))
song_count = train.loc[:,["song_id","target"]]
song_count1 = song_count.groupby(["song_id"],as_index=False ).sum().rename(columns={"target":"repeat_count"})
 song_count2 = song_count.groupby(["song_id"],as_index=False ).count().rename(columns = {"target":"play_count"} )<merge>
def preprocess_data(data):
_data = data.copy()
del _data['id']
for column in _data:
if _data[column].dtypes == float:
_data[column] = z_score_normalization(_data[column])
 if 'species' in _data.columns:
_data.insert(2, 'species_cat', _data['species'].astype('category' ).cat.codes)
 _data.drop('species', axis=1, inplace=True)
 _data = _data.astype(float)
 return _data, _data.columns.size-1
song_repeat = song_count1.merge(song_count2,how="inner",on="song_id")
 song_repeat["repeat_percentage"] = round(( song_repeat['repeat_count']*100)/ song_repeat['play_count'],1)
 song_repeat['repeat_count'] = song_repeat['repeat_count'].astype('int')
 song_repeat['repeat_percentage'] = song_repeat['repeat_percentage'].replace(100.0,np.nan)
<merge>
def load_data_and_labels(file, is_labels_exist=True):
df = pd.read_csv(file)
 processed_df, variables_size = preprocess_data(df)
 print('Reading %s' % file)
 print('N=%d' % len(df))
if is_labels_exist is True:
labels = list(processed_df['species_cat'])
 else:
labels = None
if is_labels_exist is True:
del processed_df['species_cat']
new_rows = []
for index, row in processed_df.iterrows() :
_list_row = []
for col in processed_df:
_list_row.append(row[col])
 new_rows.append(_list_row)
 if is_labels_exist is True:
return new_rows, labels
else:
return new_rows
train = train.merge(song_repeat,on="song_id",how="left")
 test = test.merge(song_repeat,on="song_id",how="left" )<data_type_conversions>
def z_score_normalization(series_of_values):
_series_of_values =(series_of_values - series_of_values.mean())/ series_of_values.std()
return _series_of_values
test['song_id'] = test['song_id'].astype('category')
 test['repeat_count'] = test['repeat_count'].fillna(0)
 test['repeat_count'] = test['repeat_count'].astype('int')
 test['play_count'] = test['play_count'].fillna(0)
 test['play_count'] = test['play_count'].astype('int' )<merge>
def shuffle_data(data, labels):
new_df = pd.DataFrame(data)
 new_df['__labels__'] = labels
new_df = new_df.reindex(np.random.permutation(new_df.index))
new_labels = list(new_df['__labels__'])
 del new_df['__labels__']
new_row = []
for index, row in new_df.iterrows() :
_list_row = []
for col in new_df:
_list_row.append(row[col])
 new_row.append(_list_row)
 return new_row, new_labels
artist_count = train.loc[:,["artist_name","target"]]
artist_count1 = artist_count.groupby(["artist_name"],as_index=False ).sum().rename(columns={"target":"repeat_count_artist"})
 artist_count2 = artist_count.groupby(["artist_name"],as_index=False ).count().rename(columns = {"target":"play_count_artist"})
 artist_repeat = artist_count1.merge(artist_count2,how="inner",on="artist_name" )<data_type_conversions>
def run_training(data, labels):
with tf.Graph().as_default() :
data_size = FLAGS.num_variables
num_classes = FLAGS.num_classes
data_placeholder = tf.placeholder("float", shape=(None, data_size))
labels_placeholder = tf.placeholder("int32", shape=None)
 keep_prob = tf.placeholder("float")
 logits = inference(data_placeholder, data_size, keep_prob)
 loss_op = loss(logits, labels_placeholder)
 train_op = training(loss_op)
 eval_correct = evaluation(logits, labels_placeholder)
 summary = tf.summary.merge_all()
init_op = tf.group(tf.global_variables_initializer() , tf.local_variables_initializer())
 saver = tf.train.Saver()
sess = tf.Session()
summary_writer = tf.summary.FileWriter('.', sess.graph)
 sess.run(init_op)
 for epoch in range(FLAGS.num_epochs):
for i in range(int(len(data)/FLAGS.batch_size)) :
batch = FLAGS.batch_size*i
sess.run(train_op, feed_dict={
data_placeholder: data[batch:batch + FLAGS.batch_size],
labels_placeholder: labels[batch:batch + FLAGS.batch_size],
keep_prob: FLAGS.keep_prob})
 train_accuracy = sess.run(eval_correct, feed_dict={
data_placeholder: data,
labels_placeholder: labels,
keep_prob: FLAGS.keep_prob})
 print("epoch %d, acc %g" %(epoch, train_accuracy / len(labels)))
 summary_str = sess.run(summary, feed_dict={
data_placeholder: data,
labels_placeholder: labels,
keep_prob: 1.0})
 summary_writer.add_summary(summary_str, epoch)
 data, labels = shuffle_data(data, labels)
 path = saver.save(sess, 'models.ckpt')
 print('checkpoint is saved at ' + path)
 sess.close()
artist_repeat["repeat_percentage_artist"] = round(( artist_repeat['repeat_count_artist']*100)/ artist_repeat['play_count_artist'],1)
 artist_repeat['repeat_count_artist'] = artist_repeat['repeat_count_artist'].fillna(0)
 artist_repeat['repeat_count_artist'] = artist_repeat['repeat_count_artist'].astype('int')
 artist_repeat['repeat_percentage_artist'] = artist_repeat['repeat_percentage_artist'].replace(100.0,np.nan )<drop_column>
train_data, train_labels = load_data_and_labels('.. /input/train.csv', is_labels_exist=True )
del artist_repeat['repeat_count_artist'] 
<merge>
run_training(train_data, train_labels )
train = train.merge(artist_repeat,on="artist_name",how="left")
 test = test.merge(artist_repeat,on="artist_name",how="left" )<drop_column>
def run_classifier(data):
with tf.Graph().as_default() :
data_size = FLAGS.num_variables
num_classes = FLAGS.num_classes
data_placeholder = tf.placeholder("float", shape=(None, data_size))
logits = inference(data_placeholder, data_size, 1.0)
 init_op = tf.group(tf.global_variables_initializer() , tf.local_variables_initializer())
 sess = tf.Session()
sess.run(init_op)
 saver = tf.train.Saver()
saver.restore(sess, './models.ckpt')
 prediction_list = []
total = len(data)
 with sess.as_default() :
for i in range(total):
v = logits.eval(feed_dict={data_placeholder: [data[i]]})
 sm = tf.nn.softmax(v)
 smv = sm.eval()
prediction_list.append(smv[0])
 cls = np.argmax(smv)
 return prediction_list
del train['artist_name']
del test['artist_name']<merge>
def make_output(class_list):
result_df = pd.DataFrame(class_list)
 train_df =(pd.read_csv('.. /input/train.csv'))
test_df =(pd.read_csv('.. /input/test.csv'))
cat_label_list = train_df['species'].astype('category' ).cat.categories
new_columns = ['id']
new_columns.extend(list(cat_label_list))
result_df.insert(0, 'id', test_df['id'])
 result_df.columns = new_columns
result_df.to_csv(path_or_buf='predicted.csv', index=False, encoding='utf-8' )
<data_type_conversions><EOS> 
test_data = load_data_and_labels('.. /input/test.csv', is_labels_exist=False)
 result_list = run_classifier(test_data)
 make_output(result_list )