Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
import numpy as np
import pandas as pd
def bert_encode(data,maximum_length):
input_ids = []
attention_masks = []
for i in range(len(data)) :
encoded = tokenizer.encode_plus(
 data[i],
add_special_tokens=True,
max_length=maximum_length,
pad_to_max_length=True,
return_attention_mask=True, 
)
 input_ids.append(encoded['input_ids'])
 attention_masks.append(encoded['attention_mask'])
 return np.array(input_ids),np.array(attention_masks )<categorify>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
train_input_ids,train_attention_masks = bert_encode(text_data_train,100)
 test_input_ids,test_attention_masks = bert_encode(text_data_test,100 )<choose_model_class>
women = train_data[train_data['Sex'] == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('% of women who survived:', rate_women )
def create_model(bert_model):
input_ids = tf.keras.Input(shape=(100,),dtype='int32')
 attention_masks = tf.keras.Input(shape=(100,),dtype='int32')
 output = bert_model([input_ids,attention_masks])
 output = output[1]
output = tf.keras.layers.Dense(1,activation='sigmoid' )(output)
 model = tf.keras.models.Model(inputs = [input_ids,attention_masks],outputs = output)
 model.compile(Adam(lr=6e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model<train_model>
men = train_data[train_data.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('% of men who survived:', rate_men )
history = model.fit([train_input_ids,train_attention_masks],Y,
validation_split=0.2,
epochs=3,
batch_size=5 )<predict_on_test>
train_data[['Sex', 'Survived']].groupby(['Sex'] ).mean()
result = model.predict([test_input_ids,test_attention_masks])
 result = np.round(result ).astype(int)
 submit = pd.DataFrame(test['id'],columns=['id'])
 submit['target'] = result
submit.head()<save_to_csv>
train_data[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()
submit.to_csv('real_nlp_bert.csv',index=False )<load_from_csv>
women_count = 0
women_survived_count = 0
for idx, row in train_data.iterrows() :
if row['Sex'] == 'female':
women_count += 1
if row['Survived'] == 1:
women_survived_count += 1
women_survived_count / women_count
DATA_DIR = '.. /input/aptos2019-blindness-detection'
train_dir = join(DATA_DIR, 'train_images')
 label_df = pd.read_csv(join(DATA_DIR, 'train.csv'))
def train_validation_split(df, val_fraction=0.1):
val_ids = np.random.choice(df.id_code, size=int(len(df)* val_fraction))
val_df = df.query('id_code in @val_ids')
 train_df = df.query('id_code not in @val_ids')
 return train_df, val_df
train_df, val_df = train_validation_split(label_df)
 print(train_df.shape, val_df.shape)
 train_df.head()<categorify>
predictions = []
for idx, row in test_data.iterrows() :
if(row['Pclass'] == 1 or row['Pclass'] == 2)and row['Sex'] == 'female':
predictions.append(1)
 elif row['Age'] < 15 and row['Pclass'] == 1:
predictions.append(1)
 else:
predictions.append(0 )
%%time
class Diabetic_Retionopathy_Data(Dataset):
def __init__(self,
image_dir: str,
label_df: pd.DataFrame,
train=True,
transform=transforms.ToTensor() ,
sample_n=None,
in_memory=False,
write_images=False):

 self.image_dir = image_dir
self.transform = transform
self.train = train
self.in_memory = in_memory
if sample_n:
label_df = label_df.sample(n=min(sample_n, len(label_df)))
 ids = set(label_df.id_code)
 self.img_files = [f for f in os.listdir(image_dir)if f.split('.')[0] in ids]
label_df.index = label_df.id_code
self.label_df = label_df.drop('id_code', axis=1)
 if in_memory:
self.id2image = {}
for i, file_name in enumerate(self.img_files):
if i and i % 500 == 0:
print(f'{i} / {len(self.img_files)}')
 image = self._read_process_image(join(image_dir, file_name))
id_ = file_name.split('.')[0]
self.id2image[id_] = image
if write_images:
image.save(file_name)
 print(f'Initialized datatset with {len(self.img_files)} images.
 ')
 @staticmethod
def _read_process_image(file_path: str, size=256):
image = cv2.imread(file_path)
 return process_image_ratio_invariant(image, size=size)
 def __getitem__(self, idx):
file_name = self.img_files[idx]
id_ = file_name.split('.')[0]
if self.in_memory:
img = self.id2image[id_]
else:
img = self._read_process_image(join(self.image_dir, file_name))
X = self.transform(img)
 if self.train:
y = float(self.label_df.loc[id_].diagnosis)
 return X, y, id_
else:
return X, id_
def __len__(self):
return len(self.img_files)
 class RandomCenterCrop(transforms.CenterCrop):

 def __init__(self, min_size: int, max_size: int):
self.min_size = min_size
self.max_size = max_size
def __call__(self, img):

 size = np.random.randint(self.min_size, self.max_size + 1)
 crop = transforms.CenterCrop(( size, size))
return crop(img)
 def __repr__(self):
return f'{self.__class__.__name__}:(min-size={self.min_size}, max-size={self.max_size})'
batchsize = 16
train_transform = transforms.Compose([
RandomCenterCrop(min_size=200, max_size=256),
transforms.Resize(( 256, 256)) ,
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(( -20, 20)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 train = Diabetic_Retionopathy_Data(train_dir,
train_df,
transform=train_transform,
in_memory=True,
write_images=False)
 val = Diabetic_Retionopathy_Data(train_dir,
val_df,
transform=train_transform,
in_memory=True,
write_images=False)
 train_loader = DataLoader(train, batch_size=batchsize, num_workers=4, shuffle=True)
 val_loader = DataLoader(val, batch_size=batchsize, num_workers=3, shuffle=False)
 X, y, _ = next(iter(val_loader))
print(f'batch-dimension:
X = {X.shape},
y = {y.shape}')
 print(f'number of batches:
train: {len(train_loader)}
validation: {len(val_loader)}' )<choose_model_class>
test_data['Survived'] = predictions
def count_parameters(model: nn.Module):
return sum([np.prod(x.shape)for x in model.parameters() ])
 def print_lr_schedule(lr: float, decay: float, num_epochs=20):
print('
learning-rate schedule:')
 for i in range(num_epochs):
if i % 2 == 0:
print(f'{i}\t{lr:.6f}')
 lr = lr* decay
net = EfficientNet.from_name('efficientnet-b0')
 net.load_state_dict(torch.load('.. /input/efficientnet-pytorch/efficientnet-b0-08094119.pth'))
num_in_features = net._fc.in_features
net._fc = nn.Linear(num_in_features, 1)
 print(f'number of parameters: {count_parameters(net)}')
 net.train()
net.cuda()
loss_function = nn.MSELoss()
lr = 0.0015
lr_decay = 0.97
optimizer = torch.optim.Adam(net.parameters() , lr=lr, weight_decay=1e-5)
 scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=lr_decay)
 print_lr_schedule(lr, lr_decay )<train_model>
test_data[['PassengerId', 'Survived']].to_csv('submission.csv', index=False )