Kaggle competition: Digit Recognizer
def holiday_in_week_train(final_data):
dates =[]
for ptr in holidays.US(years = 2010 ).items() :
dates.append(ptr[0])
 for ptr in holidays.US(years = 2011 ).items() :
dates.append(ptr[0])
 for ptr in holidays.US(years = 2012 ).items() :
dates.append(ptr[0])
 holiday_count=[]
for index, row in final_data.iterrows() :
dat = final_data['Date'][index]
dt=[]
for i in range(0,5):
dt.append(dat - datetime.timedelta(days = i))
for i in range(1,3):
dt.append(dat + datetime.timedelta(days = i))
count = 0
for date in dates:
if date in dt:
count +=1
holiday_count.append(count)
 return holiday_count<define_variables>
file_path = "/kaggle/input/digit-recognizer/train.csv"
X_train = pd.read_csv(file_path)
 y_train = X_train.label
X_train = X_train.drop(columns = ["label"])
 file_path = "/kaggle/input/digit-recognizer/test.csv"
X_test = pd.read_csv(file_path)
 X_test = np.array(X_test )
def holiday_in_week_test(final_data):
dates =[]
for ptr in holidays.US(years = 2010 ).items() :
dates.append(ptr[0])
 for ptr in holidays.US(years = 2011 ).items() :
dates.append(ptr[0])
 for ptr in holidays.US(years = 2012 ).items() :
dates.append(ptr[0])
 for ptr in holidays.US(years = 2013 ).items() :
dates.append(ptr[0])
 holiday_count=[]
for index, row in final_data.iterrows() :
dat = final_data['Date'][index]
dt=[]
for i in range(0,5):
dt.append(dat - datetime.timedelta(days = i))
for i in range(1,3):
dt.append(dat + datetime.timedelta(days = i))
count = 0
for date in dates:
if date in dt:
count +=1
holiday_count.append(count)
 return holiday_count<feature_engineering>
X_train = np.array(X_train)
 X_test = np.array(X_test )
final_data_train=markdown_imputation(final_data_train)
 final_data_train=weekly_sales_imputation(final_data_train)
 final_data_train,list1=train_temp_bins(final_data_train)
 final_data_train=split(final_data_train)
 final_data_train['diff_from_christmas'] = final_data_train.apply(days_from_christmas_for_train, axis=1)
 final_data_train['days_from_thanksgiving'] = final_data_train.apply(days_from_thanksgiving_for_train, axis=1)
 final_data_train['IsHoliday_bins'] = final_data_train.apply(holiday_type, axis=1)
 final_data_train=holiday_label(final_data_train)
 final_data_train=type_label(final_data_train)
 final_data_train['Holidays'] = np.array(holiday_in_week_train(final_data_train))<feature_engineering>
X_train = X_train /255
X_test = X_test / 255
final_data_test=markdown_imputation(final_data_test)
 final_data_test=test_temp_bins(final_data_test,list1)
 final_data_test=split(final_data_test)
 final_data_test['diff_from_christmas'] = final_data_test.apply(days_from_christmas_for_test, axis=1)
 final_data_test['days_from_thanksgiving'] = final_data_test.apply(days_from_thanksgiving_for_test, axis=1)
 final_data_test['IsHoliday_bins'] = final_data_test.apply(holiday_type, axis=1)
 final_data_test=holiday_label(final_data_test)
 final_data_test=type_label(final_data_test)
 final_data_test['Holidays'] = np.array(holiday_in_week_test(final_data_test))<drop_column>
y_train = to_categorical(y_train, num_classes = 10 )
final_data_train=final_data_train.reset_index(drop=True )<data_type_conversions>
random_seed = 2
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=random_seed)
final_data_train=final_data_train[['Store','Dept','IsHoliday','Size','Week','Type','Year','Weekly_Sales','Holidays','Day']]
final_data_test=final_data_test[['Store','Dept','IsHoliday','Size','Week','Type','Year','Holidays','Day']]
final_data_train['IsHoliday']=final_data_train['IsHoliday'].astype('bool')
 final_data_test['IsHoliday']=final_data_test['IsHoliday'].astype('bool')
 final_data_train['Type']=final_data_train['Type'].astype('int')
 final_data_test['Type']=final_data_test['Type'].astype('int')
 y = final_data_train['Weekly_Sales']
X = final_data_train.drop(['Weekly_Sales'], axis=1)
 rf_Model = RandomForestRegressor(n_estimators= 140,max_depth=27,n_jobs = -1)
 rf_Model.fit(X, y)
 y_hat= rf_Model.predict(final_data_test )<define_variables>
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from keras.models import Model
sample_submission['Weekly_Sales'] = list(y_hat )<save_to_csv>
!pip install -q efficientnet
model = Sequential()
model.add(Conv2D(64, kernel_size=5, activation="relu", padding = "same", input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(256, kernel_size=3, activation="relu", padding = "same"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=3, activation="relu", padding = "same"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=3, activation="relu", padding = "same"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(256, kernel_size=3, activation="relu", padding = "same"))
model.add(Flatten())
 model.add(Dense(10, activation="softmax"))
sample_submission.to_csv('submission.csv',index = False )<set_options>
optimizer = keras.optimizers.RMSprop(learning_rate=0.001,
rho=0.9,
epsilon=1e-08,
decay=0.0 )
%%HTML
<style type="text/css">
div.h1 {
background-color:
color: white;
padding: 8px;
padding-right: 300px;
font-size: 35px;
max-width: 1500px;
margin: auto;
margin-top: 50px;
}
div.h2 {
background-color:
color: white;
padding: 8px;
padding-right: 300px;
font-size: 35px;
max-width: 1500px;
margin: auto;
margin-top: 50px;
}
</style><set_options>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
warnings.filterwarnings('ignore' )<import_modules>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
from sklearn.preprocessing import QuantileTransformer<load_from_csv>
epochs = 40
batch_size = 50
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<define_variables>
datagen = keras.preprocessing.image.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 )
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<normalization>
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] )
for col in(GENES + CELLS):
transformer = QuantileTransformer(n_quantiles=100, random_state=0, output_distribution="normal")
 vec_len = len(train_features[col].values)
 vec_len_test = len(test_features[col].values)
 raw_vec = train_features[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 train_features[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
test_features[col] = transformer.transform(test_features[col].values.reshape(vec_len_test, 1)).reshape(1, vec_len_test)[0]<set_options>
results = model.predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )<sort_values>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
train_targets_scored.sum() [1:].sort_values()<concatenate>
file_path = "/kaggle/input/digit-recognizer/train.csv"
X_train = pd.read_csv(file_path)
 y_train = X_train.label
X_train = X_train.drop(columns = ["label"])
 file_path = "/kaggle/input/digit-recognizer/test.csv"
X_test = pd.read_csv(file_path)
 X_test = np.array(X_test )
n_comp = 50
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<concatenate>
X_train = np.array(X_train)
 X_test = np.array(X_test )
n_comp = 15
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<create_dataframe>
X_train = X_train /255
X_test = X_test / 255
var_thresh = VarianceThreshold(threshold=0.5)
 data = train_features.append(test_features)
 data_transformed = var_thresh.fit_transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1)
 train_features 
<merge>
y_train = to_categorical(y_train, num_classes = 10 )
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]<drop_column>
random_seed = 2
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state=random_seed)
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )<feature_engineering>
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from keras.models import Model
<feature_engineering>
!pip install -q efficientnet
model = Sequential()
model.add(Conv2D(64, kernel_size=5, activation="relu", padding = "same", input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Conv2D(256, kernel_size=3, activation="relu", padding = "same"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=3, activation="relu", padding = "same"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(128, kernel_size=3, activation="relu", padding = "same"))
model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Conv2D(256, kernel_size=3, activation="relu", padding = "same"))
model.add(Flatten())
 model.add(Dense(10, activation="softmax"))
<create_dataframe>
optimizer = keras.optimizers.RMSprop(learning_rate=0.001,
rho=0.9,
epsilon=1e-08,
decay=0.0 )
<drop_column>
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()<split>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=5)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int)
 folds<categorify>
epochs = 40
batch_size = 50
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct 
<train_model>
datagen = keras.preprocessing.image.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 )
def train_fn(model, optimizer, scheduler, loss_fn, dataloader, device):
model.train()
final_loss = 0
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 loss.backward()
optimizer.step()
scheduler.step()
final_loss += loss.item()
final_loss /= len(dataloader)
 return final_loss
def valid_fn(model, loss_fn, dataloader, device):
model.eval()
final_loss = 0
valid_preds = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 final_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return final_loss, valid_preds
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds 
<normalization>
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] )
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dropout1 = nn.Dropout(0.2)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(0.5)
 self.dense2 = nn.utils.weight_norm(nn.Linear(hidden_size, hidden_size))
self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(0.5)
 self.dense3 = nn.utils.weight_norm(nn.Linear(hidden_size, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = self.dropout1(x)
 x = F.relu(self.dense1(x))
x = self.batch_norm2(x)
 x = self.dropout2(x)
 x = F.relu(self.dense2(x))
x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x<feature_engineering>
results = model.predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
def process_data(data):
data = pd.get_dummies(data, columns=['cp_time','cp_dose'])
 return data<define_variables>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )