Kaggle competition: Хакатон от Кафедры ТПСТС МФТИ (level 1)
train['FamilySize'] = train['SibSp'] + train['Parch']
test['FamilySize'] = test['SibSp'] + train['Parch']<drop_column>
ship_dir = '.. /input/almaz-antey-hackathon-l1/'
train_image_dir = os.path.join(ship_dir, 'train/train')
 test_image_dir = os.path.join(ship_dir, 'test/test' )
f_file=test['PassengerId']
train=train.drop(['Cabin','PassengerId','Name', 'Ticket', 'SibSp', 'Parch'],axis=1)
 test=test.drop(['Cabin','PassengerId','Name', 'Ticket', 'SibSp', 'Parch'],axis=1)
<data_type_conversions>
train_df = pd.read_csv(os.path.join(ship_dir, 'train_segmentation.csv'))
sample_sub = pd.read_csv(os.path.join(ship_dir, 'sample_submission.csv'))
train['Age'].fillna(train['Age'].mean() , inplace=True)
 train.dropna(inplace=True)
 test['Age'].fillna(test['Age'].mean() , inplace=True)
<categorify>
montage_rgb = lambda x: np.stack([montage(x[:, :, :, i])for i in range(x.shape[3])], -1)
 def multi_rle_encode(img, **kwargs):

 labels = label(img)
 if img.ndim > 2:
return [rle_encode(np.sum(labels==k, axis=2), **kwargs)for k in np.unique(labels[labels>0])]
else:
return [rle_encode(labels==k, **kwargs)for k in np.unique(labels[labels>0])]
def rle_encode(img, min_max_threshold=1e-4, max_mean_threshold=None):

 if np.max(img)< min_max_threshold:
return ''
if max_mean_threshold and np.mean(img)> max_mean_threshold:
return ''
pixels = img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs)
 def rle_decode(mask_rle, shape=(768, 768)) :

 s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape ).T
def masks_as_image(in_mask_list, **kwargs):
all_masks = np.zeros(kwargs['shape'], dtype = np.uint8)
 for mask in in_mask_list:
if isinstance(mask, str):
all_masks |= rle_decode(mask, **kwargs)
 return all_masks
def masks_as_color(in_mask_list, **kwargs):
all_masks = np.zeros(kwargs['shape'], dtype = np.float)
 scale = lambda x:(len(in_mask_list)+ x + 1)/(len(in_mask_list)* 2)
 for i,mask in enumerate(in_mask_list):
if isinstance(mask, str):
all_masks[:,:] += scale(i)* rle_decode(mask, **kwargs)
 return all_masks
my_label = LabelEncoder()
train['Sex'] = my_label.fit_transform(train['Sex'])
 train['Embarked'] = my_label.fit_transform(train['Embarked'])
 test['Sex'] = my_label.fit_transform(test['Sex'])
 test['Embarked'] = my_label.fit_transform(test['Embarked'])
 train.head()<split>
generator = ship_generator(train_df, train_image_dir, batch_size=4 )
train_X, val_X, train_y, val_y = train_test_split(train.drop(['Survived'], axis=1), train['Survived'] )<predict_on_test>
def dice_coef2(y_true, y_pred):
y_true_f = y_true.flatten()
y_pred_f = y_pred.flatten()
union = np.sum(y_true_f)+ np.sum(y_pred_f)
 if union==0: return 1
intersection = np.sum(y_true_f * y_pred_f)
 return 2.* intersection / union
rf = RandomForestClassifier(random_state=1)
 rf.fit(train_X, train_y)
 prediction = rf.predict(val_X)
 print(mean_absolute_error(val_y, prediction), accuracy_score(val_y, prediction))<compute_train_metric>
val_data = train_df.sample(200, replace=False ).reset_index()
val_data.EncodedPixels = val_data.EncodedPixels.map(lambda x: rle_decode(x))
XGB = XGBClassifier()
XGB.fit(train_X, train_y)
 prediction = XGB.predict(val_X)
 print(mean_absolute_error(val_y, prediction), accuracy_score(val_y, prediction))<correct_missing_values>
pbar = tqdm(val_data.index)
 predicts = []
for idx in pbar:
fpath = os.path.join(train_image_dir, val_data.iloc[idx].ImageId)
 image, mask = find_mask(fpath)
 predicts.append(mask )
test['Fare'].fillna(test['Fare'].mean() , inplace=True )<save_to_csv>
dice = np.mean([dice_coef2(y, predict)for y, predict in zip(val_data.EncodedPixels.to_numpy() , predicts)] )
vals = rf.predict(test)
 file = pd.DataFrame({'PassengerId':f_file, 'Survived':vals})
 file.to_csv('submission_rf.csv', index = False)
 file.head()<save_to_csv>
print(f'DICE = {dice}' )
vals = XGB.predict(test)
 file = pd.DataFrame({'PassengerId':f_file, 'Survived':vals})
 file.to_csv('submission_xgb.csv', index = False)
 file.head()<import_modules>
pbar = tqdm(sample_sub.index[:])
 for idx in pbar:
fpath = os.path.join(test_image_dir, sample_sub.iloc[idx].ImageId)
 image, mask = find_mask(fpath)
 encode_mask = rle_encode(mask)
 sample_sub.iloc[idx].EncodedPixels = encode_mask
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import seaborn as sns
from datetime import datetime<load_from_csv>
sample_sub.to_csv('submission.csv', index=False)
 sample_sub.head()