Kaggle competition: Contradictory, My Dear Watson
sub_logreg = pd.DataFrame()
sub_logreg['PassengerId'] = df_test['PassengerId']
sub_logreg['Survived'] = pred_all_logreg 
<compute_train_metric>
os.environ["WANDB_API_KEY"] = "0"
np.random.seed(0 )
gnb=GaussianNB()
gnb.fit(X_train,y_train)
 pred_gnb = gnb.predict(X_test)
 print(confusion_matrix(y_test, pred_gnb))
print(classification_report(y_test, pred_gnb))
print(accuracy_score(y_test, pred_gnb))<train_model>
train = pd.read_csv(".. /input/contradictory-my-dear-watson/train.csv")
 test = pd.read_csv(".. /input/contradictory-my-dear-watson/test.csv")
 train.head()
knn = KNeighborsClassifier(n_neighbors=20)
 knn.fit(X_train_sc,y_train_sc )<predict_on_test>
missing_values_count = train.isnull().sum()
print("Number of missing data points per column:
")
 print(missing_values_count )
pred_knn = knn.predict(X_test)
 print(confusion_matrix(y_test, pred_knn))
print(classification_report(y_test, pred_knn))
print(accuracy_score(y_test, pred_knn))<predict_on_test>
print("Number of training data rows: {}
".format(train.shape[0]))
print("Number of training data columns: {}
".format(train.shape[1]))
knn.fit(X_train_all, y_train_all)
 pred_all_knn = knn.predict(X_test_all )<create_dataframe>
train.language.unique()
train.language.value_counts()
sub_knn = pd.DataFrame()
sub_knn['PassengerId'] = df_test['PassengerId']
sub_knn['Survived'] = pred_all_knn 
<train_model>
train, validation = train_test_split(train, stratify=train.label.values,
random_state=42,
test_size=0.2, shuffle=True )
dtree = DecisionTreeClassifier()
dtree.fit(X_train,y_train )<predict_on_test>
print("Train data: {}
".format(train.shape))
print("Validation data: {}
".format(validation.shape))
pred_dtree = dtree.predict(X_test)
 print(classification_report(y_test,pred_dtree))
print(accuracy_score(y_test, pred_dtree))<predict_on_test>
model_name = 'bert-base-multilingual-cased'
tokenizer = BertTokenizer.from_pretrained(model_name)
 save_path = '.'
if not os.path.exists(save_path):
os.makedirs(save_path)
 tokenizer.save_pretrained(save_path)
 tokenizer = BertWordPieceTokenizer("vocab.txt", lowercase=False, strip_accents=False)
 tokenizer
dtree_2 = DecisionTreeClassifier(max_features=7 , max_depth=6, min_samples_split=8)
 dtree_2.fit(X_train,y_train)
 pred_dtree_2 = dtree_2.predict(X_test)
 print(classification_report(y_test, pred_dtree_2))
print(accuracy_score(y_test, pred_dtree_2))<predict_on_test>
tokenized_premise = tokenizer.encode_batch(train.premise.values.tolist())
 train['premise_seq_length'] = [len(encoding.tokens)for encoding in tokenized_premise]
tokenized_hypothesis = tokenizer.encode_batch(train.hypothesis.values.tolist())
 train['hypothesis_seq_length'] = [len(encoding.tokens)for encoding in tokenized_hypothesis]
info_per_lang = train.groupby('language' ).agg({'premise_seq_length': ['mean', 'max', 'count'], 'hypothesis_seq_length': ['mean', 'max', 'count']})
 print(info_per_lang )
dtree_2.fit(X_train_all, y_train_all)
 pred_all_dtree2 = dtree_2.predict(X_test_all )<train_model>
EPOCHS = 3
BATCH_SIZE = 64
MAX_LEN = 100
PATIENCE = 1
LEARNING_RATE = 1e-5
rfc = RandomForestClassifier(max_depth=6, max_features=7)
 rfc.fit(X_train, y_train )<predict_on_test>
def encode(df, tokenizer, max_len=50):
pairs = df[['premise','hypothesis']].values.tolist()
tokenizer.enable_truncation(max_len)
 tokenizer.enable_padding()
print("Encoding...")
 enc_list = tokenizer.encode_batch(pairs)
 print("Complete")
 input_word_ids = tf.ragged.constant([enc.ids for enc in enc_list], dtype=tf.int32)
 input_mask = tf.ragged.constant([enc.attention_mask for enc in enc_list], dtype=tf.int32)
 input_type_ids = tf.ragged.constant([enc.type_ids for enc in enc_list], dtype=tf.int32)
 inputs = {
'input_word_ids': input_word_ids.to_tensor() ,
'input_mask': input_mask.to_tensor() ,
'input_type_ids': input_type_ids.to_tensor() }
return inputs
pred_rfc = rfc.predict(X_test)
 print(confusion_matrix(y_test, pred_rfc))
print(classification_report(y_test, pred_rfc))
print(accuracy_score(y_test, pred_rfc))<predict_on_test>
train_input = encode(train, tokenizer=tokenizer, max_len=MAX_LEN )
rfc.fit(X_train_all, y_train_all)
 pred_all_rfc = rfc.predict(X_test_all )<save_to_csv>
validation_input = encode(validation, tokenizer=tokenizer, max_len=MAX_LEN )
sub_rfc = pd.DataFrame()
sub_rfc['PassengerId'] = df_test['PassengerId']
sub_rfc['Survived'] = pred_all_rfc 
<train_model>
def build_model(model_name, max_len=50):
tf.random.set_seed(1234)
 bert_encoder = TFBertModel.from_pretrained(model_name)
 input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 input_type_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_type_ids")
 embedding = bert_encoder([input_word_ids, input_mask, input_type_ids])[0]
cls_vector = embedding[:,0,:]
output = tf.keras.layers.Dense(3, activation='softmax' )(cls_vector)
 model = tf.keras.Model(inputs=[input_word_ids, input_mask, input_type_ids], outputs=output)
 model.compile(tf.keras.optimizers.Adam(lr=LEARNING_RATE), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 return model
svc = SVC(gamma = 0.01, C = 100)
 svc.fit(X_train_sc, y_train_sc )<predict_on_test>
checkpoint_filepath='bert_best_checkpoint.hdf5'
callbacks = [EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=PATIENCE), ModelCheckpoint(filepath=checkpoint_filepath, save_best_only=True, save_weights_only=True, monitor='val_accuracy', mode='max', verbose=1)]
train_history = model.fit(x=train_input, y=train.label.values, validation_data=(validation_input, validation.label.values), epochs=EPOCHS, verbose=1, batch_size=BATCH_SIZE, callbacks=callbacks )
pred_svc = svc.predict(X_test_sc)
 print(confusion_matrix(y_test_sc, pred_svc))
print(classification_report(y_test_sc, pred_svc))
print(accuracy_score(y_test_sc, pred_svc))<save_to_csv>
del model
svc.fit(X_train_all_sc, y_train_all_sc)
 pred_all_svc = svc.predict(X_test_all_sc)
 sub_svc = pd.DataFrame()
sub_svc['PassengerId'] = df_test['PassengerId']
sub_svc['Survived'] = pred_all_svc
sub_svc.to_csv('svc.csv',index=False )<import_modules>
K.clear_session()
from sklearn.model_selection import cross_val_score<compute_train_metric>
PRETRAINED_MODEL_TYPES = {
'xlmroberta':(XLMRobertaConfig, TFXLMRobertaModel, XLMRobertaTokenizer, 'jplu/tf-xlm-roberta-large')
 }
config_class, model_class, tokenizer_class, model_name = PRETRAINED_MODEL_TYPES['xlmroberta']
tokenizer = AutoTokenizer.from_pretrained(model_name)
 tokenizer
scores_svc = cross_val_score(svc, X_train_all_sc, y_train_all_sc, cv=10, scoring='accuracy')
 print(scores_svc)
 print(scores_svc.mean() )<compute_train_metric>
def encode(df, tokenizer, max_len=50):
pairs = df[['premise','hypothesis']].values.tolist()
print("Encoding...")
 encoded_dict = tokenizer.batch_encode_plus(pairs, max_length=max_len, padding=True, truncation=True,
add_special_tokens=True, return_attention_mask=True)
 print("Complete")
 input_word_ids = tf.convert_to_tensor(encoded_dict['input_ids'], dtype=tf.int32)
 input_mask = tf.convert_to_tensor(encoded_dict['attention_mask'], dtype=tf.int32)
 inputs = {
'input_word_ids': input_word_ids,
'input_mask': input_mask}
return inputs
scores_rfc = cross_val_score(rfc, X_train_all_sc, y_train_all_sc, cv=10, scoring='accuracy')
 print(scores_rfc)
 print(scores_rfc.mean() )<compute_train_metric>
train_input = encode(train, tokenizer=tokenizer, max_len=MAX_LEN )
scores_dtree_2 = cross_val_score(dtree_2, X_train_all_sc, y_train_all_sc, cv=10, scoring='accuracy')
 print(scores_dtree_2)
 print(scores_dtree_2.mean() )<compute_test_metric>
validation_input = encode(validation, tokenizer=tokenizer, max_len=MAX_LEN )
print("dtree_2 : " , scores_dtree_2.mean())
 print("rfc : " , scores_rfc.mean())
 print("svc : " , scores_svc.mean() )<import_modules>
def build_model(max_len=50):
tf.random.set_seed(1234)
 encoder = model_class.from_pretrained(model_name)
 input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 embedding = encoder([input_word_ids, input_mask])[0]
sequence_output = embedding[:,0,:]
output = tf.keras.layers.Dense(3, activation="softmax" )(sequence_output)
 model = tf.keras.Model(inputs=[input_word_ids, input_mask], outputs=output)
 model.compile(tf.keras.optimizers.Adam(lr=LEARNING_RATE), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 return model
import numpy as np
import pandas as pd
import time
from datetime import datetime
from sklearn.model_selection import train_test_split
from google.cloud import storage
from google.cloud import automl_v1beta1 as automl
from google.api_core.gapic_v1.client_info import ClientInfo
from automlwrapper import AutoMLWrapper<choose_model_class>
checkpoint_filepath='xlmroberta_best_checkpoint.hdf5'
callbacks = [EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=PATIENCE), ModelCheckpoint(filepath=checkpoint_filepath, save_best_only=True, save_weights_only=True, monitor='val_accuracy', mode='max', verbose=1)]
train_history = model.fit(x=train_input, y=train.label.values, validation_data=(validation_input, validation.label.values), epochs=EPOCHS, verbose=1, batch_size=BATCH_SIZE, callbacks=callbacks )
PROJECT_ID = 'cloudml-demo'
bucket_name = 'cloudml-demo-lcm'
region = 'us-central1'
dataset_display_name = 'kaggle_tweets'
model_display_name = 'kaggle_starter_model1'
storage_client = storage.Client(project=PROJECT_ID)
 client = automl.AutoMlClient(client_info=ClientInfo())
 print(f'Starting AutoML notebook at {datetime.fromtimestamp(time.time() ).strftime("%Y-%m-%d, %H:%M:%S UTC")}' )<load_from_csv>
def accuracy(x):
return round(float(x[2]/x[1]), 2)*100
validation['predictions'] = validation_predictions
lang_counts = validation.language.value_counts().sort_index()
tp_per_lang = validation[validation['label'] == validation['predictions']].groupby('language' ).agg({'language': ['count']} ).sort_index()
lang_names = lang_counts.index.tolist()
lang_tuples = list(zip(lang_names, lang_counts.values.tolist() , tp_per_lang.iloc[:, 0].values.tolist()))
acc = map(accuracy, lang_tuples)
 for i, score in enumerate(acc):
print("Accuracy of {} is {} ".format(lang_tuples[i][0], score))
nlp_train_df = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 nlp_test_df = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 def callback(operation_future):
result = operation_future.result()<filter>
model.load_weights(checkpoint_filepath )
nlp_train_df.loc[nlp_train_df['text'].str.contains('fire', na=False, case=False)]<count_values>
test_input = encode(test, tokenizer=tokenizer, max_len=MAX_LEN )
nlp_train_df.loc[nlp_train_df['text'].str.contains('fire', na=False, case=False)].target.value_counts()<load_pretrained>
predictions = [np.argmax(i)for i in model.predict(test_input)]
def upload_blob(bucket_name, source_file_name, destination_blob_name):

 bucket = storage_client.get_bucket(bucket_name)
 blob = bucket.blob(destination_blob_name)
 blob.upload_from_filename(source_file_name)
 print('File {} uploaded to {}'.format(
 source_file_name,
'gs://' + bucket_name + '/' + destination_blob_name))
def download_to_kaggle(bucket_name,destination_directory,file_name,prefix=None):

 os.makedirs(destination_directory, exist_ok = True)
 full_file_path = os.path.join(destination_directory, file_name)
 blobs = storage_client.list_blobs(bucket_name,prefix=prefix)
 for blob in blobs:
blob.download_to_filename(full_file_path )<prepare_output>
submission = test.id.copy().to_frame()
submission['prediction'] = predictions
submission.head()
bucket = storage.Bucket(storage_client, name=bucket_name)
 if not bucket.exists() :
bucket.create(location=region )<save_to_csv>
submission.to_csv("submission.csv", index = False )