Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<normalization>
warnings.filterwarnings('ignore')
 os.environ["WANDB_API_KEY"] = "0"
testforfilna =preprocessing.scale(testforfilna )<feature_engineering>
def Init_TPU() :
try:
resolver = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(resolver)
 tf.tpu.experimental.initialize_tpu_system(resolver)
 strategy = tf.distribute.experimental.TPUStrategy(resolver)
 REPLICAS = strategy.num_replicas_in_sync
print("Connected to TPU Successfully:
TPUs Initialised with Replicas:",REPLICAS)
 return strategy
except ValueError:
print("Connection to TPU Falied")
 print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
return strategy
strategy=Init_TPU()
data["FamSize"] = data.SibSp +data.Parch
data['Alone'] = data.FamSize == 0
data.Alone
data_test["FamSize"] = data_test.SibSp +data_test.Parch
data_test['Alone'] = data_test.FamSize == 0<drop_column>
path = '.. /input/contradictory-my-dear-watson/'
data.drop("AgeGroup", axis = 1, inplace =True)
 data_test.drop("AgeGroup", axis = 1, inplace =True )<count_missing_values>
train_url = os.path.join(path,'train.csv')
 train_data = pd.read_csv(train_url, header='infer')
 sample_sub_url = os.path.join(path,'sample_submission.csv')
 sample_sub = pd.read_csv(sample_sub_url, header='infer')
 test_url = os.path.join(path,'test.csv')
 test_data = pd.read_csv(test_url, header='infer' )
data.isna().sum()<count_missing_values>
print("Records per Label:
", train_data.groupby('label' ).size() )
data_test.isna().sum()<find_best_model_class>
print("Records per Language:
", train_data.groupby('language' ).size() )
lr = LogisticRegression(C=2)
 lr.fit(data.drop('Survived', axis=1), data.Survived)
 kf = KFold(n_splits=10,shuffle=True,random_state=42)
 scores = cross_val_score(lr,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores.mean() 
<drop_column>
gc.collect()
data_test.drop(['Status_Col', 'Status_Dona', 'Status_Dr'], axis= 1, inplace=True )<train_on_grid>
transformer_model = 'jplu/tf-xlm-roberta-large'
tokenizer = XLMRobertaTokenizer.from_pretrained(transformer_model )
KNN = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=7, p=2,
weights='uniform')
 KNN.fit(data.drop('Survived', axis=1), data.Survived)
 scoresKNN = cross_val_score(KNN,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scoresKNN.mean() 
<train_on_grid>
tokenizer.convert_tokens_to_ids(list(tokenizer.tokenize("Elementary, My Dear Watson!")) )
tr = DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=5,
max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort=False, random_state=None,
splitter='best')
 tr.fit(data.drop('Survived', axis=1), data.Survived)
 scorestr = cross_val_score(tr,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scorestr.mean()<train_on_grid>
train = train_data[['premise','hypothesis']].values.tolist()
test = test_data[['premise','hypothesis']].values.tolist()
rf = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=5, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=20, n_jobs=None,
oob_score=False, random_state=None, verbose=0,
warm_start=False)
 rf.fit(data.drop('Survived', axis=1), data.Survived)
 scoresrf = cross_val_score(rf,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scoresrf.mean()<train_on_grid>
max_len = 80
train_encode = tokenizer.batch_encode_plus(train, pad_to_max_length=True, max_length=max_len)
 test_encode = tokenizer.batch_encode_plus(test, pad_to_max_length=True, max_length=max_len )
gr = GradientBoostingClassifier(learning_rate=0.1, n_estimators=250,max_depth=10)
 gr.fit(data.drop('Survived', axis=1), data.Survived)
 scores_gr = cross_val_score(tr,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores_gr.mean()<train_on_grid>
test_size = 0.1
x_train, x_val, y_train, y_val = train_test_split(train_encode['input_ids'], train_data.label.values, test_size=test_size)
 x_test = test_encode['input_ids']
mlpc = MLPClassifier(max_iter=5000)
 mlpc.fit(data.drop('Survived', axis=1), data.Survived)
 scores_mlpc = cross_val_score(mlpc,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores_mlpc.mean()<drop_column>
gc.collect()
data.drop(['Status_OTHER'], inplace=True, axis=1)
 data_test.drop(['Status_Rev'], inplace=True, axis=1 )<train_on_grid>
AUTO = tf.data.experimental.AUTOTUNE
batch_size = 16 * strategy.num_replicas_in_sync
train_ds =(tf.data.Dataset.from_tensor_slices(( x_train, y_train)).repeat().shuffle(2048 ).batch(batch_size ).prefetch(AUTO))
val_ds =(tf.data.Dataset.from_tensor_slices(( x_val, y_val)).batch(batch_size ).prefetch(AUTO))
test_ds =(tf.data.Dataset.from_tensor_slices(x_test ).batch(batch_size))
XGBC = XGBClassifier(n_estimators=1000, learning_rate=0.05, max_depth=3)
 XGBC.fit(data.drop('Survived', axis=1), data.Survived);
XGBC_scores = cross_val_score(XGBC,data.drop(['Survived'], axis=1), data.Survived, cv = kf);
XGBC_scores.mean()<save_to_csv>
gc.collect()
preds = XGBC.predict(data_test)
 submission = pd.DataFrame({
"PassengerId": ID,
"Survived": preds
})
 submission.to_csv('titanic.csv', index=False )<import_modules>
def build_model(strategy,transformer):
with strategy.scope() :
transformer_encoder = TFXLMRobertaModel.from_pretrained(transformer)
 input_layer = Input(shape=(max_len,), dtype=tf.int32, name="input_layer")
 sequence_output = transformer_encoder(input_layer)[0]
cls_token = sequence_output[:, 0, :]
output_layer = Dense(3, activation='softmax' )(cls_token)
 model = Model(inputs=input_layer, outputs=output_layer)
 model.compile(
 Adam(lr=1e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] 
)
 return model
model = build_model(strategy,transformer_model )
from sklearn.model_selection import GridSearchCV<import_modules>
epochs = 30
n_steps = len(train_data)// batch_size
model.fit(train_ds,
steps_per_epoch = n_steps,
validation_data = val_ds,
epochs = epochs )
from sklearn.model_selection import GridSearchCV<train_on_grid>
gc.collect()
lgbm = LGBMClassifier(n_estimators=1000, random_state=5)
 lgbm.fit(data.drop('Survived', axis=1), data.Survived)
 scores_lgbm = cross_val_score(lgbm,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores_lgbm.mean()<train_on_grid>
prediction = model.predict(test_ds, verbose=0)
 sample_sub['prediction'] = prediction.argmax(axis=1 )
<find_best_params><EOS> 
sample_sub.to_csv("submission.csv", index=False )