Kaggle competition: Titanic - Machine Learning from Disaster
test_X = test[test.drop('id', axis = 1 ).columns].values<predict_on_test>
train_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_df.head()
gsc = RandomForestClassifier(n_estimators=1000)
 gsc.fit(X, y)
 print(accuracy_score(train_y, gsc.predict(train_x)))
 print(accuracy_score(test_y, gsc.predict(test_x)))
 test_Y = gsc.predict(test_X )<create_dataframe>
test_df = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_df.head()
data = {'id':test['id'],'class':test_Y}
df = pd.DataFrame(data )<save_to_csv>
train_df[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
df.to_csv(path_or_buf ='output.csv', index = False )<load_from_csv>
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
df_train = pd.read_csv('/kaggle/input/1056lab-diabetes-diagnosis/train.csv', index_col=0)
 df_test = pd.read_csv('/kaggle/input/1056lab-diabetes-diagnosis/test.csv', index_col=0 )<set_options>
train_df[['SibSp', 'Survived']].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
%matplotlib inline
sns.countplot(x='Diabetes', data=df_train)
 plt.show()<categorify>
train_df[['Parch', 'Survived']].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
df_train_dummies = pd.get_dummies(df_train_over, columns=['Gender'], drop_first=True)
 df_test_dummies = pd.get_dummies(df_test, columns=['Gender'], drop_first=True )<train_model>
train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False)
X_train_dummies = df_train_dummies.drop(columns='Diabetes' ).values
y_train_dummies = df_train_dummies['Diabetes'].values
rfc = RandomForestClassifier(max_depth=4, n_estimators=50, criterion='gini')
 rfc.fit(X_train_dummies, y_train_dummies )<predict_on_test>
all_df = [train_df, test_df]
X_test = df_test_dummies.values
y_pred = rfc.predict_proba(X_test)[:, 1]<save_to_csv>
for df in all_df:
df['Sex'] = df['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 train_df.head()
submit = pd.read_csv('/kaggle/input/1056lab-diabetes-diagnosis/sampleSubmission.csv')
 submit['Diabetes'] = y_pred
submit.to_csv('submission.csv', index=False )<import_modules>
common_Pclass = 'S'
train_df['Embarked'].fillna(common_Pclass, inplace=True)
 train_df['Embarked'] = train_df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test_df['Embarked'] = test_df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()
sys.path.insert(0, '/kaggle/input/efficientnetmodel/efficientnetModel.py')
 sys.path.append('/kaggle/input/efficientnetmodel')
 sys.path.append('/kaggle/input/efficientnetmodel/utils.py')
<init_hyperparams>
train_df['Cabin'].isna().sum()
IMAGE_SIZE = 320
def int_parameter(level, maxval):

 return int(level * maxval / 10)
 def float_parameter(level, maxval):

 return float(level)* maxval / 10.
 def sample_level(n):
return np.random.uniform(low=0.1, high=n)
 def autocontrast(pil_img, _):
return ImageOps.autocontrast(pil_img)
 def equalize(pil_img, _):
return ImageOps.equalize(pil_img)
 def posterize(pil_img, level):
level = int_parameter(sample_level(level), 4)
 return ImageOps.posterize(pil_img, 4 - level)
 def rotate(pil_img, level):
degrees = int_parameter(sample_level(level), 30)
 if np.random.uniform() > 0.5:
degrees = -degrees
return pil_img.rotate(degrees, resample=Image.BILINEAR)
 def solarize(pil_img, level):
level = int_parameter(sample_level(level), 256)
 return ImageOps.solarize(pil_img, 256 - level)
 def shear_x(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
 if np.random.uniform() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, level, 0, 0, 1, 0),
resample=Image.BILINEAR)
 def shear_y(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
 if np.random.uniform() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, 0, 0, level, 1, 0),
resample=Image.BILINEAR)
 def translate_x(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
 if np.random.random() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, 0, level, 0, 1, 0),
resample=Image.BILINEAR)
 def translate_y(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
 if np.random.random() > 0.5:
level = -level
return pil_img.transform(( IMAGE_SIZE, IMAGE_SIZE),
Image.AFFINE,(1, 0, 0, 0, 1, level),
resample=Image.BILINEAR)
 def color(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Color(pil_img ).enhance(level)
 def contrast(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Contrast(pil_img ).enhance(level)
 def brightness(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Brightness(pil_img ).enhance(level)
 def sharpness(pil_img, level):
level = float_parameter(sample_level(level), 1.8)+ 0.1
return ImageEnhance.Sharpness(pil_img ).enhance(level)
 augmentations = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y
]
augmentations_all = [
autocontrast, equalize, posterize, rotate, solarize, shear_x, shear_y,
translate_x, translate_y, color, contrast, brightness, sharpness
] 
<define_variables>
train_df['Cabin'].fillna('X', inplace=True)
 test_df['Cabin'].fillna('X', inplace=True )
use_gpu = torch.cuda.is_available()
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
dir_test = ".. /input/issm2020-ai-challenge/semTest/semTest/"
data_dir = '.. /input/issm2020-ai-challenge/semTrain/semTrain'
batch_size = 16
lr = 0.01
momentum = 0.9
num_epochs = 100
input_size = 224
class_num = 10
net_name = 'efficientnet-oriDuAug-b7'
mixture_width = 3
mixture_depth = -1
aug_severity = 3
all_ops = True
no_jsd = True
def aug(image, preprocess):

 aug_list = augmentations
if all_ops:
aug_list = augmentations_all
ws = np.float32(np.random.dirichlet([1] * mixture_width))
m = np.float32(np.random.beta(1, 1))
mix = torch.zeros_like(preprocess(image))
for i in range(mixture_width):
image_aug = image.copy()
depth = mixture_depth if mixture_depth > 0 else np.random.randint(
 1, 4)
 for _ in range(depth):
op = np.random.choice(aug_list)
 image_aug = op(image_aug, aug_severity)
 mix += ws[i] * preprocess(image_aug)
 mixed =(1 - m)* preprocess(image)+ m * mix
return mixed
class AugMixDataset(torch.utils.data.Dataset):

 def __init__(self, dataset, preprocess, no_jsd=False):
self.dataset = dataset
self.preprocess = preprocess
self.no_jsd = no_jsd
def __getitem__(self, i):
x, y = self.dataset[i]
if self.no_jsd:
return aug(x, self.preprocess), y
else:
im_tuple =(self.preprocess(x), aug(x, self.preprocess),
aug(x, self.preprocess))
return im_tuple, y
def __len__(self):
return len(self.dataset)
 augmix_tf: Compose = transforms.Compose(
 [
transforms.Resize(input_size),
transforms.RandomHorizontalFlip() ,
transforms.RandomCrop(input_size, padding=16)
 ] 
)
 preprocess = transforms.Compose(
 [
transforms.Resize(input_size),
transforms.ToTensor() ,
transforms.Normalize(( 0.4914, 0.4822, 0.4465),(0.2023, 0.1994, 0.2010))
])
 def loaddata(data_dir, batch_size, set_name, shuffle):
data_transforms = {
'train': transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.RandomAffine(degrees=0, translate=(0.05, 0.05)) ,
transforms.RandomHorizontalFlip() ,
transforms.RandomGrayscale(p=0.1),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ]),
'test': transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ]),
}
image_datasets = datasets.ImageFolder(data_dir, augmix_tf)
 image_datasets = AugMixDataset(image_datasets, preprocess, no_jsd)
 dataset_loaders = torch.utils.data.DataLoader(image_datasets,
batch_size=batch_size,
shuffle=shuffle, num_workers=0)
 data_set_sizes = len(image_datasets)
 return dataset_loaders, data_set_sizes
def train_model(model_ft, criterion, optimizer, lr_scheduler, num_epochs):
train_loss = []
since = time.time()
best_model_wts = model_ft.state_dict()
best_acc = 0.0
model_ft.train(True)
 for epoch in range(num_epochs):
dset_loaders, dset_sizes = loaddata(data_dir=data_dir, batch_size=batch_size, set_name='train', shuffle=True)
 print('Data Size', dset_sizes)
 print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
 optimizer = lr_scheduler(optimizer, epoch)
 running_loss = 0.0
running_corrects = 0
count = 0
for data in dset_loaders:
if count==189:
break
inputs, labels = data
labels = torch.squeeze(labels.type(torch.LongTensor))
if use_gpu:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
 else:
inputs, labels = Variable(inputs), Variable(labels)
 outputs = model_ft(inputs)
 loss = criterion(outputs, labels)
 _, preds = torch.max(outputs.data, 1)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
count += 1
if count % 10 == 0 or outputs.size() [0] < batch_size:
print('Epoch:{}: loss:{:.5f}'.format(epoch, loss.item()))
train_loss.append(loss.item())
 running_loss += loss.item() * inputs.size(0)
 running_corrects += torch.sum(preds == labels.data)
 epoch_loss = running_loss / dset_sizes
epoch_acc = running_corrects.double() / dset_sizes
print('Loss: {:.4f} Acc: {:.6f}'.format(
 epoch_loss, epoch_acc))
model_ft.load_state_dict(best_model_wts)
 model_out_path = model_dir + "/" + net_name + '{}.pth'.format(epoch)
 print(model_dir + '/' + net_name + '{}.pth'.format(epoch))
torch.save(model_ft, model_out_path)
 if epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = model_ft.state_dict()
if epoch_acc > 0.999:
break
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
 time_elapsed // 60, time_elapsed % 60))
return train_loss, best_model_wts
def test_csv(model):
transform_test = transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 results = []
image_number = 0
resultFrame = pd.DataFrame(columns=["Id","LABEL"])
 img_test = os.listdir(dir_test)
 img_test.sort()
for i in range(len(img_test)) :
img = Image.open(dir_test + img_test[i])
 input = transform_test(img ).cuda()
input = input.unsqueeze(0)
 input = Variable(input)
 score = model(input)
 probability = torch.nn.functional.softmax(score, dim=1)
 max_value, index = torch.max(probability, 1)
 image_number += 1
probability = np.round(probability.cpu().detach().numpy())
 index = np.round(index.cpu().detach().numpy())
 resultFrame = resultFrame.append({'Id':int(image_number),'LABEL': int(index[0] + 1)}, ignore_index=True)
 print(resultFrame)
 resultFrame.to_csv('submission.csv', index=False)
 def exp_lr_scheduler(optimizer, epoch, init_lr=lr, lr_decay_epoch=10): 
<load_pretrained>
train_df['Cabin'].value_counts()
model_ft=torch.load('/kaggle/input/efficientnetmodel/efficientnet-oriDuAug-b799.pth')
 model_ft = model_ft.eval().cuda()
test_csv(model_ft )<prepare_x_and_y>
for df in all_df:
df['Cabin'] = df['Cabin'].astype(str ).str[0]
test_df['Cabin']
%matplotlib inline
train = pd.read_csv('.. /input/cars-train.csv')
 val = pd.read_csv('.. /input/cars-test.csv')
 x_train = pd.get_dummies(train.drop(['class', 'car.id'], axis=1))
x_val = pd.get_dummies(val.drop(['class', 'car.id'], axis=1))
y_train = pd.get_dummies(train['class'])
 y_val = pd.get_dummies(val['class'] )<choose_model_class>
train_df.groupby(['Survived', 'Cabin'])['PassengerId'].sum()
model = Sequential()
model.add(Dense(128, input_dim=(21), activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(128, activation = 'selu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
 model.add(Dense(4, activation = 'softmax'))
model.compile(optimizer='adamax', loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
for df in all_df:
df.loc[(df['Cabin'] == 'A'), 'Cabin'] = 0
df.loc[(df['Cabin'] == 'B'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'C'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'D'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'E'), 'Cabin'] = 1
df.loc[(df['Cabin'] == 'F'), 'Cabin'] = 0
df.loc[(df['Cabin'] == 'G'), 'Cabin'] = 0
df.loc[(df['Cabin'] == 'T'), 'Cabin'] = 2
df.loc[(df['Cabin'] == 'X'), 'Cabin'] = 3
train_df.head()
model.fit(x_train, y_train, epochs = 600, batch_size = 32 )<compute_test_metric>
guess_df = np.zeros(( 3,2))
guess_df
score = model.evaluate(x_train, y_train)
 print('
train loss is: ' + str(score[0].round(4)))
 print('train accuracy is: ' + str(score[1]))
score = model.evaluate(x_val, y_val)
 print('
test loss is: ' + str(score[0].round(4)))
 print('test accuracy is: ' + str(score[1]))<load_from_csv>
for df in all_df:
for i in range(0,3):
for j in range(0,2):
guess = df[(df['Pclass']==i+1)&(df['Sex']==j)]['Age'].dropna()
guess_df[i,j] = guess.median()
for i in range(0,3):
for j in range(0,2):
df.loc[(df['Age'].isna())&(df['Pclass']==i+1)&(df['Sex']==j), 'Age'] = guess_df[i,j]
df['Age'] = df['Age'].dropna().astype(int)
 train_df.head()
test = pd.read_csv('.. /input/cars-final-prediction.csv')
 test_new = pd.get_dummies(test.drop(['car.id'], axis=1))
preds = model.predict_classes(test_new)
 keras_dict = {0: 'acc', 1: 'good', 2: 'unacc', 3: 'vgood'}
converted_preds = []
for prediction in preds:
converted_preds.append(keras_dict[prediction])
 test['class']=converted_preds
output_csv = test[['car.id', 'class']]
output_csv<save_to_csv>
for df in all_df:
df.loc[(df['Age'] <= 16), 'Age'] = 0
df.loc[(df['Age'] > 16)&(df['Age'] <= 32), 'Age'] = 1
df.loc[(df['Age'] > 32)&(df['Age'] <= 48), 'Age'] = 2
df.loc[(df['Age'] > 48)&(df['Age'] <= 64), 'Age'] = 3
df.loc[(df['Age'] > 64), 'Age'] = 4
train_df.head()
output_csv.to_csv('car-submission.csv', index=False )<install_modules>
train_df.groupby('Age', as_index=False)['Survived'].mean().sort_values(by='Age', ascending=True )
!pip install.. /input/kaggle-efficientnet-repo/efficientnet-1.0.0-py3-none-any.whl<set_options>
train_df.isna().sum()
%matplotlib inline
pd.set_option('max_rows', 1000)
 pd.set_option('max_columns', 1000 )<set_options>
train_df.drop(['AgeBand'], axis=1, inplace=True )
def seed_everything(seed=42):
os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 def get_strategy() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU {}'.format(tpu.cluster_spec().as_dict() ['worker']))
except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print('REPLICAS: {}'.format(strategy.num_replicas_in_sync))
return strategy<define_variables>
train_df.isna().sum()
INPUT_DIR = '.. /input/ailab-ml-training-1/'
ARTIFACT_DIR = '.. /input/v2-5-ailab1-training/'
PATH = {
'train': os.path.join(INPUT_DIR, 'train.csv'),
'submission': os.path.join(INPUT_DIR, 'sample_submission.csv'),
'train_image_dir': os.path.join(INPUT_DIR, 'train_images/train_images'),
'test_image_dir': os.path.join(INPUT_DIR, 'test_images/test_images'),
'f0_weight': os.path.join(ARTIFACT_DIR, 'f0_best_weight.h5'),
'f1_weight': os.path.join(ARTIFACT_DIR, 'f1_best_weight.h5'),
'f2_weight': os.path.join(ARTIFACT_DIR, 'f2_best_weight.h5'),
'f3_weight': os.path.join(ARTIFACT_DIR, 'f3_best_weight.h5'),
'f4_weight': os.path.join(ARTIFACT_DIR, 'f4_best_weight.h5'),
'gcs': 'v2-ailab1-tfrecord-dataset'
}
ID = 'fname'
TARGET = 'label'<define_variables>
test_df.isna().sum()
seed_everything(args.seed )<load_from_csv>
test_df['Fare'].fillna(test_df['Fare'].median() , inplace=True )
train_df = pd.read_csv(PATH['train'])
 submission_df = pd.read_csv(PATH['submission'] )<prepare_x_and_y>
for df in all_df:
df.loc[df['Fare'] <= 7.91, 'Fare'] = 0
df.loc[(df['Fare'] > 7.91)&(df['Fare'] <= 14.454), 'Fare'] = 1
df.loc[(df['Fare'] > 14.454)&(df['Fare'] <= 31.0), 'Fare'] = 2
df.loc[(df['Fare'] > 31.0), 'Fare'] = 3
df['Fare'] = df['Fare'].astype(int)
 train_df.head()
def normalize(image):
image -= tf.constant([0.485 * 255, 0.456 * 255, 0.406 * 255])
 image /= tf.constant([0.229 * 255, 0.224 * 255, 0.225 * 255])
 return image
def get_model(
 input_size=(224, 224, 3),
backbone='efficientnet-b0',
weights='imagenet',
n_classes=10, 
):
print(f'Using backbone {backbone} and weights {weights}')
 model_fn = getattr(efn, f'EfficientNetB{backbone[-1]}')
 x = L.Input(shape=input_size, dtype='float32')
 y = normalize(x)
 y = model_fn(input_shape=input_size, weights=weights, include_top=False )(y)
 y = L.GlobalAveragePooling2D()(y)
 y = L.Dense(n_classes, activation='softmax' )(y)
 model = tf.keras.Model(x, y)
 return model
def read_tfrecords(tfrec, input_size=(224, 224, 3)) :
formats = {
'fname': tf.io.FixedLenFeature([], tf.string),
'image': tf.io.FixedLenFeature([], tf.string),
'label': tf.io.FixedLenFeature([], tf.int64)
 }
tfrec = tf.io.parse_single_example(tfrec, formats)
 image = tf.image.decode_image(tfrec['image'])
 image = tf.reshape(image,(input_size[0], input_size[1], 1))
image = tf.repeat(image, repeats=3, axis=2)
 image = tf.cast(image, tf.float32)
 return tfrec['fname'], image
def get_test_dataset(tfrec_filenames, args, ordered=True):
AUTO = tf.data.experimental.AUTOTUNE
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
func_sample = [
lambda e: read_tfrecords(e, input_size=(args.imsize, args.imsize, 3)) ,
]
ds = tf.data.TFRecordDataset(tfrec_filenames, num_parallel_reads=AUTO)
 ds = ds.map(func_sample[0], num_parallel_calls=AUTO)
 return ds<define_variables>
train_df.drop('FareBand', axis=1, inplace=True )
oof = np.zeros(( len(train_df), args.n_classes))
predictions = np.zeros(( len(submission_df), args.n_classes))
train_fnames = train_df[ID].tolist()
submission_fnames = submission_df[ID].tolist()
gcs_path = KaggleDatasets().get_gcs_path(PATH['gcs'])
 for fold in range(args.n_splits):
if fold not in args.use_folds:
print(f'skip fold {fold} training, because not in {args.use_folds}')
 continue
strategy = get_strategy()
with strategy.scope() :
model = get_model(
 input_size=(args.imsize, args.imsize, 3),
backbone=args.backbone,
weights=None,
n_classes=args.n_classes 
)
 model.load_weights(PATH[f'f{fold}_weight'])
 print('load weight {}'.format(PATH[f'f{fold}_weight']))
tfrecs = tf.io.gfile.glob(os.path.join(gcs_path, f'records/train_f{fold}_*_*.tfrec'))
print(f'inference oof {tfrecs}')
 ds = get_test_dataset(tfrecs, args, ordered=True)
 num_val_samples = sum(int(re.findall(r'\d+', fname)[-1])for fname in tfrecs)
 ds_fnames = ds.map(lambda fname, image: fname ).batch(num_val_samples)
 ds_images = ds.map(lambda fname, image: image ).batch(args.batch_size)
 _fnames = [fn.decode('utf-8')for fn in next(iter(ds_fnames)).numpy().tolist() ]
indices = [train_fnames.index(fn)for fn in _fnames]
oof[indices] = model.predict(ds_images)
 tfrecs = tf.io.gfile.glob(os.path.join(gcs_path, f'records/test_f0_*_*.tfrec'))
print(f'inference test {tfrecs}')
 ds = get_test_dataset(tfrecs, args, ordered=True)
 ds_fnames = ds.map(lambda fname, image: fname ).batch(len(submission_df))
ds_images = ds.map(lambda fname, image: image ).batch(args.batch_size)
 _fnames = [fn.decode('utf-8')for fn in next(iter(ds_fnames)).numpy().tolist() ]
indices = [submission_fnames.index(fn)for fn in _fnames]
predictions[indices] += model.predict(ds_images)/ args.n_splits<compute_test_metric>
for df in all_df:
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
train_df.groupby('FamilySize', as_index=False)['Survived'].mean().sort_values('Survived', ascending=False )
score = accuracy_score(train_df[TARGET].values, np.argmax(oof, axis=1))
print(f'CV score: {score:.5f}' )<save_to_csv>
for df in all_df:
df['Family'] = 0
df.loc[(df['FamilySize'] >= 5), 'Family'] = 1
df.loc[(df['FamilySize'] > 1)&(df['FamilySize'] < 5), 'Family'] = 2
train_df.groupby('Family', as_index=False)['Survived'].mean().sort_values('Survived', ascending=False )
np.save('oof', oof)
 np.save('predictions', predictions)
 submission_df[TARGET] = np.argmax(predictions, axis=1 ).tolist()
submission_df.to_csv('submission.csv', index=False)
 FileLink('submission.csv' )<import_modules>
train_df.drop('FamilySize', axis=1, inplace=True)
 test_df.drop('FamilySize', axis=1, inplace=True )
print(os.listdir(".. /input")) 
<load_from_csv>
for df in all_df:
title = df['Name'].apply(lambda x:x.split(',')[1].split('.')[0] ).copy()
df['Title'] = title.str.strip()
train_df.groupby(['Title', 'Survived'])['PassengerId'].count().sort_values(ascending=False )
train_df = pd.read_csv('.. /input/train.csv' )<create_dataframe>
title_map = {"Mr": 0, "Miss": 1, "Mrs": 2, "Master": 3, "Rare": 4}
for df in all_df:
df['Title'] = df['Title'].map(title_map)
 df['Title'] = df['Title'].fillna(0)
 df['Title'] = df['Title'].astype(int)
 all_df = [train_df, test_df]
train_df.head()
train_data = []
for i, row in train_df.iterrows() :
for text in row['text'].split('.'):
train_data.append([row['president'], text])
 train_data = pd.DataFrame(train_data, columns=['president', 'text'] )<string_transform>
train_df.drop(['Name', 'Ticket'], axis=1, inplace=True)
 test_df.drop(['Name', 'Ticket'], axis=1, inplace=True )
def remove_punctuation_numbers(text):
punc_numbers = string.punctuation + '0123456789'
return ''.join([l for l in text if l not in punc_numbers] )<string_transform>
y_train = train_df['Survived']
features = ['Pclass', 'Sex', 'Age', 'Fare', 'Embarked', \
'Family', 'SibSp', 'Parch', 'Title', 'Cabin']
X_train = train_df[features]
X_test = test_df[features]
def tokeniser(text):
return TreebankWordTokenizer().tokenize(text )<not_enough_vertices>
forest = RandomForestClassifier(n_estimators=153, max_depth=5, random_state=1)
 forest.fit(X_train, y_train)
 y_pred = forest.predict(X_train)
 print('Accuracy Score: ', forest.score(X_train, y_train))
print('
Confusion Matric: ', confusion_matrix(y_train, y_pred))
print('
Classification Report: ', classification_report(y_train, y_pred))
<not_enough_vertices><EOS> 
predict = forest.predict(X_test)
 report = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': predict})
 report.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )