Kaggle competition: Kannada MNIST
parameter = {'solver':['liblinear','lbfgs'],
'max_iter':[200,400]}
Logis_clf = LogisticRegression()
lreg = GridSearchCV(Logis_clf, param_grid = parameter, cv = 3, verbose=True, n_jobs=-1)
 lreg.fit(x_train, y_train)
 lreg_best = lreg.best_estimator_
print(lreg.best_score_)
 print(lreg.best_params_ )<concatenate>
print(tf.__version__ )
S_train, S_test = stacking(classifiers,
x_train, y_train, x_test,
regression= False,
mode='oof_pred_bag',
needs_proba=True,
save_dir=None,
n_folds=5,
stratified=True,
shuffle=True,
random_state=0,
verbose=2 )<define_variables>
train=pd.read_csv(".. /input/Kannada-MNIST/train.csv")
 test=pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 print("Train Shape: {}".format(train.shape))
print("Test Shape: {}".format(test.shape))
argmax_train = []
argmax_test = []
for i in range(0,S_train.shape[1],2):
argmax_train.append(np.argmax(S_train[:,i:i+2],axis=1))
argmax_test.append(np.argmax(S_test[:,i:i+2],axis=1))<prepare_x_and_y>
print("Train Nulls: {}".format(train.isna().any().sum()))
print("Test Nulls: {}".format(test.isna().any().sum()))
argmax_train = np.array(argmax_train,dtype= np.int64 ).T
argmax_test = np.array(argmax_test,dtype= np.int64 ).T 
<predict_on_test>
y = train['label'].astype('int8')
 X = train.drop(columns=['label'] ).astype('float16' ).values
X = X.reshape(len(train), 28,28, 1 )
modelc = LogisticRegression()
model1c = modelc.fit(S_train, y_train)
 y_pred1c = model1c.predict_proba(S_train)
 y_predc = model1c.predict_proba(S_test)
 print('Final test prediction score: [%.8f]' % accuracy_score(y_train, np.argmax(y_pred1,axis=1)))
 print('Final f1-score test prediction: [%.8f]' % f1_score(y_train, np.argmax(y_pred1,axis=1)))
<train_model>
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size=0.2 )
model = XGBClassifier(random_state=2, objective = 'reg:linear', n_jobs=-1, learning_rate= 0.5,
n_estimators=30, max_depth=20)
 model1 = model.fit(argmax_train, y_train)
 y_pred1 = model1.predict_proba(argmax_train)
 y_pred = model1.predict_proba(argmax_test)
 print('Final test prediction score: [%.8f]' % accuracy_score(y_train, np.argmax(y_pred1,axis=1)))
 print('Final f1-score test prediction: [%.8f]' % f1_score(y_train, np.argmax(y_pred1,axis=1)))
<save_to_csv>
train_image_generator = ImageDataGenerator(
 rescale=1./255 
)
 validation_image_generator = ImageDataGenerator(
 rescale=1./255 
)
 train_image_gen = train_image_generator.flow(
 x=X_train,
y=y_train, 
)
 validation_image_gen = validation_image_generator.flow(
 x=X_validation,
y=y_validation,
)
sample_submission['target'] = np.argmax(y_pred+y_predc,axis=1)
 sample_submission.to_csv('submission_with_stacking.csv', index = False )<load_from_url>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3,3), activation='relu',padding='same', input_shape=(28, 28, 1)) ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(128,(3,3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(( 2,2)) ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Conv2D(64,(3,3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(128,(3,3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(( 2,2)) ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1024, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(512, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(10, activation='softmax')
 ] )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
tf.keras.backend.clear_session()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(
 train_image_gen,
epochs=5,
validation_data=validation_image_gen
)
!pip install inflect 
<define_variables>
validation_predict = model.predict(X_validation)
 low_predictions = pd.DataFrame()
low_predictions['label'] = np.argmax(validation_predict, axis=1)
 low_predictions['Confidence'] = np.max(validation_predict, axis=1)
 low_index = low_predictions.sort_values(by=['Confidence'])[:10].index
low_labels = low_predictions.sort_values(by=['Confidence'])['label']
fig, axs = plt.subplots(2, 5, figsize=(24,9.5))
for i, low_label, ax in zip(low_index,low_labels, axs.flat):
image = X_validation[i].astype('float32' ).reshape(28, 28)
 ax.imshow(image, cmap='gray')
 ax.set_xlabel("True: {}".format(y_validation.iloc[i]))
ax.set_title("Guessed:{} ".format(low_label))
b_add_keyword = True
cleaning = []
max_len = 160<load_from_csv>
train_image_generator = ImageDataGenerator(
 rescale=1./255,
rotation_range=25,
width_shift_range=.1,
height_shift_range=.1,
zoom_range=0.1 
)
 validation_image_generator = ImageDataGenerator(
 rescale=1./255,
rotation_range=25,
width_shift_range=.1,
height_shift_range=.1,
zoom_range=0.1 
)
 train_image_gen = train_image_generator.flow(
 x=X_train,
y=y_train, 
)
 validation_image_gen = validation_image_generator.flow(
 x=X_validation,
y=y_validation, 
)
 lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay(
 initial_learning_rate=0.001,
decay_steps=250,
decay_rate=1,
staircase=False 
)
 def get_optimizer() :
return tf.keras.optimizers.Adam(lr_schedule)
 tf.keras.backend.clear_session()
model.compile(optimizer=get_optimizer() ,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(
 train_image_gen,
epochs=100,
validation_data=validation_image_gen
)
train_df = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 test_df = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 print('n train/test: {}/{}'.format(train_df.shape[0], test_df.shape[0]))<count_values>
X_test = test.drop(columns=['id'] ).astype('float16' ).values
X_test = X_test / 255.0
X_test = X_test.reshape(len(X_test), 28, 28, 1)
 label_pred = model.predict_classes(X_test, verbose=0)
 submission = pd.DataFrame()
submission['label'] = label_pred
submission['id'] = submission.index
submission.to_csv('.. /working/submission.csv', index=False )