Kaggle competition: Contradictory, My Dear Watson
test['Ticket1']=test['Ticket1'].replace(['A','W', 'F', 'L'], 'OtherTicket' )<count_values>
train=pd.read_csv("/kaggle/input/contradictory-my-dear-watson/train.csv")
 print("Shape of Train Data ",train.shape)
 test=pd.read_csv("/kaggle/input/contradictory-my-dear-watson/test.csv")
 print("Shape of Test Data ",test.shape )
test["Ticket1"].value_counts()<categorify>
!pip install nlp
dumytitletr1=pd.get_dummies(train['Ticket1'])
 train=pd.concat([train,dumytitletr1], axis=1 )<categorify>
snli= nlp.load_dataset("snli" )
dumytitletes1=pd.get_dummies(test['Ticket1'])
 test=pd.concat([test,dumytitletes1], axis=1 )<categorify>
snli_train_df = pd.DataFrame(snli['train'])
 snli_train_df.head()
Sex_mapping={"male":0,"female":1}
train["Sex"]=train["Sex"].map(Sex_mapping)
 test["Sex"]=test["Sex"].map(Sex_mapping )<drop_column>
test_dup=pd.merge(test[['premise','hypothesis']],snli_train_df[['premise','hypothesis']],how="outer",indicator=True)
 test_dup.head()
test=test.drop(['Ticket', 'Ticket1', 'Cabin', 'Title'], axis=1 )<drop_column>
xnli=nlp.load_dataset("xnli")
 xnli
train=train.drop(['Ticket', 'Ticket1', 'Cabin', 'Title'], axis=1 )<split>
buffer = {
'premise': [],
'hypothesis': [],
'label': [],
'lang_abv': []
}
for x in xnli['validation']:
label = x['label']
for idx, lang in enumerate(x['hypothesis']['language']):
hypothesis = x['hypothesis']['translation'][idx]
premise = x['premise'][lang]
buffer['premise'].append(premise)
 buffer['hypothesis'].append(hypothesis)
 buffer['label'].append(label)
 buffer['lang_abv'].append(lang)
 xnli_valid_df = pd.DataFrame(buffer)
 xnli_valid_df = xnli_valid_df[['premise', 'hypothesis', 'label', 'lang_abv']]
X = train.drop(['Survived', 'PassengerId'], axis=1)
 y = train["Survived"]
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 21 )<train_model>
test_dup=pd.merge(test[['premise','hypothesis']],xnli_valid_df[['premise','hypothesis']],how="outer",indicator=True)
 test_dup.head()
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_test)
 acc_logreg = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_logreg )<train_model>
test_dup['_merge'].value_counts()
logmodel = LogisticRegression(random_state= 21, solver='lbfgs', max_iter=1000)
 logmodel.fit(x_train, y_train )<compute_test_metric>
dup_pairs=test_dup[test_dup['_merge']=="both"]
dup_pairs.head()
logmodel.score(x_train, y_train )<set_options>
dup_pairs['combo']=dup_pairs['premise']+" "+dup_pairs['hypothesis']
dup_pairs.head()
logmodel.intercept_<predict_on_test>
xnli_valid_df['combo']=xnli_valid_df['premise']+" "+xnli_valid_df['hypothesis']
y_pred = logmodel.predict(x_test )<import_modules>
xnli_valid_without_dups=xnli_valid_df[(~xnli_valid_df['combo'].isin(dup_pairs['combo'].tolist())) ]
from sklearn.metrics import classification_report<compute_test_metric>
xnli_valid_without_dups.drop(['combo'],axis=1,inplace=True )
print(classification_report(y_test,y_pred))<compute_test_metric>
xnli_valid_without_dups['dataset']="xnli"
logmodel.score(x_train, y_train )<drop_column>
train_df=pd.concat([train_df,xnli_valid_without_dups])
 train_df.shape
X_test = test.drop(['PassengerId'], axis=1 )<predict_on_test>
mnli=nlp.load_dataset(path='glue', name='mnli')
 mnli
y_test_pred = logmodel.predict(X_test )<save_to_csv>
mnli_train=pd.DataFrame(mnli['train'])
 mnli_train.head()
output = pd.DataFrame({ 'PassengerId' : test['PassengerId'], 'Survived': y_test_pred })
 output.to_csv('submission.csv', index=False )<import_modules>
test_dup=pd.merge(test[['premise','hypothesis']],mnli_train[['premise','hypothesis']],how="outer",indicator=True)
 test_dup['_merge'].unique()
RANDOM_SEED = 42
CV_FOLDS = 12<define_variables>
mnli_train.drop(['idx'],axis=1,inplace=True )
data_types = {'MSZoning': 'category',
'Street': 'category',
'Alley': 'category',
'LotShape': 'category',
'LandContour': 'category',
'Utilities': 'category',
'LotConfig': 'category',
'LandSlope': 'category',
'Neighborhood': 'category',
'Condition1': 'category',
'Condition2': 'category',
'BldgType': 'category',
'HouseStyle': 'category',
'RoofStyle': 'category',
'RoofMatl': 'category',
'Exterior1st': 'category',
'Exterior2nd': 'category',
'MasVnrType': 'category',
'ExterQual': 'category',
'ExterCond': 'category',
'Foundation': 'category',
'BsmtQual': 'category',
'BsmtCond': 'category',
'BsmtExposure': 'category',
'BsmtFinType1': 'category',
'BsmtFinType2': 'category',
'ExterQual': 'category',
'Heating': 'category',
'HeatingQC': 'category',
'CentralAir': 'category',
'Electrical': 'category',
'KitchenQual': 'category',
'Functional': 'category',
'FireplaceQu': 'category',
'GarageType': 'category',
'GarageFinish': 'category',
'GarageQual': 'category',
'GarageCond': 'category',
'PavedDrive': 'category',
'PoolQC': 'category',
'Fence': 'category',
'MiscFeature': 'category',
'SaleType': 'category',
'SaleCondition': 'category',
'MoSold': 'int64',
'YrSold': 'int64',
'YearBuilt': 'int64'}
data_types = {'MoSold': 'int64',
'YrSold': 'int64',
'YearBuilt': 'int64'}
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col='Id', dtype=data_types)
 df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col='Id', dtype=data_types )<count_missing_values>
mnli_train['lang_abv']="en"
mnli_train['dataset']="mnli"
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'No.Missing Values', 1 : '%'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '%', ascending=False ).round(1)
 print("There are " + str(mis_val_table_ren_columns.shape[0])+
" features with missing values.")
 return mis_val_table_ren_columns
missing_values_table(df_train )<drop_column>
train_df=pd.concat([train_df,mnli_train])
 train_df.shape
numerical_features = df_train.select_dtypes(include='number' ).columns.tolist()
numerical_features.remove('BsmtFinSF2')
 numerical_features.remove('LowQualFinSF')
 numerical_features.remove('BsmtHalfBath')
 numerical_features.remove('KitchenAbvGr')
 numerical_features.remove('EnclosedPorch')
 numerical_features.remove('3SsnPorch')
 numerical_features.remove('ScreenPorch')
 numerical_features.remove('PoolArea')
 numerical_features.remove('MiscVal')
 for feature in numerical_features:
num_summary(df_train, feature )<statistical_test>
from transformers import BertTokenizer,TFBertModel
import tensorflow as tf
from transformers import AutoTokenizer
def get_redundant_pairs(df):

 pairs_to_drop = set()
cols = df.columns
for i in range(0, df.shape[1]):
for j in range(0, i+1):
pairs_to_drop.add(( cols[i], cols[j]))
return pairs_to_drop
def get_top_abs_correlations(df, n=5):

 au_corr = df.corr().abs().unstack()
labels_to_drop = get_redundant_pairs(df)
 au_corr = au_corr.drop(labels=labels_to_drop ).sort_values(ascending=False)
 return au_corr[0:n]
print("Top Absolute Correlations")
 print("=========================
")
 print(get_top_abs_correlations(df_train[df_train.select_dtypes(include='number' ).columns.tolist() ], 15))<load_from_csv>
MODEL_NAME="jplu/tf-xlm-roberta-large"
MAX_LEN=64
BATCH_SIZE=64
data_types = {'MoSold': 'int64',
'YrSold': 'int64',
'YearBuilt': 'int64'}
df_train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv', index_col='Id', dtype=data_types)
 df_test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv', index_col='Id', dtype=data_types )<feature_engineering>
tokeniser=AutoTokenizer.from_pretrained(MODEL_NAME)
for data in [df_train, df_test]:
data['haspool'] = data['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 data['has2ndfloor'] = data['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 data['hasgarage'] = data['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 data['hasbsmt'] = data['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 data['hasfireplace'] = data['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
<data_type_conversions>
from tensorflow.keras import Input, Model, Sequential
from tensorflow.keras.layers import Dense, Dropout
from keras.optimizers import Adam
from transformers import TFAutoModel
numerical_features = df_train.select_dtypes(include='number' ).columns.tolist()
numerical_features.remove('SalePrice')
 categorical_features = df_train.select_dtypes(include='object' ).columns.tolist()<drop_column>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("In TPU STRATERGY")
 print('Number of replicas:', strategy.num_replicas_in_sync)
 except ValueError:
strategy = tf.distribute.get_strategy()
print('Number of replicas:', strategy.num_replicas_in_sync )
numerical_features_to_drop = ['FireplaceQu'] + ['GarageCars', 'GarageYrBlt', 'TotRmsAbvGrd', '1stFlrSF']
for data in [df_train, df_test]:
data.drop(numerical_features_to_drop,inplace=True, axis=1 )<data_type_conversions>
def createModel() :
with strategy.scope() :
model=TFAutoModel.from_pretrained(MODEL_NAME)
 input_ids = tf.keras.layers.Input(shape=(MAX_LEN,), name='input_token', dtype='int32')
 embedding=model(input_ids)[0]
cls_token=embedding[:,0,:]
output_layer = Dense(3, activation='softmax' )(cls_token)
 classification_model= Model(inputs=input_ids, outputs = output_layer)
 classification_model.compile(Adam(lr=1e-5),loss='sparse_categorical_crossentropy',metrics=['accuracy'])
 return classification_model
categorical_features_na = ['PoolQC', 'MiscFeature', 'Alley', 'Fence']
for data in [df_train, df_test]:
for feature in categorical_features_na:
data[feature].fillna("missing_value", inplace = True )<count_missing_values>
tokeniser,train_dataset,valid_dataset,train_rows=createTFDataSet(train_df,external_dataset=['xnli','mnli'] )
missing_values_table(df_train )<train_model>
n_steps = train_rows//BATCH_SIZE
train_history = model.fit(
 train_dataset,
steps_per_epoch=n_steps,
validation_data=valid_dataset,
epochs=5
)
class TransLog(BaseEstimator, TransformerMixin):
def __init__(self, feature_index):
self.feature_index = feature_index
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X_trans = np.log(X[:, self.feature_index])
 return np.c_[X[:, 0:self.feature_index - 1], X_trans, X[:, self.feature_index:]]<prepare_x_and_y>
test_encoded=tokeniser.batch_encode_plus(test[['hypothesis','premise']].values.tolist() ,pad_to_max_length=True,max_length=MAX_LEN,truncation=True)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(test_encoded['input_ids'])
.batch(BATCH_SIZE)
)
y_train = np.log(df_train['SalePrice'])
 X_train = df_train
X_train.drop(columns=['SalePrice'], inplace=True)
 X_test = df_test
numerical_features = df_test.select_dtypes(include='number' ).columns.tolist()
categorical_features = df_test.select_dtypes(include='object' ).columns.tolist()<define_variables>
test_preds = model.predict(test_dataset, verbose=1)
 predictions = test_preds.argmax(axis=1 )
log_feature_index = numerical_features.index('GrLivArea' )<choose_model_class>
submission = pd.DataFrame()
submission['id']=test['id'].tolist()
submission['prediction'] = predictions
num_pipeline = Pipeline([ 
('simple_imp', SimpleImputer(strategy='median')) , 
('log_trans', TransLog(feature_index=log_feature_index)) , 
('std_scaler', StandardScaler())
 ])
 cat_pipeline = Pipeline([ 
('simple_imp', SimpleImputer(strategy='most_frequent')) , 
('one_hot_enc', OneHotEncoder(handle_unknown='ignore'))
])
 comp_pipeline = ColumnTransformer([('num_pipeline', num_pipeline, numerical_features), 
('cat_pipeline', cat_pipeline, categorical_features)
 ])
 X_train_prepared = comp_pipeline.fit_transform(X_train)
 X_test_prepared = comp_pipeline.transform(X_test )<define_variables>
submission.to_csv("submission.csv",index=False )
<compute_train_metric><EOS> 
model.save_weights("XLM_R_MNLI_XNLI.h5",overwrite=True )