Kaggle competition: Kannada MNIST
models = {}
model_preds = {}
id_preds3 = defaultdict(list)
 ids = df_test['ncodpers'].values
for c in product_col:
if c != 'ncodpers':
print(c)
 y_train = df_train[c]
x_train = df_train.drop(product_col + ['ncodpers',"fecha_dato"], 1)
 clf = ensemble.RandomForestClassifier(n_estimators=60, n_jobs=-1,max_depth=10, min_samples_split=10, verbose=1)
 clf.fit(x_train, y_train)
 x_train2 = df_test.drop(['ncodpers',"fecha_dato"], 1)
 p_train = clf.predict_proba(x_train2)[:,1]
p_train2 = clf.predict_proba(x_train)[:,1]
models[c] = clf
model_preds[c] = p_train
for id, p in zip(ids, p_train):
id_preds3[id].append(p)
 print(roc_auc_score(y_train, p_train2)) 
<prepare_output>
train_df = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_df = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_df = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 print(train_df.shape, test_df.shape, dig_df.shape )
fraction1 = 0.4
fraction3 = 0.6
id_preds_combined = {}
for uid, p in id_preds1.items() :
id_preds_combined[uid] = fraction1*np.asarray(id_preds1[uid])+ fraction3*np.asarray(id_preds3[uid])
 id_preds = id_preds_combined<load_from_csv>
train_df['label'].value_counts().sort_index()
df_recent = pd.read_csv('.. /input/santander-product-recommendation/train_ver2.csv',usecols=['ncodpers'] + product_col)
<load_from_csv>
dig_df['label'].value_counts().sort_index()
sample = pd.read_csv('.. /input/santander-product-recommendation/sample_submission.csv' )<define_variables>
X = train_df.drop(['label'], axis=1 ).values
Y = train_df['label']
X_test = test_df.drop(['id'], axis=1 ).values
X_dig = dig_df.drop(['label'], axis=1 ).values
Y_dig = dig_df['label']
print(X.shape, Y.shape, X_test.shape)
 print(X_dig.shape, Y_dig.shape )
already_active = {}
for row in df_recent.values:
row = list(row)
 id = row.pop(0)
 active = [c[0] for c in zip(tuple(product_col), row)if c[1] > 0]
already_active[id] = active
train_preds = {}
for id, p in id_preds.items() :
preds = [i[0] for i in sorted([i for i in zip(tuple(product_col), p)if i[0] not in already_active[id]],
key=lambda i:i [1],
reverse=True)[:7]]
train_preds[id] = preds
test_preds = []
for row in sample.values:
id = row[0]
p = train_preds[id]
test_preds.append(' '.join(p)) 
<prepare_output>
X_train, X_dev, Y_train, Y_dev = train_test_split(X, Y, test_size=0.1 )
sample['added_products'] = test_preds 
<save_to_csv>
n_x = 28
train_img = X_train.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 dev_img = X_dev.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 test_img = X_test.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 dig_img = X_dig.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 print(train_img.shape, dev_img.shape, test_img.shape, dig_img.shape)
 train_labels = to_categorical(Y_train)
 dev_labels = to_categorical(Y_dev)
 dig_labels = to_categorical(Y_dig)
 print(train_labels.shape, dev_labels.shape, dig_labels.shape)
 print(Y_dig[8], dig_labels[8])
 plt.figure(figsize=(1,1))
plt.imshow(X_dig[8].reshape(( 28,28)) ,cmap=plt.cm.binary)
 plt.show()
sample.to_csv('LR-RF_sub.csv', index=False)
 print(datetime.datetime.now() -start_time)
<set_options>
data_augment = ImageDataGenerator(rotation_range=10, zoom_range=0.1,
width_shift_range=0.1, height_shift_range=0.1 )
torch.cuda.set_device(0)
 torch.cuda.set_device(0 )<define_variables>
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=3, padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(layers.Conv2D(32, kernel_size=3, padding='same', activation='relu'))
model.add(layers.BatchNormalization(momentum=0.15))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Conv2D(32, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'))
model.add(layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'))
model.add(layers.BatchNormalization(momentum=0.15))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Conv2D(64, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'))
model.add(layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'))
model.add(layers.BatchNormalization(momentum=0.15))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Conv2D(128, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Flatten())
 model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
ls /kaggle/input/<load_from_csv>
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'] )
MASKS = 'train.csv'
SUB = 'sample_submission.csv'
TRAIN = Path('train/')
 TEST = Path('test/')
 PATH = Path('/kaggle/input/human-protein-atlas-image-classification/')
 TMP = Path('/kaggle/working/tmp/')
 MODEL = Path('/kaggle/working/model/')
 PRETRAINED = '/kaggle/input/4-channel-darknet-sz-256/model/256'
seg = pd.read_csv(PATH/MASKS ).set_index('Id')
 sample_sub = pd.read_csv(PATH/SUB ).set_index('Id')
 sample= len(seg)
 seg.head()<define_variables>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',patience=3,factor=0.5,min_lr=0.00001,
verbose=1 )
train_names_png = [TRAIN/f for f in os.listdir(PATH/TRAIN)]
train_names = list(seg.index.values)
 train_names_sample = list(seg.index.values)[0:sample]
test_names_png = [TEST/f for f in os.listdir(PATH/TEST)]
test_names = list(sample_sub.index.values)
 test_names_sample = list(sample_sub.index.values)[0:sample]
len(train_names_sample), len(test_names )<find_best_params>
epochs = 75
batch_size = 64
history = model.fit_generator(data_augment.flow(train_img, train_labels, batch_size=batch_size),
epochs=epochs, steps_per_epoch=train_img.shape[0]//batch_size,
validation_data=(dev_img, dev_labels), callbacks=[learning_rate_reduction] )
seg2 = seg.iloc[0:sample]
val_idxs = get_cv_idxs(sample )<prepare_x_and_y>
model.evaluate(dig_img, dig_labels )
indexes = seg2.Target.apply(str.split)
 y = np.zeros(( sample, 28))
for i in range(sample):
y[i,np.array(indexes[i], dtype=int)]=1<split>
pred_dig = model.predict(dig_img)
 dig_df['pred'] = np.argmax(pred_dig, axis=1 )
((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, np.array(train_names_sample), y )<create_dataframe>
dig_df['correct'] = dig_df['label'] - dig_df['pred']
errors = dig_df[dig_df['correct'] != 0]
error_list = errors.index
print('Number of errors is ', len(errors))
print('The indices are ', error_list )
def get_data(sz=128, bs=32):
datasets = ImageData.get_ds(CustomDataset,(trn_x,trn_y),(val_x,val_y), sz=sz, tfms=(None,None), path=PATH/TRAIN)
 datasets[4] = CustomDataset(test_names, test_names, None, PATH/TEST, sz)
 return ImageData(PATH, datasets, bs=bs, num_workers=4, classes=28 )<choose_model_class>
predictions = model.predict(test_img)
 print(predictions.shape )
class ConvBN(nn.Module):
"convolutional layer then batchnorm"
def __init__(self, ch_in, ch_out, kernel_size = 3, stride=1, padding=0):
super().__init__()
self.conv = nn.Conv2d(ch_in, ch_out, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)
 self.bn = nn.BatchNorm2d(ch_out, momentum=0.01)
 self.relu = nn.LeakyReLU(0.1, inplace=True)
 def forward(self, x): return self.relu(self.bn(self.conv(x)) )<choose_model_class>
predicted_labels = np.argmax(predictions, axis=1 )
<choose_model_class><EOS> 
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predicted_labels
submission.to_csv('submission.csv', index=False )