Kaggle competition: UMUC DATA 650 Summer 2019 Competition
<SOS> metric: categorizationaccuracy Kaggle data source: umuc-data-650-summer-2019-competition<predict_on_test>
%matplotlib inline
train = pd.read_csv('.. /input/cars-train.csv')
 val = pd.read_csv('.. /input/cars-test.csv')
 x_train = pd.get_dummies(train.drop(['class', 'car.id'], axis=1))
x_val = pd.get_dummies(val.drop(['class', 'car.id'], axis=1))
y_train = pd.get_dummies(train['class'])
 y_val = pd.get_dummies(val['class'] )
pred_cnn_val_y = model.predict([val_X], batch_size=1024, verbose=1)
 for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 print("F1 score at threshold {0} is {1}".format(thresh, metrics.f1_score(val_y,(pred_cnn_val_y>thresh ).astype(int))))<predict_on_test>
model = Sequential()
model.add(Dense(128, input_dim=(21), activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(4, activation = 'softmax'))
model.compile(optimizer='adamax', loss='categorical_crossentropy', metrics=['accuracy'] )
pred_cnn_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<set_options>
model.fit(x_train, y_train, epochs = 600, batch_size = 32 )
del word_index, embeddings_index, all_embs, embedding_matrix, model, inp, x
time.sleep(10 )<set_options>
score = model.evaluate(x_train, y_train)
 print('
train loss is: ' + str(score[0].round(4)))
 print('train accuracy is: ' + str(score[1]))
score = model.evaluate(x_val, y_val)
 print('
test loss is: ' + str(score[0].round(4)))
 print('test accuracy is: ' + str(score[1]))
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim<statistical_test>
test = pd.read_csv('.. /input/cars-final-prediction.csv')
 test_new = pd.get_dummies(test.drop(['car.id'], axis=1))
preds = model.predict_classes(test_new)
 keras_dict = {0: 'acc', 1: 'good', 2: 'unacc', 3: 'vgood'}
converted_preds = []
for prediction in preds:
converted_preds.append(keras_dict[prediction])
 test['class']=converted_preds
output_csv = test[['car.id', 'class']]
output_csv
<train_model><EOS> 
output_csv.to_csv('car-submission.csv', index=False )