Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<save_model>
cf.go_offline()
py.init_notebook_mode()
cf.getThemes()
cf.set_config_file(theme='ggplot')
 warnings.simplefilter('ignore')
 pd.plotting.register_matplotlib_converters()
sns.mpl.rc('figure',figsize=(16, 6))
plt.style.use('ggplot')
 sns.set_style('darkgrid')
learn1.save('unfreeze_densenet121_learn1' )<choose_model_class>
base = os.path.abspath('/kaggle/input/liverpool-ion-switching/')
 train = pd.read_csv(os.path.join(base + '/train.csv'))
test = pd.read_csv(os.path.join(base + '/test.csv'))
interp = ClassificationInterpretation.from_learner(learn1 )<drop_column>
train2 = train.copy()
a=500000; b=600000
train2.loc[train.index[a:b],'signal2'] = train2.signal[a:b].values - 3*(train2.time.values[a:b] - 50)/10.
 def f(x,low,high,mid): return -(( -low+high)/625)*(x-mid)**2+high -low
batch = 7; a = 500000*(batch-1); b = 500000*batch
train2.loc[train2.index[a:b],'signal2'] = train.signal.values[a:b] - f(train.time[a:b].values,-1.817,3.186,325)
 batch = 8; a = 500000*(batch-1); b = 500000*batch
train2.loc[train2.index[a:b],'signal2'] = train.signal.values[a:b] - f(train.time[a:b].values,-0.094,4.936,375)
 batch = 9; a = 500000*(batch-1); b = 500000*batch
train2.loc[train2.index[a:b],'signal2'] = train.signal.values[a:b] - f(train.time[a:b].values,1.715,6.689,425)
 batch = 10; a = 500000*(batch-1); b = 500000*batch
train2.loc[train2.index[a:b],'signal2'] = train.signal.values[a:b] - f(train.time[a:b].values,3.361,8.45,475 )
test_df.drop('id',axis = 'columns',inplace = True )<define_variables>
test2 = test.copy()
start=500
a = 0; b = 100000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - 3*(test2.time.values[a:b]-start)/10.
 start=510
a = 100000; b = 200000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - 3*(test2.time.values[a:b]-start)/10.
 start=540
a = 400000; b = 500000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - 3*(test2.time.values[a:b]-start)/10.
 start=560
a = 600000; b = 700000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - 3*(test2.time.values[a:b]-start)/10.
 start=570
a = 700000; b = 800000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - 3*(test2.time.values[a:b]-start)/10.
 start=580
a = 800000; b = 900000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - 3*(test2.time.values[a:b]-start)/10.
 def f(x):
return -(0.00788)*(x-625)**2+2.345 +2.58
a = 1000000; b = 1500000
test2.loc[test2.index[a:b],'signal2'] = test2.signal.values[a:b] - f(test2.time[a:b].values )
img_arr = np.array(test_df )<feature_engineering>
def features(df):
df = df.sort_values(by=['time'] ).reset_index(drop=True)
 df.index =(( df.time * 10_000)- 1 ).values
df['batch'] = df.index // 25_000
df['batch_index'] = df.index -(df.batch * 25_000)
 df['batch_slices'] = df['batch_index'] // 2500
df['batch_slices2'] = df.apply(lambda r: '_'.join([str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 for j in ['signal']:
for c in ['batch','batch_slices2']:
d = {}
d['mean'+c+j] = df.groupby([c])[j].mean()
d['median'+c+j] = df.groupby([c])[j].median()
d['max'+c+j] = df.groupby([c])[j].max()
d['min'+c+j] = df.groupby([c])[j].min()
d['std'+c+j] = df.groupby([c])[j].std()
d['mean_abs_chg'+c+j] = df.groupby([c])[j].apply(lambda x: np.mean(np.abs(np.diff(x))))
d['abs_max'+c+j] = df.groupby([c])[j].apply(lambda x: np.max(np.abs(x)))
 d['abs_min'+c+j] = df.groupby([c])[j].apply(lambda x: np.min(np.abs(x)))
 d['range'+c+j] = d['max'+c+j] - d['min'+c+j]
d['maxtomin'+c+j] = d['max'+c+j] / d['min'+c+j]
d['abs_avg'+c+j] =(d['abs_min'+c+j] + d['abs_max'+c+j])/ 2
for v in d:
df[v] = df[c].map(d[v].to_dict())
 df['signal_shift_+1'+j] = df[j].shift(1)
 df['signal_shift_-1'+j] = df[j].shift(-1)
 for c in [c1 for c1 in df.columns if c1 not in ['time', 'signal', 'open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]:
df[c+'_msignal'] = df[c] - df['signal']
return df
train2 = features(train2)
 test = features(test2 )
for i in range(len(img_arr)) :
img_data = img_arr[i].reshape(28,28)/255.
 img_data = np.stack(( img_data,)*3, axis=0)
 img = Image(FloatTensor(img_data))
tmp_df.loc[i]=[i+1,int(learn1.predict(img)[1])]<feature_engineering>
col = [c for c in train2.columns if c not in ['time', 'open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]
tmp_df['id'] = tmp_df['id'].apply(lambda k: k-1 )<save_to_csv>
def MacroF1Metric(preds, dtrain):
labels = dtrain.get_label()
preds = np.round(np.clip(preds, 0, 10)).astype(int)
 score = metrics.f1_score(labels, preds, average = 'macro')
 return('MacroF1Metric', score, True )
tmp_df.to_csv('submission.csv',index=False )<set_options>
model_batch = [[1,2],[3,7],[4,8],[6,9],[5,10]]
%matplotlib inline<load_from_csv>
sub = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv' )
train=pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test=pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )<prepare_x_and_y>
valid_predict_LGBM =[]
valid_target_LGBM =[]
valid_predict_XGB =[]
valid_target_XGB =[]
valid_predict_CAT =[]
valid_target_CAT =[]
test=test.drop('id',axis=1)
 X_train=train.drop('label',axis=1)
 Y_train=train.label
X_train=X_train/255
test=test/255
X_train=X_train.values.reshape(-1,28,28,1)
 test=test.values.reshape(-1,28,28,1)
 print('The shape of train set now is',X_train.shape)
 print('The shape of test set now is',test.shape )<split>
sub['LGBM_preds']=sub.open_channels
sub['XGB_preds']=sub.open_channels
sub['CAT_preds']=sub.open_channels
Y_train=to_categorical(Y_train)
 X_train,X_test,y_train,y_test=train_test_split(X_train,Y_train,random_state=42,test_size=0.1 )<train_model>
for j,idx in enumerate(model_batch[0]):
if j ==0:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split = train2[col].loc[train2.index[a:b]]
train_model_target = train2.loc[train2.index[a:b],'open_channels']
else:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split1 = train2[col].loc[train2.index[a:b]]
train_model_target1 = train2.loc[train2.index[a:b],'open_channels']
train_model_split = np.concatenate(( train_model_split,train_model_split1),axis = 0)
 train_model_target = np.concatenate(( train_model_target,train_model_target1),axis = 0)
 x1, x2, y1, y2 = model_selection.train_test_split(train_model_split, train_model_target , test_size=0.2, random_state=49)
 print('============================ start training LGBM ==================================')
 params = {'learning_rate': 0.1, 'max_depth': -1, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1, 'sample_fraction':0.33}
model = lgb.train(params, lgb.Dataset(x1, y1), 22222, lgb.Dataset(x2, y2), verbose_eval=100, early_stopping_rounds=250, feval=MacroF1Metric)
 oof_valid_lgb =(model.predict(x2, num_iteration=model.best_iteration)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_lgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
valid_predict_LGBM = valid_predict_LGBM + oof_valid_lgb.tolist()
valid_target_LGBM = valid_target_LGBM + np.array(y2 ).tolist()
print('============================ start LGBM predict==================================')
 a = 0
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 a = 3
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 a = 8
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 test_batch = test[col].loc[test.index[1000000:2000000]]
print('start test last subsample'+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[1000000:2000000,2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 print(gc.collect())
 print('============================ start training XGBBOOST ==================================')
 params = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse', 'n_estimators':22222, 'tree_method':'gpu_hist',}
train_set = xgb.DMatrix(x1, y1)
 val_set = xgb.DMatrix(x2, y2)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=100, early_stopping_rounds=250)
 oof_valid_xgb =(model.predict(xgb.DMatrix(x2), ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_xgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_set, val_set; gc.collect()
valid_predict_XGB = valid_predict_XGB + oof_valid_xgb.tolist()
valid_target_XGB = valid_target_XGB + np.array(y2 ).tolist()
print('============================ start XGB predict==================================')
 a = 0
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 a = 3
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 a = 8
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 test_batch = test[col].loc[test.index[1000000:2000000]]
print('start test last subsample'+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[1000000:2000000,3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print('============================ start training Catboost ==================================')
 model = CatBoostRegressor(task_type = 'GPU', iterations=22222, learning_rate=0.1, random_seed = 7, depth=7, eval_metric='RMSE')
 train_dataset = Pool(x1, y1)
 eval_dataset = Pool(x2, y2)
 model.fit(train_dataset, eval_set=eval_dataset, verbose=100, early_stopping_rounds=250)
 oof_valid_cb =(model.predict(x2)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_cb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_dataset, eval_dataset
print(gc.collect())
 valid_predict_CAT = valid_predict_CAT + oof_valid_cb.tolist()
valid_target_CAT = valid_target_CAT + np.array(y2 ).tolist()
print('============================ start Catboost predict==================================')
 a = 0
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
 a = 3
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
 a = 8
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
 test_batch = test[col].loc[test.index[1000000:2000000]]
print('start test last subsample'+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[1000000:2000000,4] =(model.predict(test_batch)).astype(np.float16 )
datagen = ImageDataGenerator(rotation_range = 10,
width_shift_range = 0.3,
height_shift_range = 0.3,
shear_range = 0.15,
zoom_range = 0.3, 
)
 datagen.fit(X_train )<choose_model_class>
for j,idx in enumerate(model_batch[1]):
if j ==0:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split = train2[col].loc[train2.index[a:b]]
train_model_target = train2.loc[train2.index[a:b],'open_channels']
else:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split1 = train2[col].loc[train2.index[a:b]]
train_model_target1 = train2.loc[train2.index[a:b],'open_channels']
train_model_split = np.concatenate(( train_model_split,train_model_split1),axis = 0)
 train_model_target = np.concatenate(( train_model_target,train_model_target1),axis = 0)
 x1, x2, y1, y2 = model_selection.train_test_split(train_model_split, train_model_target , test_size=0.2, random_state=49)
 print('============================ start training LGBM ==================================')
 params = {'learning_rate': 0.1, 'max_depth': -1, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1, 'sample_fraction':0.33}
model = lgb.train(params, lgb.Dataset(x1, y1), 22222, lgb.Dataset(x2, y2), verbose_eval=100, early_stopping_rounds=250, feval=MacroF1Metric)
 oof_valid_lgb =(model.predict(x2, num_iteration=model.best_iteration)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_lgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
valid_predict_LGBM = valid_predict_LGBM + oof_valid_lgb.tolist()
valid_target_LGBM = valid_target_LGBM + np.array(y2 ).tolist()
print('============================ start LGBM predict==================================')
 a = 4
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 gc.collect()
print('============================ start training XGBBOOST ==================================')
 params = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse', 'n_estimators':22222, 'tree_method':'gpu_hist',}
train_set = xgb.DMatrix(x1, y1)
 val_set = xgb.DMatrix(x2, y2)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=100, early_stopping_rounds=250)
 oof_valid_xgb =(model.predict(xgb.DMatrix(x2), ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_xgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_set, val_set; gc.collect()
valid_predict_XGB = valid_predict_XGB + oof_valid_xgb.tolist()
valid_target_XGB = valid_target_XGB + np.array(y2 ).tolist()
print('============================ start XGB predict==================================')
 a = 4
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print('============================ start training Catboost ==================================')
 model = CatBoostRegressor(task_type = 'GPU', iterations=22222, learning_rate=0.1, random_seed = 7, depth=7, eval_metric='RMSE')
 train_dataset = Pool(x1, y1)
 eval_dataset = Pool(x2, y2)
 model.fit(train_dataset, eval_set=eval_dataset, verbose=100, early_stopping_rounds=250)
 oof_valid_cb =(model.predict(x2)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_cb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_dataset, eval_dataset; gc.collect()
valid_predict_CAT = valid_predict_CAT + oof_valid_cb.tolist()
valid_target_CAT = valid_target_CAT + np.array(y2 ).tolist()
print('============================ start Catboost predict==================================')
 a = 4
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
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(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.4))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
for j,idx in enumerate(model_batch[2]):
if j ==0:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split = train2[col].loc[train2.index[a:b]]
train_model_target = train2.loc[train2.index[a:b],'open_channels']
else:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split1 = train2[col].loc[train2.index[a:b]]
train_model_target1 = train2.loc[train2.index[a:b],'open_channels']
train_model_split = np.concatenate(( train_model_split,train_model_split1),axis = 0)
 train_model_target = np.concatenate(( train_model_target,train_model_target1),axis = 0)
 x1, x2, y1, y2 = model_selection.train_test_split(train_model_split, train_model_target , test_size=0.2, random_state=49)
 print('============================ start training LGBM ==================================')
 params = {'learning_rate': 0.1, 'max_depth': -1, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1, 'sample_fraction':0.33}
model = lgb.train(params, lgb.Dataset(x1, y1), 22222, lgb.Dataset(x2, y2), verbose_eval=100, early_stopping_rounds=250, feval=MacroF1Metric)
 oof_valid_lgb =(model.predict(x2, num_iteration=model.best_iteration)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_lgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
valid_predict_LGBM = valid_predict_LGBM + oof_valid_lgb.tolist()
valid_target_LGBM = valid_target_LGBM + np.array(y2 ).tolist()
print('============================ start LGBM predict==================================')
 a = 1
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 a = 9
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 gc.collect()
print('============================ start training XGBBOOST ==================================')
 params = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse', 'n_estimators':22222, 'tree_method':'gpu_hist',}
train_set = xgb.DMatrix(x1, y1)
 val_set = xgb.DMatrix(x2, y2)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=100, early_stopping_rounds=250)
 oof_valid_xgb =(model.predict(xgb.DMatrix(x2), ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_xgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_set, val_set; gc.collect()
valid_predict_XGB = valid_predict_XGB + oof_valid_xgb.tolist()
valid_target_XGB = valid_target_XGB + np.array(y2 ).tolist()
print('============================ start XGB predict==================================')
 a = 1
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 a = 9
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print('============================ start training Catboost ==================================')
 model = CatBoostRegressor(task_type = 'GPU', iterations=22222, learning_rate=0.1, random_seed = 7, depth=7, eval_metric='RMSE')
 train_dataset = Pool(x1, y1)
 eval_dataset = Pool(x2, y2)
 model.fit(train_dataset, eval_set=eval_dataset, verbose=100, early_stopping_rounds=250)
 oof_valid_cb =(model.predict(x2)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_cb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_dataset, eval_dataset; gc.collect()
valid_predict_CAT = valid_predict_CAT + oof_valid_cb.tolist()
valid_target_CAT = valid_target_CAT + np.array(y2 ).tolist()
print('============================ start Catboost predict==================================')
 a = 1
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
 a = 9
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16 )
epochs=100
batch_size=1024
optimizer=Adam(learning_rate=0.001,beta_1=0.9,beta_2=0.999)
 model.compile(optimizer=optimizer,loss=['categorical_crossentropy'],metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=2,
verbose=1,
factor=0.5,
min_lr=0.00001)
 early_stopping = EarlyStopping(monitor='val_loss',
min_delta=0,
patience=10,
verbose=0,
mode='auto')
 callbacks = [learning_rate_reduction, early_stopping]<train_model>
for j,idx in enumerate(model_batch[3]):
if j ==0:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split = train2[col].loc[train2.index[a:b]]
train_model_target = train2.loc[train2.index[a:b],'open_channels']
else:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split1 = train2[col].loc[train2.index[a:b]]
train_model_target1 = train2.loc[train2.index[a:b],'open_channels']
train_model_split = np.concatenate(( train_model_split,train_model_split1),axis = 0)
 train_model_target = np.concatenate(( train_model_target,train_model_target1),axis = 0)
 x1, x2, y1, y2 = model_selection.train_test_split(train_model_split, train_model_target , test_size=0.2, random_state=49)
 print('============================ start training LGBM ==================================')
 params = {'learning_rate': 0.1, 'max_depth': -1, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1, 'sample_fraction':0.33}
model = lgb.train(params, lgb.Dataset(x1, y1), 22222, lgb.Dataset(x2, y2), verbose_eval=100, early_stopping_rounds=250, feval=MacroF1Metric)
 oof_valid_lgb =(model.predict(x2, num_iteration=model.best_iteration)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_lgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
valid_predict_LGBM = valid_predict_LGBM + oof_valid_lgb.tolist()
valid_target_LGBM = valid_target_LGBM + np.array(y2 ).tolist()
print('============================ start LGBM predict==================================')
 a = 2
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 a = 6
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 gc.collect()
print('============================ start training XGBBOOST ==================================')
 params = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse', 'n_estimators':22222, 'tree_method':'gpu_hist',}
train_set = xgb.DMatrix(x1, y1)
 val_set = xgb.DMatrix(x2, y2)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=100, early_stopping_rounds=250)
 oof_valid_xgb =(model.predict(xgb.DMatrix(x2), ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_xgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_set, val_set; gc.collect()
valid_predict_XGB = valid_predict_XGB + oof_valid_xgb.tolist()
valid_target_XGB = valid_target_XGB + np.array(y2 ).tolist()
print('============================ start XGB predict==================================')
 a = 2
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 a = 6
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print('============================ start training Catboost ==================================')
 model = CatBoostRegressor(task_type = 'GPU', iterations=22222, learning_rate=0.1, random_seed = 7, depth=7, eval_metric='RMSE')
 train_dataset = Pool(x1, y1)
 eval_dataset = Pool(x2, y2)
 model.fit(train_dataset, eval_set=eval_dataset, verbose=100, early_stopping_rounds=250)
 oof_valid_cb =(model.predict(x2)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_cb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_dataset, eval_dataset; gc.collect()
valid_predict_CAT = valid_predict_CAT + oof_valid_cb.tolist()
valid_target_CAT = valid_target_CAT + np.array(y2 ).tolist()
print('============================ start Catboost predict==================================')
 a = 2
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
 a = 6
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16 )
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_test,y_test),
verbose = 2,
steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=callbacks )<predict_on_test>
for j,idx in enumerate(model_batch[4]):
if j ==0:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split = train2[col].loc[train2.index[a:b]]
train_model_target = train2.loc[train2.index[a:b],'open_channels']
else:
batch = idx; a = 500000*(batch-1); b = 500000*batch
train_model_split1 = train2[col].loc[train2.index[a:b]]
train_model_target1 = train2.loc[train2.index[a:b],'open_channels']
train_model_split = np.concatenate(( train_model_split,train_model_split1),axis = 0)
 train_model_target = np.concatenate(( train_model_target,train_model_target1),axis = 0)
 x1, x2, y1, y2 = model_selection.train_test_split(train_model_split, train_model_target , test_size=0.2, random_state=49)
 print('============================ start training LGBM ==================================')
 params = {'learning_rate': 0.1, 'max_depth': -1, 'num_leaves':2**7+1, 'metric': 'rmse', 'random_state': 7, 'n_jobs':-1, 'sample_fraction':0.33}
model = lgb.train(params, lgb.Dataset(x1, y1), 22222, lgb.Dataset(x2, y2), verbose_eval=100, early_stopping_rounds=250, feval=MacroF1Metric)
 oof_valid_lgb =(model.predict(x2, num_iteration=model.best_iteration)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_lgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
valid_predict_LGBM = valid_predict_LGBM + oof_valid_lgb.tolist()
valid_target_LGBM = valid_target_LGBM + np.array(y2 ).tolist()
print('============================ start LGBM predict==================================')
 a = 5
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 a = 7
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),2] =(model.predict(test_batch, num_iteration=model.best_iteration)).astype(np.float16)
 gc.collect()
print('============================ start training XGBBOOST ==================================')
 params = {'colsample_bytree': 0.375,'learning_rate': 0.1,'max_depth': 10, 'subsample': 1, 'objective':'reg:squarederror',
'eval_metric':'rmse', 'n_estimators':22222, 'tree_method':'gpu_hist',}
train_set = xgb.DMatrix(x1, y1)
 val_set = xgb.DMatrix(x2, y2)
 model = xgb.train(params, train_set, num_boost_round=2222, evals=[(train_set, 'train'),(val_set, 'val')],
verbose_eval=100, early_stopping_rounds=250)
 oof_valid_xgb =(model.predict(xgb.DMatrix(x2), ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_xgb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_set, val_set; gc.collect()
valid_predict_XGB = valid_predict_XGB + oof_valid_xgb.tolist()
valid_target_XGB = valid_target_XGB + np.array(y2 ).tolist()
print('============================ start XGB predict==================================')
 a = 5
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 a = 7
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),3] =(model.predict(xgb.DMatrix(np.array(test_batch)) , ntree_limit=model.best_ntree_limit)).astype(np.float16)
 print('============================ start training Catboost ==================================')
 model = CatBoostRegressor(task_type = 'GPU', iterations=22222, learning_rate=0.1, random_seed = 7, depth=7, eval_metric='RMSE')
 train_dataset = Pool(x1, y1)
 eval_dataset = Pool(x2, y2)
 model.fit(train_dataset, eval_set=eval_dataset, verbose=100, early_stopping_rounds=250)
 oof_valid_cb =(model.predict(x2)).astype(np.float16)
 print("BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(oof_valid_cb, 0, 10)).astype(int),np.array(y2),average = 'macro'))
del train_dataset, eval_dataset; gc.collect()
valid_predict_CAT = valid_predict_CAT + oof_valid_cb.tolist()
valid_target_CAT = valid_target_CAT + np.array(y2 ).tolist()
print('============================ start Catboost predict==================================')
 a = 5
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
 a = 7
test_batch = test[col].loc[test.index[100000*a:100000*(a+1)]]
print('start test subsample {}'.format(a)+', test_batch shape= ' + str(test_batch.shape))
sub.iloc[100000*a:100000*(a+1),4] =(model.predict(test_batch)).astype(np.float16)
y_pre_test=model.predict(X_test)
 y_pre_test=np.argmax(y_pre_test,axis=1)
 y_test=np.argmax(y_test,axis=1 )<save_to_csv>
print("LGBM BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(valid_predict_LGBM, 0, 10)).astype(int),np.array(valid_target_LGBM),average = 'macro'))
print("XGB BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(valid_predict_XGB, 0, 10)).astype(int),np.array(valid_target_XGB),average = 'macro'))
print("XGB BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(valid_predict_CAT, 0, 10)).astype(int),np.array(valid_target_CAT),average = 'macro'))
y_pre=model.predict(test)
 y_pre=np.argmax(y_pre,axis=1)
 sample_sub['label']=y_pre
sample_sub.to_csv('submission.csv',index=False )<load_from_csv>
Ensemble_predict =np.array(valid_predict_LGBM)*0.4+np.array(valid_predict_XGB)*0.4+np.array(valid_predict_CAT)*0.2
print("Ensemble BEST VALIDATION_SCORE(F1): ", f1_score(np.round(np.clip(Ensemble_predict, 0, 10)).astype(int),np.array(valid_target_XGB),average = 'macro'))
train=pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test=pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 submission_sample = pd.read_csv(".. /input/Kannada-MNIST/sample_submission.csv")
 train_CNN=train
Gtrain=train
label0=Gtrain.groupby('label' ).get_group(0)
 label1=Gtrain.groupby('label' ).get_group(1)
 label2=Gtrain.groupby('label' ).get_group(2)
 label3=Gtrain.groupby('label' ).get_group(3)
 label4=Gtrain.groupby('label' ).get_group(4)
 label5=Gtrain.groupby('label' ).get_group(5)
 label6=Gtrain.groupby('label' ).get_group(6)
 label7=Gtrain.groupby('label' ).get_group(7)
 label8=Gtrain.groupby('label' ).get_group(8)
 label9=Gtrain.groupby('label' ).get_group(9)
 X_label0=label0.drop('label',axis=1)
 X_label1=label1.drop('label',axis=1)
 X_label2=label2.drop('label',axis=1)
 X_label3=label3.drop('label',axis=1)
 X_label4=label4.drop('label',axis=1)
 X_label5=label5.drop('label',axis=1)
 X_label6=label6.drop('label',axis=1)
 X_label7=label7.drop('label',axis=1)
 X_label8=label8.drop('label',axis=1)
 X_label9=label9.drop('label',axis=1)
 Y_label0=label0.label
Y_label1=label1.label
Y_label2=label2.label
Y_label3=label3.label
Y_label4=label4.label
Y_label5=label5.label
Y_label6=label6.label
Y_label7=label7.label
Y_label8=label8.label
Y_label9=label9.label
X_label0=np.asarray(X_label0,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label1=np.asarray(X_label1,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label2=np.asarray(X_label2,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label3=np.asarray(X_label3,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label4=np.asarray(X_label4,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label5=np.asarray(X_label5,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label6=np.asarray(X_label6,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label7=np.asarray(X_label7,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label8=np.asarray(X_label8,dtype=np.float32 ).reshape(-1,28,28,1)
 X_label9=np.asarray(X_label9,dtype=np.float32 ).reshape(-1,28,28,1)
 test=test.drop('id',axis=1)
 y=train.label.value_counts()
X_train=train.drop('label',axis=1)
 Y_train=train.label
X_train=np.array(X_train,dtype=np.float32)
 Y_train=np.array(Y_train)
 X_train=X_train/255
test=test/255
X_train=X_train.reshape(-1,28,28,1)
 new_image_set=X_train
test=test.values.reshape(-1,28,28,1)
 X_train,X_test,y_train,y_test=train_test_split(X_train,Y_train,random_state=42,test_size=0.15)
<prepare_x_and_y>
class OptimizedRounder(object):

 def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
 return -f1_score(y, X_p,average = 'macro')
 def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
return pd.cut(X, [-np.inf] + list(np.sort(coef)) + [np.inf], labels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
 def coefficients(self):
return self.coef_['x']
train_CNN_X=train_CNN.drop('label',axis=1)
 train_CNN_Y=train.label
train_CNN_X=np.array(train_CNN_X,dtype=np.float32)
 train_CNN_Y=np.array(train_CNN_Y)
 train_CNN_X=train_CNN_X/255 
<define_variables>
%%time
optR = OptimizedRounder()
optR.fit(np.array(Ensemble_predict ).reshape(-1,), np.array(valid_target_XGB))
coefficients = optR.coefficients()
print(coefficients )
BUFFER_SIZE = 6000
BATCH_SIZE = 256<choose_model_class>
opt_preds = optR.predict(np.array(Ensemble_predict ).reshape(-1,), coefficients)
 print('f1', metrics.f1_score(np.array(valid_target_XGB), opt_preds, average = 'macro'))
def make_generator_model() :
model = tf.keras.Sequential()
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
 model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU())
 model.add(layers.Reshape(( 7, 7, 256)))
 assert model.output_shape ==(None, 7, 7, 256)
 model.add(layers.Conv2DTranspose(128,(5, 5), strides=(1, 1), padding='same', use_bias=False))
assert model.output_shape ==(None, 7, 7, 128)
 model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU())
 model.add(layers.Conv2DTranspose(64,(5, 5), strides=(2, 2), padding='same', use_bias=False))
assert model.output_shape ==(None, 14, 14, 64)
 model.add(layers.BatchNormalization())
 model.add(layers.LeakyReLU())
 model.add(layers.Conv2DTranspose(1,(5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
assert model.output_shape ==(None, 28, 28, 1)
 return model<choose_model_class>
predict_logic = sub.LGBM_preds*0.4+sub.XGB_preds*0.4+sub.CAT_preds*0.2
opt_test_preds = optR.predict(np.array(predict_logic ).reshape(-1,), coefficients )
def make_discriminator_model() :
model = tf.keras.Sequential()
model.add(layers.Conv2D(64,(5, 5), strides=(2, 2), padding='same',
input_shape=[28, 28, 1]))
model.add(layers.LeakyReLU())
 model.add(layers.Dropout(0.3))
model.add(layers.Conv2D(128,(5, 5), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU())
 model.add(layers.Dropout(0.3))
model.add(layers.Flatten())
 model.add(layers.Dense(1))
return model<define_search_model>
submit = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv')
 submit.open_channels = opt_test_preds.astype(int)
 submit.to_csv('submission_lgb.csv', index=False, float_format='%.4f' )
<choose_model_class><EOS> 
test2['open_channels'] = submit['open_channels'].astype(int )