Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<normalization>
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
up = market_train.returnsOpenNextMktres10 >= 0
fcol = [c for c in market_train if c not in ['assetCode', 'assetCodes', 'assetCodesLen', 'assetName', 'audiences',
'firstCreated', 'headline', 'headlineTag', 'marketCommentary', 'provider',
'returnsOpenNextMktres10', 'sourceId', 'subjects', 'time', 'time_x', 'universe','sourceTimestamp']]
X = market_train[fcol].values
up = up.values
r = market_train.returnsOpenNextMktres10.values
mins = np.min(X, axis=0)
 maxs = np.max(X, axis=0)
 rng = maxs - mins
X = 1 -(( maxs - X)/ rng)
 assert X.shape[0] == up.shape[0] == r.shape[0]
te = market_train_df['time']>date(2015, 1, 1)
 tt = 0
for tt,i in enumerate(te.values):
if i:
idx = tt
print(i,tt)
 break
print(idx)
 X_train, X_test = X[:idx],X[idx:]
up_train, up_test = up[:idx],up[idx:]
r_train, r_test = r[:idx],r[idx:]
u_train,u_test = universe[:idx],universe[idx:]
d_train,d_test = d[:idx],d[idx:]
train_data = lgb.Dataset(X_train, label=up_train.astype(int))
test_data = lgb.Dataset(X_test, label=up_test.astype(int))<train_model>
origin_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 validate = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )
<init_hyperparams>
train = pd.concat([origin_train, validate] )
x_1 = [0.19055524469598384, 2453, 229, 329, 200]
x_2 = [0.19986071577294634, 2590, 230, 310, 202]
params_1 = {
'task': 'train',
'boosting_type': 'dart',
'objective': 'binary',
'learning_rate': x_1[0],
'num_leaves': x_1[1],
'min_data_in_leaf': x_1[2],
'num_iteration': x_1[3],
'max_bin': x_1[4],
'verbose': 1
}
params_2 = {
'task': 'train',
'boosting_type': 'dart',
'objective': 'binary',
'learning_rate': x_2[0],
'num_leaves': x_2[1],
'min_data_in_leaf': x_2[2],
'num_iteration': x_2[3],
'max_bin': x_2[4],
'verbose': 1
}
gbm_1 = lgb.train(params_1,
train_data,
num_boost_round=100,
valid_sets=test_data,
early_stopping_rounds=5)
 gbm_2 = lgb.train(params_2,
train_data,
num_boost_round=100,
valid_sets=test_data,
early_stopping_rounds=5)
<compute_train_metric>
X_train = train.iloc[:, 1:]
y_train = train.iloc[:, 0]
print(X_train.shape, y_train.shape )
confidence_test =(gbm_1.predict(X_test)+ gbm_2.predict(X_test)) /2
confidence_test =(confidence_test-confidence_test.min())/(confidence_test.max() -confidence_test.min())
 confidence_test = confidence_test*2-1
print(max(confidence_test),min(confidence_test))
r_test = r_test.clip(-1,1)
 x_t_i = confidence_test * r_test * u_test
data = {'day' : d_test, 'x_t_i' : x_t_i}
df = pd.DataFrame(data)
 x_t = df.groupby('day' ).sum().values.flatten()
mean = np.mean(x_t)
 std = np.std(x_t)
 score_test = mean / std
print(score_test)
 del X_train,X_test
gc.collect()<install_modules>
X_train, X_test, y_train, y_test = train_test_split(X_train.values, y_train.values, test_size=0.2, random_state=0 )
!pip install bayesian-optimization<set_options>
def func_prep(data_prep):
data_prep_data=[]
for i in range(data_prep.shape[0]):
num=np.array(data_prep[i:i+1])
 num=num.ravel()
num=num.reshape(( 28,28))
data_prep_data.append(num)
 return np.array(data_prep_data)
 X_train=func_prep(X_train)
 X_test=np.array(X_test )
pd.set_option('display.max_columns', 200 )<load_from_csv>
img_rows, img_cols = 28, 28
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
 X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
 input_shape =(img_rows, img_cols, 1 )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<define_variables>
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
 print(X_train.shape[0], 'train samples')
 print(X_test.shape[0], 'test samples' )
target = 'target'
predictors = train_df.columns.values.tolist() [2:]<count_values>
num_classes = 10
y_train = to_categorical(y_train, num_classes)
 y_test = to_categorical(y_test, num_classes )
train_df.target.value_counts()<normalization>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(img_rows,img_cols,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
bayesian_tr_index, bayesian_val_index = list(StratifiedKFold(n_splits=2, shuffle=True, random_state=1 ).split(train_df, train_df.target.values)) [0]<choose_model_class>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',patience=3,factor=0.2,min_lr=0.00001)
 datagen = ImageDataGenerator(rotation_range=10,zoom_range = 0.1, width_shift_range=0.1, height_shift_range=0.1)
 hist = model.fit(x=datagen.flow(X_train,y_train, batch_size=128),
epochs = 20,
verbose = 2, steps_per_epoch=X_train.shape[0] // 128
,callbacks=[learning_rate_reduction] )
def LGB_bayesian(
 num_leaves,
min_data_in_leaf,
learning_rate,
min_sum_hessian_in_leaf,
feature_fraction,
lambda_l1,
lambda_l2,
min_gain_to_split,
max_depth):
num_leaves = int(num_leaves)
 min_data_in_leaf = int(min_data_in_leaf)
 max_depth = int(max_depth)
 assert type(num_leaves)== int
assert type(min_data_in_leaf)== int
assert type(max_depth)== int
param = {
'num_leaves': num_leaves,
'max_bin': 63,
'min_data_in_leaf': min_data_in_leaf,
'learning_rate': learning_rate,
'min_sum_hessian_in_leaf': min_sum_hessian_in_leaf,
'bagging_fraction': 1.0,
'bagging_freq': 5,
'feature_fraction': feature_fraction,
'lambda_l1': lambda_l1,
'lambda_l2': lambda_l2,
'min_gain_to_split': min_gain_to_split,
'max_depth': max_depth,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False,
}
xg_train = lgb.Dataset(train_df.iloc[bayesian_tr_index][predictors].values,
label=train_df.iloc[bayesian_tr_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train_df.iloc[bayesian_val_index][predictors].values,
label=train_df.iloc[bayesian_val_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 num_round = 5000
clf = lgb.train(param, xg_train, num_round, valid_sets = [xg_valid], verbose_eval=250, early_stopping_rounds = 50)
 predictions = clf.predict(train_df.iloc[bayesian_val_index][predictors].values, num_iteration=clf.best_iteration)
 score = metrics.roc_auc_score(train_df.iloc[bayesian_val_index][target].values, predictions)
 return score<define_search_space>
score = model.evaluate(X_test, y_test, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )
bounds_LGB = {
'num_leaves':(5, 20),
'min_data_in_leaf':(5, 20),
'learning_rate':(0.01, 0.3),
'min_sum_hessian_in_leaf':(0.00001, 0.01),
'feature_fraction':(0.05, 0.5),
'lambda_l1':(0, 5.0),
'lambda_l2':(0, 5.0),
'min_gain_to_split':(0, 1.0),
'max_depth':(3,15),
}<import_modules>
test = pd.read_csv('.. /input/Kannada-MNIST/test.csv' )
from bayes_opt import BayesianOptimization<choose_model_class>
X_test = test.values[:, 1:]
X_test = func_prep(X_test)
 X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
 X_test = X_test.astype('float32')
 X_test /= 255
LGB_BO = BayesianOptimization(LGB_bayesian, bounds_LGB, random_state=13 )<define_variables>
t = time.process_time()
preds = np.argmax(model.predict(X_test), axis=-1)
 elapsed_time = time.process_time() - t
print('elapsed_time: ', elapsed_time )
<find_best_params><EOS> 
df = pd.DataFrame({'id': [ i for i in range(len(preds)) ], 'label': preds})
 df.to_csv('submission.csv', index=False )