Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
%matplotlib inline
pred = aml.predict(h2o_valid)
 pred.head()<save_model>
data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 print(data.shape )
h2o.save_model(aml.leader, path="./model_bin" )<train_model>
test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print(test_data.shape )
params = {'objective': 'reg:linear',
'eta': 0.01,
'max_depth': 6,
'min_child_weight': 3,
'subsample': 0.8,
'colsample_bytree': 0.8,
'colsample_bylevel': 0.50,
'gamma': 1.45,
'eval_metric': 'rmse',
'seed': 12,
'silent': True}
xgb_data = [(xgb.DMatrix(X_train, y_train), 'train'),(xgb.DMatrix(X_valid, y_valid), 'valid')]
print('Starting training...')
 xgb_model = xgb.train(params,
xgb.DMatrix(X_train, y_train),
10000,
xgb_data,
verbose_eval=300,
early_stopping_rounds=300 )<predict_on_test>
sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 print(sample_submission.shape )
xgb_pred = xgb_model.predict(xgb.DMatrix(X_valid))<drop_column>
encoder = OneHotEncoder(sparse=False,categories='auto')
 yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]
encoder.fit(yy)
 train_label = train_label.reshape(-1,1)
 val_label = val_label.reshape(-1,1)
 train_label = encoder.transform(train_label)
 val_label = encoder.transform(val_label)
 print('train_label shape: %s'%str(train_label.shape))
print('val_label shape: %s'%str(val_label.shape))
X_test = test.drop('revenue',axis=1 )<prepare_x_and_y>
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras.layers.normalization import BatchNormalization
from keras.layers import LeakyReLU
X_test[X_test==np.inf]=np.nan
X_test.fillna(X_test.mean() , inplace=True )<predict_on_test>
model = Sequential()
model.add(Conv2D(32,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(32,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(64,(3, 3), activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(128, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(Conv2D(256, kernel_size=5, activation='relu',padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu', name='my_dense'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
test_pred_xgb = xgb_model.predict(xgb.DMatrix(( X_test)) , ntree_limit=xgb_model.best_ntree_limit )<choose_model_class>
datagen = ImageDataGenerator(
 rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range = 15,
horizontal_flip = False,
zoom_range = 0.20 )
model = CatBoostRegressor(iterations=100000,
learning_rate=0.005,
depth=5,
eval_metric='RMSE',
colsample_bylevel=0.8,
random_seed = 21,
bagging_temperature = 0.2,
metric_period = None,
early_stopping_rounds=200 
)
 model.fit(X_train, y_train,eval_set=(X_valid, y_valid),use_best_model=True,verbose=500)
 val_pred = model.predict(X_valid)
 print('RMSE',np.sqrt(mean_squared_error(val_pred,y_valid)))
 test_pred_cat = model.predict(X_test )<init_hyperparams>
model.compile(loss='categorical_crossentropy',optimizer=Adam() ,metrics=['accuracy'])
 datagen.fit(train_image)
 history = model.fit_generator(datagen.flow(train_image,train_label, batch_size=32),
epochs = 75,
shuffle=True,
validation_data =(val_image,val_label),
verbose = 1,
steps_per_epoch=train_image.shape[0] // 32 )
params = {'objective':'regression',
'num_leaves' : 30,
'min_data_in_leaf' : 20,
'max_depth' : 9,
'learning_rate': 0.004,
'feature_fraction':0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
'lambda_l1': 0.2,
"bagging_seed": 11,
"metric": 'rmse',
"random_state" : 11,
"verbosity": -1}
record = dict()
model = lgb.train(params
, lgb.Dataset(X_train, y_train)
 , num_boost_round = 100000
, valid_sets = [lgb.Dataset(X_valid, y_valid)]
, verbose_eval = 500
, early_stopping_rounds = 500
, callbacks = [lgb.record_evaluation(record)] 
)
 best_idx = np.argmin(np.array(record['valid_0']['rmse']))
val_pred = model.predict(X_valid, num_iteration = model.best_iteration)
 test_pred_gbm = model.predict(X_test, num_iteration = model.best_iteration )<save_to_csv>
intermediate_output = intermediate_layer_model.predict(train_image)
 intermediate_output = pd.DataFrame(data=intermediate_output )
sub = pd.read_csv('.. /input/tmdb-box-office-prediction/sample_submission.csv')
 df_sub = pd.DataFrame()
df_sub['id'] = sub['id']
final_pred = 0.3*test_pred_xgb + 0.7*test_pred_cat
df_sub['revenue'] = np.expm1(final_pred)
 print(df_sub['revenue'])
 df_sub.to_csv("submission.csv", index=False )<import_modules>
val_data = intermediate_output[40000:]
confusion_matrix)
<define_variables>
submission_cnn = model.predict(test_image )
PRETRAINED_MODEL_NAME = 'bert-base-uncased'
LABELS_NUMBER = 2
MAX_LENGHT = 512
BATCH_SIZE = 6
LEARNING_RATE = 2e-5
EPOCHS_NUMBER = 1
N_PREDICTIONS_TO_SHOW = 10<load_from_csv>
intermediate_test_output = intermediate_layer_model.predict(test_image)
 intermediate_test_output = pd.DataFrame(data=intermediate_test_output )
train_data = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 print(train_data.shape)
 train_data.head(3 )<load_from_csv>
xgbmodel = XGBClassifier(objective='multi:softprob',
num_class= 10)
 xgbmodel.fit(intermediate_output, train_label1)
 xgbmodel.score(val_data, val_label1 )
test_data = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 print(test_data.shape)
 test_data.head(3 )<load_pretrained>
submission_xgb = xgbmodel.predict(intermediate_test_output )
tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME,
do_lower_case=True )<string_transform>
submission_cnn = submission_cnn.astype(int)
 submission_xgb = submission_xgb.astype(int)
vocabulary = tokenizer.get_vocab()
print(f'Size of the vocabulary: {len(vocabulary)}')
 print(f'Some tokens of the vocabulary: {list(vocabulary.keys())[5000:5010]}' )<categorify>
submission_cnn
label = np.argmax(submission_cnn,1)
 id_ = np.arange(0,label.shape[0])
 label
def prepare_sequence(text):

 prepared_sequence = tokenizer.encode_plus(
 text,
add_special_tokens = True,
max_length = MAX_LENGHT,
padding = 'max_length',
return_attention_mask = True 
)
 return prepared_sequence<categorify>
final_sub = submission_xgb
test_sentence = 'Is this jacksonville?'
test_sentence_encoded = prepare_sequence(test_sentence)
 token_ids = test_sentence_encoded["input_ids"]
print(f'Test sentence: {test_sentence}')
 print(f'Keys: {test_sentence_encoded.keys() }')
 print(f'Tokens: {tokenizer.convert_ids_to_tokens(token_ids)[:12]}')
 print(f'Token IDs: {token_ids[:12]}')
 print(f'Segment IDs: {test_sentence_encoded["token_type_ids"][:12]}')
 print(f'Mask IDs {test_sentence_encoded["attention_mask"][:12]}')
 print(f'Input dimension: {len(token_ids)}' )<categorify>
save = pd.DataFrame({'ImageId':sample_submission.ImageId,'label':final_sub})
 print(save.head(10))
save.to_csv('submission.csv',index=False )