Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
train_dataset = pd.read_csv('./.. /input/train.csv')
 test_dataset = pd.read_csv('./.. /input/test.csv' )
def evaluate(tasks):
ts = TaskSolver()
result = []
predictions = []
for task in tqdm(tasks):
if input_output_shape_is_same(task):
ts.train(task['train'])
 pred = ts.predict(task['test'])
 score = calk_score(task['test'], pred)
 else:
pred = [el['input'] for el in task['test']]
score = [0]*len(task['test'])
 predictions.append(pred)
 result.append(score)
 return result, predictions<compute_test_metric>
print(train_dataset[["Parch", "Survived"]].groupby('Parch' ).mean().sort_values(by='Survived',ascending=False))
train_result, train_predictions = evaluate(train_tasks)
 train_solved = [any(score)for score in train_result]
total = sum([len(score)for score in train_result])
 print(f"solved : {sum(train_solved)} from {total}({sum(train_solved)/total})" )<compute_test_metric>
train_dataset.drop('Ticket',axis=1,inplace=True)
 test_dataset.drop('Ticket', axis=1,inplace=True )
evaluation_result, evaluation_predictions = evaluate(evaluation_tasks)
 evaluation_solved = [any(score)for score in evaluation_result]
total = sum([len(score)for score in evaluation_result])
 print(f"solved : {sum(evaluation_solved)} from {total}({sum(evaluation_solved)/total})" )<predict_on_test>
train_dataset['Title'] = train_dataset.Name.str.extract('([A-Za-z]+)\.')
 train_dataset['Title'].replace(['Lady','Countess','Capt','Col','Don','Dr','Major','Rev','Sir','Jonkheer','Dona'],'Rare',inplace=True)
 test_dataset['Title'] = test_dataset.Name.str.extract('([A-Za-z]+)\.')
 test_dataset['Title'].replace(['Lady','Countess','Capt','Col','Don','Dr','Major','Rev','Sir','Jonkheer','Dona'],'Rare',inplace=True)
 train_dataset.groupby('Title' ).sum() ['Survived']
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred
def make_pediction(tasks):
ts = TaskSolver()
result = pd.Series()
for idx, task in tqdm(test_tasks.iteritems()):
if input_output_shape_is_same(task):
ts.train(task['train'])
 pred = ts.predict(task['test'])
 else:
pred = [el['input'] for el in task['test']]
for i, p in enumerate(pred):
result[f'{idx}_{i}'] = flattener(np.array(p ).tolist())
 return result<prepare_output>
train_dataset['Title'] = train_dataset['Title'].replace('Mlle','Miss')
 train_dataset['Title'] = train_dataset['Title'].replace('Mme','Miss')
 train_dataset['Title'] = train_dataset['Title'].replace('Ms','Miss')
 test_dataset['Title'] = test_dataset['Title'].replace('Mlle','Miss')
 test_dataset['Title'] = test_dataset['Title'].replace('Mme','Miss')
 test_dataset['Title'] = test_dataset['Title'].replace('Ms','Miss')
 train_dataset.groupby('Title' ).sum() ['Survived']
submission = make_pediction(test_tasks)
 submission.head()<save_to_csv>
train_dataset['Embarked'].value_counts()
submission = submission.reset_index()
submission.columns = ['output_id', 'output']
submission.to_csv('submission.csv', index=False)
 submission<import_modules>
train_dataset[train_dataset['Embarked'].isnull() ]
import numpy as np
import pandas as pd
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np<categorify>
train_dataset[(train_dataset['Pclass'] == 1)&(train_dataset['Survived']==1)].mean() ['Fare']
def model_1(input, task_id=''):
input = np.asarray(input)
 if input.shape !=(2,2):
return input.tolist()
output = np.zeros(( 6,6))
output[0,:] = np.array(input[0,:].tolist() *3)
 output[1,:] = np.array(input[1,:].tolist() *3)
 output[2,:] = np.array(input[0,:].tolist() [::-1]*3)
 output[3,:] = np.array(input[1,:].tolist() [::-1]*3)
 output[4,:] = np.array(input[0,:].tolist() *3)
 output[5,:] = np.array(input[1,:].tolist() *3)
 print('model1', task_id)
 return output.astype(int ).tolist()
def model_2(task, task_id=''):
train_data = task['train']
test_data = task['test']
for data in train_data:
input = data['input']
output = data['output']
if np.asarray(input ).shape !=np.asarray(output ).shape :
return input
color_map = dict()
for item in train_data:
input = item['input']
output = item['output']
for x,y in zip(input[0],output[0]):
color_map[x] = y
for data in test_data:
input = data['input']
output = np.array(input, dtype='int')
 for i in range(output.shape[0]):
for j in range(output.shape[1]):
output[i,j] = color_map.get(output[i,j], 0)
 print(task_id, 'model2 predict')
 return output.tolist() 
<define_variables>
train_dataset.groupby(['Embarked','Pclass'] ).mean()
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'<create_dataframe>
train_dataset['Embarked'] = train_dataset['Embarked'].fillna('C')
 train_dataset['Embarked'].isnull().sum()
ret = []
for file in training_tasks:
task = load_task(file, 0)
 x = task['train'][0]['input']
x = np.asarray(x)
 ret.append(( file, str(x.shape)))
 df_shape = pd.DataFrame(ret, columns=['filename','shape'] )<count_values>
train_dataset.drop('Cabin',axis=1,inplace=True)
 test_dataset.drop('Cabin',axis=1,inplace=True )
df_shape['shape'].value_counts()<filter>
train_dataset['Age'].isnull().sum()
file1 = df_shape[df_shape['shape']=='(2, 2)']
display(file1 )<filter>
train_dataset['Ageband'] = pd.cut(train_dataset['Age'], 5)
 train_dataset.head()
file1 = df_shape[df_shape['shape']=='(3, 3)']
display(file1 )<prepare_output>
test_dataset['Ageband'] = pd.cut(test_dataset['Age'], 5)
 test_dataset.head()
pred = model_2(task)
 print(pred)
 print(task['test'][0]['output'] )<load_from_csv>
print(train_dataset['Ageband'].value_counts())
 print("---"*40)
 print(test_dataset['Ageband'].value_counts() )
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )<categorify>
train_dataset.loc[train_dataset['Age']<= 16,'Age']=0
train_dataset.loc[(train_dataset['Age']>16)&(train_dataset['Age']<=32),'Age']=1
train_dataset.loc[(train_dataset['Age']>32)&(train_dataset['Age']<=48),'Age']=2
train_dataset.loc[(train_dataset['Age']>48)&(train_dataset['Age']<=64),'Age']=3
train_dataset.loc[(train_dataset['Age']>64),'Age']=4
train_dataset.head()
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred<concatenate>
train_dataset.Age = train_dataset.Age.astype(int )
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))<load_pretrained>
test_dataset.loc[test_dataset['Age']<= 16,'Age']=0
test_dataset.loc[(test_dataset['Age']>16)&(test_dataset['Age']<=32),'Age']=1
test_dataset.loc[(test_dataset['Age']>32)&(test_dataset['Age']<=48),'Age']=2
test_dataset.loc[(test_dataset['Age']>48)&(test_dataset['Age']<=64),'Age']=3
test_dataset.loc[(test_dataset['Age']>64),'Age']=4
test_dataset.head()
for output_id in submission.index:
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 data = task['test'][pair_id]['input']
pred_1 = model_1(data, task_id)
 pred_1 = flattener(pred_1)
 data = model_2(task, task_id)
 pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred 
<save_to_csv>
test_dataset.Age = test_dataset.Age.astype(int )
submission.to_csv('submission.csv' )<install_modules>
train_dataset['Family']= train_dataset['SibSp']+train_dataset['Parch']+1
train_dataset.head()
!pip install --no-deps '.. /input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null
!pip install --no-deps '.. /input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl' > /dev/null<define_variables>
test_dataset['Family']= test_dataset['SibSp']+test_dataset['Parch']+1
test_dataset.head()
sys.path.insert(0, ".. /input/effdet")
 sys.path.insert(0, ".. /input/omegaconf")
 sys.path.insert(0, ".. /input/weightedboxesfusion")
 model_path='.. /input/model6/best.bin'<set_options>
train_dataset['IsAlone']=0
train_dataset.loc[train_dataset.Family == 1,'IsAlone']=1
test_dataset['IsAlone']=0
test_dataset.loc[test_dataset.Family == 1,'IsAlone']=1
SEED = 42
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(SEED )<load_from_csv>
test_dataset['Fareband'] = pd.qcut(test_dataset['Fare'],4)
 test_dataset.head(10 )
marking = pd.read_csv('.. /input/global-wheat-detection/train.csv')
 bboxs = np.stack(marking['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=',')))
 for i, column in enumerate(['x', 'y', 'w', 'h']):
marking[column] = bboxs[:,i]
marking.drop(columns=['bbox'], inplace=True )<init_hyperparams>
print(train_dataset['Fareband'].value_counts())
 print("---"*40)
 print(test_dataset['Fareband'].value_counts() )
warnings.filterwarnings("ignore")
 class Fitter:
def __init__(self, model, device, config):
self.config = config
self.epoch = 0
self.base_dir = f'./{config.folder}'
if not os.path.exists(self.base_dir):
os.makedirs(self.base_dir)
 self.log_path = f'{self.base_dir}/log.txt'
self.best_summary_loss = 10**5
self.model = model
self.device = device
param_optimizer = list(self.model.named_parameters())
 no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.001},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
self.optimizer = torch.optim.AdamW(self.model.parameters() , lr=config.lr)
 self.scheduler = config.SchedulerClass(self.optimizer, **config.scheduler_params)
 self.log(f'Fitter prepared.Device is {self.device}')
 def fit(self, train_loader, validation_loader):
for e in range(self.config.n_epochs):
if self.config.verbose:
lr = self.optimizer.param_groups[0]['lr']
timestamp = datetime.utcnow().isoformat()
self.log(f'
{timestamp}
LR: {lr}')
 t = time.time()
summary_loss = self.train_one_epoch(train_loader)
 self.log(f'[RESULT]: Train.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')
 self.save(f'{self.base_dir}/last-checkpoint1.bin')
 t = time.time()
summary_loss = self.validation(validation_loader)
 self.log(f'[RESULT]: Val.Epoch: {self.epoch}, summary_loss: {summary_loss.avg:.5f}, time: {(time.time() - t):.5f}')
 if summary_loss.avg < self.best_summary_loss:
self.best_summary_loss = summary_loss.avg
self.model.eval()
self.save(f'{self.base_dir}/best-checkpoint-{str(self.epoch ).zfill(3)}epoch.bin')
 for path in sorted(glob(f'{self.base_dir}/best-checkpoint-*epoch.bin')) [:-3]:
os.remove(path)
 if self.config.validation_scheduler:
self.scheduler.step(metrics=summary_loss.avg)
 self.epoch += 1
def validation(self, val_loader):
self.model.eval()
summary_loss = AverageMeter()
t = time.time()
for step,(images, targets, image_ids)in enumerate(val_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Val Step {step}/{len(val_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 with torch.no_grad() :
images = torch.stack(images)
 batch_size = images.shape[0]
images = images.to(self.device ).float()
boxes = [target['boxes'].to(self.device ).float() for target in targets]
labels = [target['labels'].to(self.device ).float() for target in targets]
loss, _, _ = self.model(images, boxes, labels)
 summary_loss.update(loss.detach().item() , batch_size)
 return summary_loss
def train_one_epoch(self, train_loader):
self.model.train()
summary_loss = AverageMeter()
t = time.time()
for step,(images, targets, image_ids)in enumerate(train_loader):
if self.config.verbose:
if step % self.config.verbose_step == 0:
print(
 f'Train Step {step}/{len(train_loader)}, ' + \
f'summary_loss: {summary_loss.avg:.5f}, ' + \
f'time: {(time.time() - t):.5f}', end='\r' 
)
 images = torch.stack(images)
 images = images.to(self.device ).float()
batch_size = images.shape[0]
boxes = [target['boxes'].to(self.device ).float() for target in targets]
labels = [target['labels'].to(self.device ).float() for target in targets]
self.optimizer.zero_grad()
loss, _, _ = self.model(images, boxes, labels)
 loss.backward()
summary_loss.update(loss.detach().item() , batch_size)
 self.optimizer.step()
if self.config.step_scheduler:
self.scheduler.step()
return summary_loss
def save(self, path):
self.model.eval()
torch.save({
'model_state_dict': self.model.model.state_dict() ,
'optimizer_state_dict': self.optimizer.state_dict() ,
'scheduler_state_dict': self.scheduler.state_dict() ,
'best_summary_loss': self.best_summary_loss,
'epoch': self.epoch,
}, path)
 def load(self, path):
checkpoint = torch.load(path)
 self.model.model.load_state_dict(checkpoint['model_state_dict'])
 self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
 self.best_summary_loss = checkpoint['best_summary_loss']
self.epoch = checkpoint['epoch'] + 1
def log(self, message):
if self.config.verbose:
print(message)
 with open(self.log_path, 'a+')as logger:
logger.write(f'{message}
' )<init_hyperparams>
train_dataset.loc[train_dataset['Fare']<=7.91,'Fare']=0
train_dataset.loc[(train_dataset['Fare']>7.91)&(train_dataset['Fare']<=14.454),'Fare']=1
train_dataset.loc[(train_dataset['Fare']>14.454)&(train_dataset['Fare']<=31),'Fare']=2
train_dataset.loc[(train_dataset['Fare']>31),'Fare']=3
train_dataset.Fare = train_dataset.Fare.astype(int)
 test_dataset.Fare = test_dataset.Fare.fillna(test_dataset.Fare.mean())
 test_dataset.loc[test_dataset['Fare']<=7.91,'Fare']=0
test_dataset.loc[(test_dataset['Fare']>7.91)&(test_dataset['Fare']<=14.454),'Fare']=1
test_dataset.loc[(test_dataset['Fare']>14.454)&(test_dataset['Fare']<=31),'Fare']=2
test_dataset.loc[(test_dataset['Fare']>31),'Fare']=3
test_dataset.Fare = test_dataset.Fare.astype(int)
class TrainGlobalConfig:
num_workers = 2
batch_size = 5
n_epochs = 2
lr = 0.0001
folder = 'plabel_model'
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08
)<data_type_conversions>
train_dataset.columns
test_dataset.columns
train_dataset.drop(['Name','SibSp','Parch','Ageband','Family','Fareband'],axis=1,inplace=True)
 test_dataset.drop(['Name','SibSp','Parch','Ageband','Family','Fareband'],axis=1,inplace=True )
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<load_pretrained>
train_pid = train_dataset['PassengerId']
train_dataset.drop('PassengerId',axis=1,inplace=True)
 test_pid = test_dataset['PassengerId']
test_dataset.drop('PassengerId',axis=1,inplace=True )
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_test_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=1,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)<choose_model_class>
label_encoder = preprocessing.LabelEncoder()
train_dataset['Title'] = label_encoder.fit_transform(train_dataset['Title'])
 train_dataset['Sex'] = label_encoder.fit_transform(train_dataset['Sex'])
 train_dataset['Embarked'] = label_encoder.fit_transform(train_dataset['Embarked'] )
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
net = load_net(model_path )<categorify>
label_encoder = preprocessing.LabelEncoder()
test_dataset['Title'] = label_encoder.fit_transform(test_dataset['Title'])
 test_dataset['Sex'] = label_encoder.fit_transform(test_dataset['Sex'])
 test_dataset['Embarked'] = label_encoder.fit_transform(test_dataset['Embarked'])
 test_dataset.head()
class BaseWheatTTA:

 image_size = 512
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(1)
 def batch_augment(self, images):
return images.flip(2)
 def deaugment_boxes(self, boxes):
boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(2)
 def batch_augment(self, images):
return images.flip(3)
 def deaugment_boxes(self, boxes):
boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 1,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = self.image_size - boxes[:, [1,3]]
res_boxes[:, [1,3]] = boxes[:, [2,0]]
return res_boxes
class TTACompose(BaseWheatTTA):

 def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
 return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
 return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)
 result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)
 result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)
 result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)
 return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
 return self.prepare_boxes(boxes )<concatenate>
X = train_dataset.drop('Survived',axis=1)
 Y = train_dataset['Survived']
Z = test_dataset
tta_transforms = []
for tta_combination in product([TTAHorizontalFlip() , None],
[TTAVerticalFlip() , None],
[TTARotate90() , None]):
tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform]))<find_best_model_class>
X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.33,random_state=0 )
def make_tta_predictions(images, score_threshold=0.25):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.45, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
svc = SVC(C=4,gamma='auto')
 svc.fit(X_train, Y_train)
 y_svc = svc.predict(X_test)
 acc_svc = round(svc.score(X_train, Y_train)* 100, 2)
 print("accuracy score is = {0}
f-score is = {1}".format(acc_svc,f1_score(Y_test,y_svc)))
 sns.heatmap(confusion_matrix(Y_test,y_svc),annot=True,fmt='2.0f' )
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<categorify>
accuracy_kscore = []
for k in range(3,10):
knn = KNeighborsClassifier(n_neighbors = k)
 knn.fit(X_train, Y_train)
 Y_pred = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 accuracy_kscore.append([k,acc_knn])
 acc_knndf = pd.DataFrame(data=accuracy_kscore,columns=['k','accuracy'])
 sns.barplot(x=acc_knndf['k'],y=acc_knndf['accuracy'],data=acc_knndf)
 acc_knndf.sort_values(by='accuracy',ascending=False)
results_plabel = []
for images, image_ids in data_loader:
predictions = make_tta_predictions(images)
 for i, image in enumerate(images):
image_id = image_ids[i]
image_ = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 h,w,_ = np.shape(image_)
 boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
for box in boxes:
result_p = {
'image_id': image_id,
'width':w,
'height':h,
'source':'usask_1',
'x':box[0],
'y':box[1],
'w':box[2],
'h':box[3],
}
results_plabel.append(result_p )<prepare_output>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, Y_train)
 Y_knn = knn.predict(X_test)
 acc_knn = round(knn.score(X_train, Y_train)* 100, 2)
 print("accuracy score is = {0}
f-score is = {1}".format(acc_knn,f1_score(Y_test,Y_knn)))
 sns.heatmap(confusion_matrix(Y_test,Y_knn),annot=True,fmt='2.0f' )
results_df = pd.DataFrame(results_plabel, columns=['image_id', 'width','height','source','x','y','w','h'])
 results_df.head()<feature_engineering>
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_gauss = gaussian.predict(X_test)
 acc_gaussian = round(gaussian.score(X_train, Y_train)* 100, 2)
 acc_gaussian
results_df['image_id'] = results_df['image_id'].apply(lambda x: DATA_ROOT_PATH+'/'+ x+'.jpg' )<feature_engineering>
decision_tree = DecisionTreeClassifier(max_depth=10)
 decision_tree.fit(X_train, Y_train)
 Y_decision = decision_tree.predict(X_test)
 acc_decision_tree = round(decision_tree.score(X_train, Y_train)* 100, 2)
 acc_decision_tree
TRAIN_ROOT_PATH = '.. /input/global-wheat-detection/train'
marking['image_id'] = marking['image_id'].apply(lambda x: TRAIN_ROOT_PATH+'/'+ x+'.jpg' )<concatenate>
random_forest = RandomForestClassifier(n_estimators=400)
 random_forest.fit(X_train, Y_train)
 Y_random = random_forest.predict(X_test)
 Y_Pred = random_forest.predict(Z)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest
train_data_plabel = pd.concat([results_df,marking], axis=0 )<feature_engineering>
kfold = KFold(n_splits=10,random_state=10)
 classfy_mean = [];
accuracy_kfold = []
classifier = ['Logistic regression','SVC','KNN','Gaussian NB','Decision Tree','Random Forest']
models = [LogisticRegression() ,SVC(C=4,gamma='auto'),KNeighborsClassifier(n_neighbors=3),GaussianNB() ,
DecisionTreeClassifier(max_depth=10),RandomForestClassifier(n_estimators=100)]
for i in models:
model = i
cv_result = cross_val_score(model,X,Y,cv=kfold,scoring='accuracy')
 accuracy_kfold.append(cv_result)
 classfy_mean.append(cv_result.mean())
 classfy_mean
df = pd.DataFrame(data=classfy_mean,index=classifier,columns=['cross-accuracy'])
 df.sort_values(by='cross-accuracy',ascending=False )
<import_modules><EOS> 
submission = pd.DataFrame({
"PassengerId": test_pid,
"Survived": Y_Pred
})
 submission
submission.to_csv("titanic_submission.csv", index=False )