Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<load_from_csv>
%pylab inline
train = reduce_mem_usage(pd.read_csv('.. /input/train.csv'))
test = reduce_mem_usage(pd.read_csv('.. /input/test.csv'))
sei = pd.read_csv('.. /input/song_extra_info.csv')
 members = pd.read_csv('.. /input/members.csv',parse_dates=['registration_init_time','expiration_date'])
 songs = pd.read_csv('.. /input/songs.csv' )<categorify>
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
def get_codes(isrc):
if pd.isnull(isrc):
return np.nan
else:
if int(str(isrc)[5:7])> 17:
temp = 1900+int(str(isrc)[5:7])
 else:
temp = 2000+int(isrc[5:7])
 return temp<feature_engineering>
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical
sei['year'] = sei['isrc'].apply(lambda x: get_codes(x))
sei.sample(10 )<feature_engineering>
print(sys.version )
members['membership_days'] = members['expiration_date'].subtract(members['registration_init_time'] ).dt.days.astype(int)
 members['registration_year'] = members['registration_init_time'].dt.year
members['expiration_year'] = members['expiration_date'].dt.year
members.drop(columns = ['registration_init_time' , 'expiration_date'] , inplace = True)
 members.head()<merge>
pd.__version__
train = train.merge(songs , on='song_id' , how='left')
 train = train.merge(members , on = 'msno' , how='left')
 train = train.merge(sei , on = 'song_id' , how='left')
 test = test.merge(songs , on='song_id' , how='left')
 test = test.merge(members , on = 'msno' , how = 'left')
 test = test.merge(sei , on = 'song_id' , how = 'left')
 del sei ,members , songs
gc.collect()<data_type_conversions>
rcParams['figure.figsize'] = 8,8
print(train['song_length'].isnull().value_counts() /train.shape[0])
 train['song_length'].fillna(train['song_length'].mean() , inplace = True)
 train['song_length'] = train['song_length'].astype(np.uint32)
 print(train['language'].isnull().value_counts() /train.shape[0])
 train['language'].fillna(train['language'].mode().values[0] , inplace= True)
 train['language'] = train['language'].astype(np.int8)
 test['song_length'].fillna(test['song_length'].mean() , inplace = True)
 test['song_length'] = test['song_length'].astype(np.uint32)
 test['language'].fillna(test['language'].mode().values[0] , inplace= True)
 test['language'] = test['language'].astype(np.int8 )<data_type_conversions>
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )
def genre_count(genre):
if genre == 'no_genre_id':
return 0
else :
return genre.count('|')+ 1
print(train['genre_ids'].isnull().value_counts() /train.shape[0])
 train['genre_ids'].fillna('no_genre_id' , inplace= True)
 train['genre_ids_count'] = train['genre_ids'].apply(lambda x: genre_count(x)).astype(np.int8)
 test['genre_ids'].fillna('no_genre_id' , inplace= True)
 test['genre_ids_count'] = test['genre_ids'].apply(lambda x: genre_count(x)).astype(np.int8)
<data_type_conversions>
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )
def artist_count(art):
if art=='no_artist_name':
return 0
else:
return art.count('|')+art.count('/')+ art.count('//')+ art.count(';')+ 1
train['artist_name'].isnull().value_counts()
train['artist_name'].fillna('no_artist_name' , inplace = True)
 train['artist_count'] = train['artist_name'].apply(lambda x : artist_count(x)).astype(np.int8)
 test['artist_name'].fillna('no_artist_name' , inplace = True)
 test['artist_count'] = test['artist_name'].apply(lambda x : artist_count(x)).astype(np.int8 )<count_values>
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )
def count_composer(comp):
if comp=='no_composer':
return 0
else:
return comp.count('|')+comp.count('/')+ comp.count('//')+ comp.count(';')+ 1
def count_lyricist(lyr):
if lyr=='no_lyricist':
return 0
else:
return lyr.count('|')+lyr.count('/')+ lyr.count('//')+ lyr.count(';')+ 1<data_type_conversions>
y_cat = to_categorical(y)
 print(y_cat.shape )
train['composer'].fillna('no_composer',inplace=True)
 train['composer_count'] = train['composer'].apply(lambda x: count_composer(x)).astype(np.int8)
 train['lyricist'].fillna('no_lyricist',inplace=True)
 train['lyricist_count'] = train['lyricist'].apply(lambda x: count_lyricist(x)).astype(np.int8)
 test['composer'].fillna('no_composer',inplace=True)
 test['composer_count'] = test['composer'].apply(lambda x: count_composer(x)).astype(np.int8)
 test['lyricist'].fillna('no_lyricist',inplace=True)
 test['lyricist_count'] = test['lyricist'].apply(lambda x: count_lyricist(x)).astype(np.int8 )<count_values>
model = Sequential()
model.add(Dense(2048,input_dim=192, init='uniform', activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1024, activation='sigmoid'))
model.add(Dropout(0.3))
model.add(Dense(99, activation='softmax'))
dict_count_song_played_train = {k: v for k, v in train['song_id'].value_counts().iteritems() }
dict_count_song_played_test = {k: v for k, v in test['song_id'].value_counts().iteritems() }
def return_number_played(x):
try:
return dict_count_song_played_train[x]
except KeyError:
try:
return dict_count_song_played_test[x]
except KeyError:
return 0
train['number_of_time_played'] = train['song_id'].apply(lambda x: return_number_played(x))
test['number_of_time_played'] = test['song_id'].apply(lambda x: return_number_played(x))<feature_engineering>
model.compile(loss='categorical_crossentropy',optimizer='Adamax', metrics = ["accuracy"] )
dict_user_activity = {k:v for k,v in pd.concat([train['msno'] , test['msno']] , axis = 0 ).value_counts().iteritems() }
def return_user_activity(x):
try:
return dict_user_activity[x]
except KeyError:
return 0
train['user_activity_msno'] = train['msno'].apply(lambda x: return_user_activity(x))
test['user_activity_msno'] = test['msno'].apply(lambda x: return_user_activity(x))<define_variables>
start = time.time()
history = model.fit(X,y_cat,batch_size=100,
nb_epoch=125,verbose=0, validation_split=0.1)
 end = time.time()
print('runtime: ',"%.3f" %(end-start),' [sec]' )
train_col = list(train.columns)
 test_col = list(test.columns)
 for f in test_col :
if f not in train_col:
print('ERROR !!! Column from Test not found in train is ->' , f)
 label_encoding = ['source_system_tab', 'source_screen_name',
'source_type','gender']
drop = ['msno', 'song_id' , 'isrc','artist_name',
'composer', 'lyricist','name','genre_ids']
min_max_scaling = ['number_of_time_played', 'user_activity_msno','membership_days', 'song_length']<categorify>
print('---------------------------------------')
 print('acc: ',max(history.history['acc']))
print('loss: ',min(history.history['loss']))
print('---------------------------------------')
 print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))
for f in label_encoding:
lb = LabelEncoder()
lb.fit(list(train[f].values)+ list(test[f].values))
train[f] = lb.transform(list(train[f].values))
test[f] = lb.transform(list(test[f].values))
for f in min_max_scaling:
ms = MinMaxScaler()
train[f] = ms.fit_transform(train[[f]])
 test[f] = ms.transform(test[[f]])
<data_type_conversions>
test = pd.read_csv('.. /input/test.csv')
 index = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)
 yPred = model.predict_proba(test )
for col in train.columns:
if train[col].dtype == object:
train[col] = train[col].astype('category')
 test[col] = test[col].astype('category' )<prepare_x_and_y>
yPred = pd.DataFrame(yPred,index=index,columns=sort(parent_data.species.unique()))
<init_hyperparams><EOS> 
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv() )