Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<compute_train_metric>
!nvidia-smi
rf_clf = RandomForestRegressor(random_state=RANDOM_SEED)
 %time rf_clf.fit(X_train_prepared, y_train)
 y_train_predicted = rf_clf.predict(X_train_prepared)
 results.append({'alg': 'rf_clf', 'model': rf_clf, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<train_model>
!pip install git+https://github.com/huggingface/transformers
ada_clf=AdaBoostRegressor(random_state=RANDOM_SEED, n_estimators=100)
 %time ada_clf.fit(X_train_prepared,y_train)
 y_train_predicted = ada_clf.predict(X_train_prepared)
 print('Feature Importance Values
',ada_clf.feature_importances_)
 results.append({'alg': 'ada_clf', 'model': ada_clf, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<predict_on_test>
!pip install -U nlp
xgb_clf = XGBRegressor(objective="reg:squarederror", seed=RANDOM_SEED)
 %time xgb_clf.fit(X_train_prepared, y_train)
 y_train_predicted = xgb_clf.predict(X_train_prepared)
 results.append({'alg': 'xgb_clf', 'model': xgb_clf, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<compute_train_metric>
import pandas as pd
import numpy as np
import torch
import nlp
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from torch.utils.data import DataLoader
lso_clf = Lasso(random_state=RANDOM_SEED)
 %time lso_clf.fit(X_train_prepared, y_train)
 y_train_predicted = lso_clf.predict(X_train_prepared)
 results.append({'alg': 'lso_clf', 'model': lso_clf, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<compute_train_metric>
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
eln_clf = ElasticNet(random_state=RANDOM_SEED)
 %time eln_clf.fit(X_train_prepared, y_train)
 y_train_predicted = eln_clf.predict(X_train_prepared)
 results.append({'alg': 'eln_clf', 'model': eln_clf, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<compute_train_metric>
tokenizer = AutoTokenizer.from_pretrained("joeddav/xlm-roberta-large-xnli")
 model = AutoModelForSequenceClassification.from_pretrained("joeddav/xlm-roberta-large-xnli" ).to(device )
lgbm_clf = LGBMRegressor(random_state=RANDOM_SEED)
 %time lgbm_clf.fit(X_train_prepared, y_train)
 y_train_predicted = lgbm_clf.predict(X_train_prepared)
 results.append({'alg': 'lgbm_clf', 'model': lgbm_clf, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<compute_test_metric>
train_dataset = nlp.load_dataset('csv', data_files={'train': '.. /input/contradictory-my-dear-watson/train.csv'}, split='train[:1024]' )
print('
-------------------------Results-----------------------')
 for result in sorted(results, key=lambda k: k['mae']):
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<define_search_space>
train_dataset.num_rows
xgb_param_grid = {
'min_child_weight': [1],
'gamma': [0],
'subsample': [1.0],
'colsample_bytree': [0.2],
'max_depth': [3],
'n_estimators': [2500]
}
xgb_grid_search = GridSearchCV(xgb_clf, xgb_param_grid, cv=CV_FOLDS, n_jobs=4, return_train_score=True, verbose=1)
 %time xgb_grid_search.fit(X_train_prepared, y_train)
 print(xgb_grid_search.best_params_)
 print(xgb_grid_search.best_score_)
 y_train_predicted = xgb_grid_search.best_estimator_.predict(X_train_prepared)
 results.append({'alg': 'xgb_clf_tuned', 'model': xgb_grid_search.best_estimator_, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted), 'best_params': xgb_grid_search.best_params_} )<init_hyperparams>
def convert_to_features(batch):
input_pairs = list(zip(batch['premise'], batch['hypothesis']))
encodings = tokenizer.batch_encode_plus(input_pairs,
add_special_tokens=True,
padding=False)
 return encodings
train_dataset = train_dataset.map(convert_to_features, batched=True)
 train_dataset.set_format("torch", columns=['input_ids', 'attention_mask', 'label'] )
rf_param_grid = [{'ccp_alpha': [0.0],
'criterion': ['mse'],
'max_depth': [None],
'max_features': [None],
'max_leaf_nodes': [None],
'min_impurity_decrease': [0.0],
'oob_score': [True],
'min_impurity_split': [None],
'min_samples_leaf': [2],
'min_samples_split': [4],
'min_weight_fraction_leaf': [0.0],
'random_state': [42],
}
]
rf_grid_search = GridSearchCV(rf_clf, rf_param_grid, n_jobs=4, cv=CV_FOLDS, return_train_score=True, verbose=1)
 %time rf_grid_search.fit(X_train_prepared, y_train)
 print(rf_grid_search.best_params_)
 print(rf_grid_search.best_score_)
 y_train_predicted = rf_grid_search.best_estimator_.predict(X_train_prepared)
 results.append({'alg': 'rf_clf_tuned', 'model': rf_grid_search.best_estimator_, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted), 'best_params': rf_grid_search.best_params_} )<define_search_space>
batch_size = 16
ada_param_grid = [{'n_estimators': [2000],
'loss': ['square'],
'random_state': [RANDOM_SEED]
}
]
ada_grid_search = GridSearchCV(ada_clf, ada_param_grid, n_jobs=4, cv=CV_FOLDS, return_train_score=True, verbose=1)
 %time ada_grid_search.fit(X_train_prepared, y_train)
 print(ada_grid_search.best_params_)
 print(ada_grid_search.best_score_)
 y_train_predicted = ada_grid_search.best_estimator_.predict(X_train_prepared)
 results.append({'alg': 'ada_clf_tuned', 'model': ada_grid_search.best_estimator_, 'mse': mean_squared_error(y_train, y_train_predicted), 'mae': mean_absolute_error(y_train, y_train_predicted)})
 result=results[-1]
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'] )<train_model>
train_dataloader = DataLoader(
 train_dataset,
batch_size=batch_size,
num_workers=0,
collate_fn=tokenizer.pad
)
<save_to_csv>
def eval(model, dataloader, device):
model.eval()
val_loss = 0
correct = 0
with torch.no_grad() :
for batch in dataloader:
input_ids = batch['input_ids'].to(device)
 attention_mask = batch['attention_mask'].to(device)
 labels = batch['label'].to(device)
 outputs = model(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
 loss = outputs[0]
val_loss += loss.item()
correct +=(outputs[1].argmax(dim=-1)== labels ).float().sum()
return val_loss / len(dataloader), correct / dataloader.dataset.shape[0]
print('
-------------------------Results-----------------------')
 for result in sorted(results, key=lambda k: k['mae']):
print('Algorithm:', result['alg'], 'MSE:', result['mse'], 'MAE:', result['mae'])
 best_model = sorted(results, key=lambda k: k['mae'])[0]['model']
print(best_model)
 y_test_predicted = np.exp(best_model.predict(X_test_prepared))
output = pd.DataFrame({'Id': X_test.index, 'SalePrice': y_test_predicted})
 output.to_csv('/kaggle/working/submission.csv', index=False )<load_from_csv>
%%time
val_loss, val_acc = eval(model, train_dataloader, device )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 features = ["Pclass", "Sex", "SibSp", "Parch", "Embarked"]
X_train = pd.get_dummies(train_data[features])
 y_train = train_data["Survived"]
final_X_test = pd.get_dummies(test_data[features])
 classifier = RandomForestClassifier(max_features='auto', min_samples_leaf=10)
 classifier.fit(X_train, y_train)
 predictions = classifier.predict(final_X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId,
'Survived': predictions})
 output.to_csv('submission.csv', index=False )<save_to_csv>
print(f'Loss : {val_loss:.5}, Accuracy : {val_acc:.2%}' )
df = pd.read_csv('/kaggle/input/nlp-with-disaster-tweets-eda-cleaning-and-bert/perfect_submission.csv')
 print(df.columns)
 df.to_csv('perfect-score.csv', index=False )<import_modules>
model.eval()
with torch.no_grad() :
input_ids = batch['input_ids'].to(device)
 attention_mask = batch['attention_mask'].to(device)
 outputs = model(input_ids=input_ids, attention_mask=attention_mask)[0]
print(f'Output : {torch.argmax(outputs, -1 ).cpu().detach() }')
 print('Target : {}'.format(batch['label']))
import pandas as pd
from tqdm import tqdm<load_from_csv>
train = pd.read_csv('.. /input/contradictory-my-dear-watson/train.csv', nrows=1024 )
train = pd.read_csv('.. /input/ames-housing-dataset/AmesHousing.csv')
 train.drop(['PID'], axis=1, inplace=True)
 origin = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 train.columns = origin.columns
test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 print('Train:{} Test:{}'.format(train.shape,test.shape))<drop_column>
train_dataset = nlp.Dataset.from_pandas(train )
missing = test.isnull().sum()
missing = missing[missing>0]
train.drop(missing.index, axis=1, inplace=True)
 train.drop(['Electrical'], axis=1, inplace=True)
 test.dropna(axis=1, inplace=True)
 test.drop(['Electrical'], axis=1, inplace=True )<feature_engineering>
train_dataset.num_rows
l_test = tqdm(range(0, len(test)) , desc='Matching')
 for i in l_test:
for j in range(0, len(train)) :
for k in range(1, len(test.columns)) :
if test.iloc[i,k] == train.iloc[j,k]:
continue
else:
break
else:
submission.iloc[i, 1] = train.iloc[j, -1]
break
l_test.close()<save_to_csv>
train_dataloader = DataLoader(
 train_dataset,
batch_size=batch_size,
num_workers=0,
collate_fn=tokenizer.pad
)
submission.to_csv('result-with-best.csv', index=False )<load_from_csv>
%%time
val_loss, val_acc = eval(model, train_dataloader, device )
train = pd.read_csv("/kaggle/input/covid-diagnostic/covid_19_train.csv")
 test = pd.read_csv("/kaggle/input/covid-diagnostic/covid_19_test.csv")
 submission = pd.read_csv("/kaggle/input/covid-diagnostic/covid_19_submission.csv" )<prepare_x_and_y>
print(f'Loss : {val_loss:.5}, Accuracy : {val_acc:.2%}' )
y = train["covid_19"].values
y<concatenate>
sample_submission = pd.read_csv('.. /input/contradictory-my-dear-watson/sample_submission.csv' )
df = pd.concat([train, test])
 df.shape<count_missing_values>
test_dataset = nlp.load_dataset('csv', data_files={'test': '.. /input/contradictory-my-dear-watson/test.csv'})
 test_dataset = test_dataset.map(convert_to_features, batched=True)
 test_dataset.set_format("torch", columns=['input_ids', 'attention_mask'] )
df.isna().mean(0 ).values<drop_column>
test_dataloader = DataLoader(
 test_dataset['test'],
batch_size=batch_size,
drop_last=False,
num_workers=0,
shuffle=False,
collate_fn=tokenizer.pad
)
df = df.drop(( df.isna().mean(0)).index[df.isna().mean(0)> 0.95].values, axis = 'columns' )<groupby>
def predict(model, dataloader, device):
model.eval()
val_loss = 0
correct = 0
test_preds = np.array([])
 with torch.no_grad() :
for batch in dataloader:
input_ids = batch['input_ids'].to(device)
 attention_mask = batch['attention_mask'].to(device)
 outputs = model(input_ids=input_ids, attention_mask=attention_mask)[0]
test_preds = np.concatenate(( test_preds, torch.argmax(outputs, -1 ).cpu().detach().numpy()))
sample_submission.prediction = test_preds.astype(int)
 sample_submission.prediction.replace([0, 2], [2, 0], inplace=True)
 sample_submission.to_csv('submission.csv', index=False)
 return sample_submission
<feature_engineering><EOS> 
%%time
predict(model, test_dataloader, device )