Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<count_values>
!pip install transformers
all_data['OverallCond'].value_counts()<data_type_conversions>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 tokenizer = AutoTokenizer.from_pretrained("joeddav/xlm-roberta-large-xnli", padding=True)
 model = AutoModelForSequenceClassification.from_pretrained("joeddav/xlm-roberta-large-xnli" ).to(device)
 model.config
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )<categorify>
train_data = pd.read_csv('.. /input/contradictory-my-dear-watson/train.csv', encoding='utf8')
 test_data = pd.read_csv('.. /input/contradictory-my-dear-watson/test.csv', encoding='utf8')
 train_data.head()
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
print('Shape all_data: {}'.format(all_data.shape))<feature_engineering>
train_data['label'] = train_data['label'].replace([0, 2], [2, 0])
 train_data.head()
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']<feature_engineering>
class MyDataset(Dataset):
def __init__(self, df, tokenizer, labels = False):
self.inputs = df.loc[:,['premise', 'hypothesis']].values
self.tokenizer = tokenizer
self.labels = labels
if self.labels:
self.tgt = df['label'].values
def __len__(self):
return len(self.inputs)
 def __getitem__(self, idx):
inputs = tokenizer(self.inputs[idx].tolist() , add_special_tokens=True, padding=True, return_tensors='pt')
 if self.labels:
inputs['labels'] = self.tgt[idx]
return inputs
return inputs
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
all_data[feat] = boxcox1p(all_data[feat], lam )<categorify>
def eval_model(model, dataloader, device):
correct = 0
eval_loss = 0
model.eval()
for i, batch in enumerate(dataloader):
out = model(input_ids=batch['input_ids'].squeeze().to(device),
attention_mask=batch['attention_mask'].squeeze().to(device),
labels=batch['labels'].squeeze().to(device))
eval_loss += out[0].item()
correct +=(out[1].argmax(dim=1)==batch['labels'].squeeze().to(device)).float().sum()
accu = correct / train_dataloader.dataset.__len__()
eval_loss /= len(dataloader)
 return eval_loss, accu
all_data = pd.get_dummies(all_data)
 all_data.shape<prepare_x_and_y>
train_dataset = MyDataset(train_data.loc[0:1000], tokenizer, labels=True)
 train_dataloader = DataLoader(dataset=train_dataset,
sampler=BatchSampler(
 SequentialSampler(train_dataset),
batch_size=8, drop_last=True))
loss, accuracy = eval_model(model, train_dataloader, device)
 print("Loss: {}, Accuracy: {}".format(loss, accuracy))
train = all_data[:ntrain]
test = all_data[ntrain:]
train.shape<import_modules>
def submission_predict(model, dataloader, device):
model.eval()
predicts = np.array([])
 for i, batch in enumerate(dataloader):
inp_ids = batch['input_ids'].squeeze().to(device)
 mask = batch['attention_mask'].squeeze().to(device)
 out = model(input_ids=inp_ids, attention_mask=mask)
 batch_preds = out[0].argmax(dim=1)
 predicts = np.concatenate(( predicts, batch_preds.cpu().detach().numpy()))
return predicts
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb<compute_train_metric>
test_dataset = MyDataset(test_data, tokenizer, labels=False)
 test_dataloader = DataLoader(dataset=test_dataset,
sampler=BatchSampler(
 SequentialSampler(test_dataset),
batch_size=8, drop_last=False), shuffle=False)
 test_preds = submission_predict(model, test_dataloader, device )
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse= np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse )<compute_train_metric>
submission = np.concatenate(( test_data['id'].values.reshape(-1,1), np.int32(test_preds.reshape(-1,1))), axis=1)
 submission = pd.DataFrame(submission, columns=['id', 'prediction'])
 submission['prediction'] = submission['prediction'].astype(np.int32 ).replace([0,2], [2,0])
 submission.to_csv('submission.csv', index=False)
 model.save_pretrained('./model.pt')
 tokenizer.save_pretrained('.. /tokenizer' )