Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
print("Lasso picked " + str(sum(coef != 0)) + " variables and eliminated the other " + str(sum(coef == 0)) + " variables" )<concatenate>
train=pd.read_csv('.. /input/titanic/train.csv' )
imp_coef = pd.concat([coef.sort_values().head(12),
coef.sort_values().tail(11)] )<import_modules>
train.isnull().sum()
import xgboost as xgb<train_on_grid>
train['Survived'].value_counts()
dtrain = xgb.DMatrix(X_train, label = y)
 dtest = xgb.DMatrix(X_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=1000, early_stopping_rounds=100 )<train_model>
train['Survived'].value_counts(normalize=True )
model_xgb = xgb.XGBRegressor(n_estimators=1000, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X_train, y )<predict_on_test>
train['Sex'].value_counts()
xgb_preds = np.expm1(model_xgb.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))<create_dataframe>
embark=pd.get_dummies(train['Embarked'] )
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )<prepare_output>
gender=pd.get_dummies(train['Sex'] )
preds = 0.7*lasso_preds + 0.3*xgb_preds<save_to_csv>
train['Age'].fillna(30,inplace=True)
 train['Age']=train['Age'].astype('int' )
solution = pd.DataFrame({"ID":test.ID, "price":preds})
 solution.to_csv("ridge_sol.csv", index = False )<import_modules>
train['Age_grp']=train['Age'].apply(lambda x: age_grp(x))
from keras.layers import Dense
from keras.models import Sequential
from keras.regularizers import l1
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split<normalization>
age=pd.get_dummies(train['Age_grp'] )
X_train = StandardScaler().fit_transform(X_train )<split>
train_df=pd.concat([train,embark,gender,age],axis=1 )
X_tr, X_val, y_tr, y_val = train_test_split(X_train, y, random_state = 3 )<choose_model_class>
def is_var(val):
if val>0:
return 1
else:
return 0
model = Sequential()
model.add(Dense(1, input_dim = X_train.shape[1], W_regularizer=l1(0.001)))
 model.compile(loss = "mse", optimizer = "adam" )<train_model>
train_df['Family']=train_df['Parch'] + 1 + train_df['SibSp']
train_df['Parch']=train_df['Parch'].apply(lambda x: is_var(x))
train_df['SibSp']=train_df['SibSp'].apply(lambda x: is_var(x))
hist = model.fit(X_tr, y_tr, validation_data =(X_val, y_val))<set_options>
sel_cols=['Fare',
'Pclass', 'SibSp',
'Parch', 'C', 'Q',
'S', 'female', 'male', '18-24', '25-34', '35-44', '45+', '<13','13-18', 'Family'
]
%matplotlib inline<set_options>
train_df.fillna(0,inplace=True )
def seed_everything(seed=42):
os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<define_variables>
X=train_df[sel_cols]
DIRPATH = '.. /input/ailab-ml-training-0/'
TRAIN_IMAGE_DIR = 'train_images/train_images/'
TEST_IMAGE_DIR = 'test_images/test_images/'
ID = 'fname'
TARGET = 'label'
VALID_SIZE = 0.2
EPOCHS = 5
BATCH_SIZE = 64
LR = 1e-3
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
SEED = 42
seed_everything(SEED )<load_from_csv>
y=train_df['Survived']
train_df = pd.read_csv(os.path.join(DIRPATH, 'train.csv'))<load_pretrained>
train_X,val_X,train_y,val_y=train_test_split(X,y,test_size=0.3,random_state=1 )
class TrainDataset(Dataset):
def __init__(self, fname_list, label_list, image_dir, transform=None):
super().__init__()
self.fname_list = fname_list
self.label_list = label_list
self.image_dir = image_dir
self.transform = transform
def __len__(self):
return len(self.fname_list)
 def __getitem__(self, idx):
fname = self.fname_list[idx]
label = self.label_list[idx]
image = cv2.imread(os.path.join(self.image_dir, fname))
if self.transform is not None:
image = self.transform(image)
 return image, label<define_search_model>
lr=LogisticRegression(max_iter=400)
 lr.fit(train_X,train_y )
class SimpleClassifier(nn.Module):
def __init__(self):
super().__init__()
self.conv_module = nn.Sequential(
 nn.Conv2d(3, 32, 3, stride=1, padding=1, bias=True),
nn.ReLU(True),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, stride=1, padding=1, bias=True),
nn.ReLU(True),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, stride=1, padding=1, bias=True),
nn.ReLU(True), 
)
 self.dense_module = nn.Sequential(
 nn.Linear(128*7*7, 10, bias=True)
)
 def forward(self, x):
x = self.conv_module(x)
 x = x.view(x.size(0), -1)
 x = self.dense_module(x)
 return x<split>
lr.score(train_X,train_y )
fname_list = train_df[ID].to_list()
label_list = train_df[TARGET].to_list()
train_fname_list, valid_fname_list, train_label_list, valid_label_list = train_test_split(
 fname_list, label_list, test_size=VALID_SIZE, random_state=SEED, shuffle=True
)<create_dataframe>
lr.score(val_X,val_y )
image_dir = os.path.join(DIRPATH, TRAIN_IMAGE_DIR)
 transform = transforms.Compose([
transforms.ToTensor()
])
 train_dataset = TrainDataset(train_fname_list, train_label_list, image_dir, transform=transform)
 train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 valid_dataset = TrainDataset(valid_fname_list, valid_label_list, image_dir, transform=transform)
 valid_dataloader = DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False )<choose_model_class>
test=pd.read_csv('.. /input/titanic/test.csv' )
model = SimpleClassifier().to(DEVICE)
 optim = Adam(model.parameters() , lr=LR)
 criterion = nn.CrossEntropyLoss()<train_model>
embark=pd.get_dummies(test['Embarked'] )
mb = master_bar(range(EPOCHS))
for epoch in mb:
model.train()
train_loss_list = []
train_accuracy_list = []
for batch_image, batch_label in progress_bar(train_dataloader, parent=mb):
batch_image = batch_image.to(dtype=torch.float32, device=DEVICE)
 batch_label = batch_label.to(dtype=torch.long, device=DEVICE)
 optim.zero_grad()
batch_pred = model(batch_image)
 loss = criterion(batch_pred, batch_label)
 loss.backward()
optim.step()
train_loss_list.append(loss.item())
 accuracy = accuracy_score(torch.argmax(batch_pred, axis=1 ).cpu().numpy() , batch_label.cpu().numpy())
 train_accuracy_list.append(accuracy)
 model.eval()
valid_loss_list = []
valid_accuracy_list = []
for batch_image, batch_label in valid_dataloader:
batch_image = batch_image.to(dtype=torch.float32, device=DEVICE)
 batch_label = batch_label.to(dtype=torch.long, device=DEVICE)
 with torch.no_grad() :
batch_pred = model(batch_image)
 loss = criterion(batch_pred, batch_label)
 valid_loss_list.append(loss.item())
 accuracy = accuracy_score(torch.argmax(batch_pred, axis=1 ).cpu().numpy() , batch_label.cpu().numpy())
 valid_accuracy_list.append(accuracy)
 mb.write('epoch: {}/{} - loss: {:.5f} - accuracy: {:.3f} - val_loss: {:.5f} - val_accuracy: {:.3f}'.format(
 epoch,
EPOCHS,
np.mean(train_loss_list),
np.mean(train_accuracy_list),
np.mean(valid_loss_list),
np.mean(valid_accuracy_list)
))<load_from_csv>
gender=pd.get_dummies(test['Sex'] )
submission_df = pd.read_csv(os.path.join(DIRPATH, 'sample_submission.csv'))<categorify>
test['Age'].fillna(30,inplace=True)
 test['Age']=test['Age'].astype('int' )
fname_list = submission_df[ID].to_list()
label_list = submission_df[TARGET].to_list()
image_dir = os.path.join(DIRPATH, TEST_IMAGE_DIR)
 transform = transforms.Compose([transforms.ToTensor() ])
 test_dataset = TrainDataset(fname_list, label_list, image_dir, transform=transform)
 test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False )<feature_engineering>
test['Age_grp']=test['Age'].apply(lambda x: age_grp(x))
submission_df[TARGET] = predictions<save_to_csv>
age=pd.get_dummies(test['Age_grp'] )
submission_df.to_csv('submission.csv', index=False )<import_modules>
test_df=pd.concat([test,embark,gender,age],axis=1 )
FileLink('submission.csv' )<import_modules>
test_df['Family']=test_df['Parch']+1+test_df['SibSp']
test_df['Parch']=test_df['Parch'].apply(lambda x: is_var(x))
test_df['SibSp']=test_df['SibSp'].apply(lambda x: is_var(x))
import numpy as np
import pandas as pd
from tqdm.notebook import tqdm
import re
import torch
from sklearn.model_selection import train_test_split
from tqdm import tqdm_notebook
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from scipy import stats
from sklearn.model_selection import KFold
import os,urllib,glob,tarfile
from transformers import BertJapaneseTokenizer, BertModel, BertConfig<install_modules>
test_df.fillna(0,inplace=True )
!pip install mecab-python3==0.996.2 fugashi ipadic<load_from_csv>
test_X=test_df[sel_cols]
train_df = pd.read_csv(".. /input/japanesereviewratingprediction/yahoo_review_train.csv")
 test_df = pd.read_csv(".. /input/japanesereviewratingprediction/yahoo_review_test.csv")
 sample_submission = pd.read_csv(".. /input/japanesereviewratingprediction/sample_submission.csv" )<filter>
test_y=lr.predict(test_X )
train_df = train_df[train_df["Label"]!=-1]<load_pretrained>
sub=pd.read_csv('.. /input/titanic/gender_submission.csv' )
tokenizer = BertJapaneseTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-whole-word-masking")
 for i,item in enumerate(tokenizer.vocab.items()):
print(item)
 if i > 20:
break<define_variables>
sub['Survived']=test_y
<string_transform><EOS> 
sub.to_csv('submission.csv',index=False )