Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<predict_on_test>
warnings.filterwarnings('ignore')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
X_test = test_df[['Weeks', 'FVC']]
Y_test = test_df['FVC']
Y_pred = regr.predict(X_test)
 ', Y_pred)
 ', Y_test )<train_on_grid>
raw_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 raw_test = pd.read_csv('.. /input/Kannada-MNIST/test.csv' )
X = train[['Weeks', 'InitFVC', 'InitWeeks']]
Y = train['FVC']
regr = linear_model.LinearRegression()
regr.fit(X, Y)
 print('Intercept:
', regr.intercept_)
 print('Coefficients:
', regr.coef_)
 model = sm.OLS(Y, X ).fit()
print(model.summary() )<predict_on_test>
raw_train.iloc[[0,-1],[1,-1]]
X_test = test_df[['Weeks', 'FVC', 'InitWeeks']]
Y_test = test_df['FVC']
Y_pred = regr.predict(X_test)
 ', Y_pred)
 ', Y_test )<train_on_grid>
x = raw_train.iloc[:, 1:].values.astype('float32')/ 255
y = raw_train.iloc[:, 0]
X = train[['Weeks', 'InitFVC','InitWeeks']]
Y = train['FVC']
regr = linear_model.LinearRegression()
regr.fit(X, Y)
 print('Intercept:
', regr.intercept_)
 print('Coefficients:
', regr.coef_)
 model = sm.OLS(Y, X ).fit()
print(model.summary() )<train_on_grid>
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size = 0.2, random_state=42 )
X = train[['Weeks', 'InitFVC', 'InitWeeks', 'Age']]
Y = train['FVC']
regr = linear_model.LinearRegression()
regr.fit(X, Y)
 print('Intercept:
', regr.intercept_)
 print('Coefficients:
', regr.coef_)
 model = sm.OLS(Y, X ).fit()
print(model.summary() )<predict_on_test>
x_train = x_train.reshape(-1, 28, 28,1)
 x_val = x_val.reshape(-1, 28, 28,1)
 y_train = to_categorical(y_train)
 y_val = to_categorical(y_val )
X_test = test_df[['Weeks', 'FVC', 'InitWeeks', 'Age']]
Y_test = test_df['FVC']
Y_pred = regr.predict(X_test)
 ', Y_pred)
 ', Y_test )<train_on_grid>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), padding='same', input_shape=(28, 28, 1)) ,
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(64,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(64,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(128,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(256,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Conv2D(256,(3,3), padding='same'),
tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(10, activation='softmax')
 ] )
X = train_df[['Weeks', 'InitFVC', 'InitWeeks', 'Age', 'SmokingStatus_Currently smokes']]
Y = train_df['FVC']
regr = linear_model.LinearRegression()
regr.fit(X, Y)
 print('Intercept:
', regr.intercept_)
 print('Coefficients:
', regr.coef_)
 model = sm.OLS(Y, X ).fit()
print(model.summary() )<prepare_output>
optimizer = RMSprop(learning_rate=0.0015,
rho=0.9,
momentum=0.1,
epsilon=1e-07,
centered=True,
name='RMSprop')
 model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'] )
data = []
for i in range(test_df.shape[0]):
new_cols = ['Patient_Week', 'FVC', 'Confidence']
new_vals = [test_df.iloc[i]['Patient']+"_"+str(test_df.iloc[i]['Weeks']), Y_pred[i], 244]
data.append(dict(zip(new_cols, new_vals)))
 submission = pd.DataFrame(data)
 submission.head(95)
<save_to_csv>
batch_size = 1024
num_classes = 10
epochs = 60
submission.to_csv('submission.csv', index=False )<save_to_csv>
datagen_try = ImageDataGenerator(rotation_range=15,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 25,
zoom_range = 0.4,)
 datagen_try.fit(x_train)
 for x_batch, y_batch in datagen_try.flow(x_train, y_train, batch_size=9):
	
	for i in range(0, 9):
		plt.subplot(330 + 1 + i)
 		plt.imshow(x_batch[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
	
	plt.show()
	break
submission.to_csv('submission.csv', index=False )<import_modules>
datagen_train = ImageDataGenerator(rotation_range = 10,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 10,
zoom_range = 0.1,
horizontal_flip = False)
 datagen_val = ImageDataGenerator()
step_train = x_train.shape[0] // batch_size
step_val = x_val.shape[0] // batch_size
learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='loss',
factor=0.25,
patience=2,
verbose=1,
mode="auto",
min_delta=0.0001,
cooldown=0,
min_lr=0.00001 
)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=300, restore_best_weights=True)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from pandas import DataFrame
import numpy as np
import json<set_options>
history = model.fit_generator(datagen_train.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=len(x_train)//batch_size,
epochs=epochs,
validation_data=(x_val, y_val),
validation_steps=50,
callbacks=[learning_rate_reduction, es],
verbose=2 )
!cp -r cased_L-12_H-768_A-12/* .<categorify>
tf.keras.utils.plot_model(model, to_file="model.png", show_shapes=True )
def preprocess(data):

 punct = "/-'?!.,
def clean_special_chars(text, punct):
for p in punct:
text = text.replace(p, '')
 return text
data = data.astype(str ).apply(lambda x: clean_special_chars(x, punct))
return data<load_from_csv>
model.evaluate(x_val, y_val, verbose=2);
train = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv')
 test = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv')
 train.comment_text = preprocess(train['comment_text'])
 test.comment_text = preprocess(test['comment_text'])
 df_bert = pd.DataFrame({'id':train['id'],
'target':np.where(train.target >=.8,1,0),
'alpha':['a']*train.shape[0],
'comment_text':train["comment_text"].replace(r'
',' ',regex=True)})
 df_0 = df_bert[df_bert['target'] == 0].sample(1000, random_state = 101)
 df_1 = df_bert[df_bert['target'] == 1].sample(1000, random_state = 101)
 df_calibrated = pd.concat([df_0, df_1], axis=0 ).reset_index(drop=True)
 df_calibrated = shuffle(df_calibrated)
 df_bert_test = pd.DataFrame({'User_ID':test['id'],
'text':test['comment_text'].replace(r'
',' ',regex=True)})
 del(train)
 df_calibrated.to_csv('data/train.tsv', sep='\t', index=False, header=False)
 df_bert_test.to_csv('data/test.tsv', sep='\t', index=False, header=True )<load_pretrained>
y_predicted = model.predict(x_val)
 y_grand_truth = y_val
y_predicted = np.argmax(y_predicted,axis=1)
 y_grand_truth = np.argmax(y_grand_truth,axis=1)
 cm = confusion_matrix(y_grand_truth, y_predicted )
!python.. /input/bertclassifier/repository/google-research-bert-0fce551/run_classifier.py \
--task_name=cola --do_train=True --do_predict=True \
--data_dir='data' \
--vocab_file='vocab.txt' \
--bert_config_file='bert_config.json' \
--init_checkpoint='bert_model.ckpt' \
--max_seq_length=220 --train_batch_size=8 --learning_rate=2e-5 --num_train_epochs=3.0 \
--output_dir='bertoutput' \
--do_lower_case=False<save_to_csv>
scores = np.zeros(( 10,3))
def calc_F1(num):
TP = cm[num,num]
FN = np.sum(cm[num,:])-cm[num,num]
FP = np.sum(cm[:,num])-cm[num,num]
precision = TP/(TP+FP)
 recall = TP/(TP+FN)
 F1_score = 2*(recall * precision)/(recall + precision)
 return precision, recall, F1_score
for i in range(10):
precision, recall, F1_score = calc_F1(i)
 scores[i,:] = precision, recall, F1_score
scores_frame = pd.DataFrame(scores,columns=["Precision", "Recall", "F1 Score"], index=[list(range(0, 10)) ] )
result = pd.read_csv("bertoutput/test_results.tsv",sep="\t",header=None)
 preds_id = test['id'].values
preds = result.iloc[:,1].values
submission_file = pd.DataFrame({'id':preds_id,
'prediction':preds})
 submission_file.to_csv('submission.csv',sep=",",index=None )<load_from_csv>
raw_dig = pd.read_csv(".. /input/Kannada-MNIST/Dig-MNIST.csv")
 raw_dig.head()
x_dig = raw_dig.iloc[:, 1:].values.astype('float32')/ 255
y_dig = raw_dig.iloc[:, 0].values
x_dig = x_dig.reshape(-1,28,28,1)
 y_dig = to_categorical(y_dig)
 model.evaluate(x_dig, y_dig, verbose=2 )
sub = pd.read_csv('submission.csv' )<import_modules>
sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 raw_test_id=raw_test.id
raw_test=raw_test.drop("id",axis="columns")
 raw_test=raw_test / 255
test=raw_test.values.reshape(-1,28,28,1)
 test.shape
import numpy as np
import pandas as pd
from keras.models import Model
from keras.layers import Input, Dense, Embedding, SpatialDropout1D, Dropout, add, concatenate
from keras.layers import CuDNNLSTM, Bidirectional, GlobalMaxPooling1D, GlobalAveragePooling1D
from keras.preprocessing import text, sequence
from keras.callbacks import LearningRateScheduler<define_variables>
sub=model.predict(test)
 sub=np.argmax(sub,axis=1)
 sample_sub['label']=sub
sample_sub.to_csv('submission.csv',index=False )