Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<install_modules>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
!pip install --upgrade xgboost
xgb.__version__<init_hyperparams>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
xgb_params= {
"objective": "reg:squarederror",
"max_depth": 6,
"learning_rate": 0.01,
"colsample_bytree": 0.4,
"subsample": 0.6,
"reg_alpha" : 6,
"min_child_weight": 100,
"n_jobs": 2,
"seed": 2001,
'tree_method': "gpu_hist",
"gpu_id": 0,
}<define_variables>
X_train = X_train / 255.0
test = test / 255.0
train_oof = np.zeros(( 300000,))
test_preds = 0
train_oof.shape<prepare_x_and_y>
Y_train = to_categorical(Y_train, num_classes = 10 )
Test = xgb.DMatrix(Test[columns] )<train_model>
random_seed = 2
NUM_FOLDS = 10
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 for f,(train_ind, val_ind)in tqdm(enumerate(kf.split(train, target))):
train_df, val_df = train.iloc[train_ind][columns], train.iloc[val_ind][columns]
train_target, val_target = target[train_ind], target[val_ind]
train_df = xgb.DMatrix(train_df, label=train_target)
 val_df = xgb.DMatrix(val_df, label=val_target)
 model = xgb.train(xgb_params, train_df, 3600)
 temp_oof = model.predict(val_df)
 temp_test = model.predict(Test)
 train_oof[val_ind] = temp_oof
test_preds += temp_test/NUM_FOLDS
print(mean_squared_error(temp_oof, val_target, squared=False))<compute_test_metric>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
mean_squared_error(train_oof, target, squared=False )<save_model>
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(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"))
np.save('train_oof', train_oof)
 np.save('test_preds', test_preds )<predict_on_test>
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )
%%time
shap_preds = model.predict(test, pred_contribs=True )<load_from_csv>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
train = pd.read_csv('.. /input/tabular-playground-series-feb-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-feb-2021/test.csv')
 for feature in cat_features:
le = LabelEncoder()
le.fit(train[feature])
 train[feature] = le.transform(train[feature])
 test[feature] = le.transform(test[feature] )<save_to_csv>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
sample_sub['target'] = test_preds
sample_sub.to_csv('submission.csv', index=False )<install_modules>
epochs = 30
batch_size = 86
!pip install librosa<import_modules>
import numpy as np
import pandas as pd
import os<load_pretrained>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
audio_data = '/kaggle/input/birdsong-recognition/train_audio/nutwoo/XC462016.mp3'
x , sr = librosa.load(audio_data)
 print(type(x), type(sr))
print(x.shape, sr )<load_pretrained>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
librosa.load(audio_data, sr=44100 )<normalization>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )
<set_options><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )