Kaggle competition: Statoil/C-CORE Iceberg Classifier Challenge
<SOS> metric: LogLoss Kaggle data source: statoil-iceberg-classifier-challenge<count_values>
np.random.seed(1337)
train['PlayDirection'].value_counts()<feature_engineering>
df_train = pd.read_json('.. /input/train.json' )
train['PlayDirection'] = train['PlayDirection'].apply(lambda x: x is 'right' )<feature_engineering>
def get_scaled_imgs(df):
imgs = []
for i, row in df.iterrows() :
band_1 = np.array(row['band_1'] ).reshape(75, 75)
 band_2 = np.array(row['band_2'] ).reshape(75, 75)
 band_3 = band_1 + band_2
a =(band_1 - band_1.mean())/(band_1.max() - band_1.min())
 b =(band_2 - band_2.mean())/(band_2.max() - band_2.min())
 c =(band_3 - band_3.mean())/(band_3.max() - band_3.min())
 imgs.append(np.dstack(( a, b, c)))
 return np.array(imgs )
train['Team'] = train['Team'].apply(lambda x: x.strip() =='home' )<feature_engineering>
Xtrain = get_scaled_imgs(df_train )
train['GameWeather'] = train['GameWeather'].str.lower()
indoor = "indoor"
train['GameWeather'] = train['GameWeather'].apply(lambda x: indoor if not pd.isna(x)and indoor in x else x)
 train['GameWeather'] = train['GameWeather'].apply(lambda x: x.replace('coudy', 'cloudy' ).replace('clouidy', 'cloudy' ).replace('party', 'partly')if not pd.isna(x)else x)
 train['GameWeather'] = train['GameWeather'].apply(lambda x: x.replace('clear and sunny', 'sunny and clear')if not pd.isna(x)else x)
 train['GameWeather'] = train['GameWeather'].apply(lambda x: x.replace('skies', '' ).replace("mostly", "" ).strip() if not pd.isna(x)else x )<count_values>
Ytrain = np.array(df_train['is_iceberg'] )
weather_count = Counter()
for weather in train['GameWeather']:
if pd.isna(weather):
continue
for word in weather.split() :
weather_count[word]+=1
weather_count.most_common() [:15]<categorify>
df_train.inc_angle = df_train.inc_angle.replace('na',0)
 idx_tr = np.where(df_train.inc_angle>0 )
def map_weather(txt):
ans = 1
if pd.isna(txt):
return 0
if 'partly' in txt:
ans*=0.5
if 'climate controlled' in txt or 'indoor' in txt:
return ans*3
if 'sunny' in txt or 'sun' in txt:
return ans*2
if 'clear' in txt:
return ans
if 'cloudy' in txt:
return -ans
if 'rain' in txt or 'rainy' in txt:
return -2*ans
if 'snow' in txt:
return -3*ans
return 0<feature_engineering>
Ytrain = Ytrain[idx_tr[0]]
Xtrain = Xtrain[idx_tr[0],...]
train['GameWeather'] = train['GameWeather'].apply(map_weather )<feature_engineering>
Xtr_more = get_more_images(Xtrain )
train['IsRusher'] = train['NflId'] == train['NflIdRusher']<drop_column>
Ytr_more = np.concatenate(( Ytrain,Ytrain,Ytrain))
train.drop(['NflId', 'NflIdRusher'], axis=1, inplace=True )<sort_values>
def getModel() :
model=Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),activation='relu', input_shape=(75, 75, 3)))
 model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation="sigmoid"))
optimizer = Adam(lr=0.001, decay=0.0)
 model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 return model
train = train.sort_values(by=['PlayId', 'Team', 'IsRusher'] ).reset_index()<drop_column>
model = getModel()
model.summary()
batch_size = 32
earlyStopping = EarlyStopping(monitor='val_loss', patience=10, verbose=0, mode='min')
 mcp_save = ModelCheckpoint('.mdl_wts.hdf5', save_best_only=True, monitor='val_loss', mode='min')
 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min' )
train.drop(['GameId', 'PlayId', 'index', 'IsRusher', 'Team', 'Season'], axis=1, inplace=True )<drop_column>
model.fit(Xtr_more, Ytr_more, batch_size=batch_size, epochs=50, verbose=1, callbacks=[earlyStopping, mcp_save, reduce_lr_loss], validation_split=0.25 )
cat_features = []
for col in train.columns:
if train[col].dtype =='object':
cat_features.append(col)
 train = train.drop(cat_features, axis=1 )<correct_missing_values>
model.load_weights(filepath = '.mdl_wts.hdf5')
 score = model.evaluate(Xtrain, Ytrain, verbose=1)
 print('Train score:', score[0])
 print('Train accuracy:', score[1] )
<prepare_x_and_y><EOS> 
df_test = pd.read_json('.. /input/test.json')
 df_test.inc_angle = df_test.inc_angle.replace('na',0)
 Xtest =(get_scaled_imgs(df_test))
pred_test = model.predict(Xtest)
 submission = pd.DataFrame({'id': df_test["id"], 'is_iceberg': pred_test.reshape(( pred_test.shape[0])) })
 print(submission.head(10))
submission.to_csv('submission.csv', index=False )