Kaggle competition: Titanic - Machine Learning from Disaster
preds,y = learn.TTA(ds_type=DatasetType.Test )<predict_on_test>
!jupyter nbextension enable --py widgetsnbextension
test_predictions = optR.predict(preds, coefficients )<data_type_conversions>
data = pd.read_csv("/kaggle/input/titanic/train.csv")
 data.head(5 )
sample_df.diagnosis = test_predictions.astype(int)
 sample_df.head()<save_to_csv>
data.groupby('Sex')['Survived'].mean()
sample_df.to_csv('submission.csv',index=False )<set_options>
data.groupby(['Pclass', 'Sex'])['Survived'].mean()
%pylab inline 
<import_modules>
data['Initial']=0
for i in data:
data['Initial']=data.Name.str.extract('([A-Za-z]+)\.')
 pd.crosstab(data.Initial,data.Sex ).T.style.background_gradient(cmap='summer_r' )
from sklearn.preprocessing import StandardScaler
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder<import_modules>
data['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'],inplace=True)
 data.groupby('Initial')['Age'].mean()
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical<import_modules>
data.loc[(data.Age.isnull())&(data.Initial=='Mr'),'Age']=33
data.loc[(data.Age.isnull())&(data.Initial=='Mrs'),'Age']=36
data.loc[(data.Age.isnull())&(data.Initial=='Master'),'Age']=5
data.loc[(data.Age.isnull())&(data.Initial=='Miss'),'Age']=22
data.loc[(data.Age.isnull())&(data.Initial=='Other'),'Age']=46
data.Age.isnull().any()
print(sys.version )<import_modules>
data['Embarked'] = data['Embarked'].fillna('S' )
pd.__version__<set_options>
data['Age_band']=0
data.loc[(data['Age']>16)&(data['Age']<=32),'Age_band']=1
data.loc[(data['Age']>32)&(data['Age']<=48),'Age_band']=2
data.loc[(data['Age']>48),'Age_band']=3
rcParams['figure.figsize'] = 8,8<load_from_csv>
data['FamilySize'] = data['SibSp'] + data['Parch'] + 1
data['IsAlone'] = 1
data['IsAlone'].loc[data['FamilySize'] > 1] = 0
data = pd.read_csv('.. /input/train.csv')
 parent_data = data.copy()
ID = data.pop('id' )<categorify>
data['Sex'] = data['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 data['Embarked'] = data['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int )
y = data.pop('species')
 y = LabelEncoder().fit(y ).transform(y)
 print(y.shape )<normalization>
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import GridSearchCV
X = StandardScaler().fit(data ).transform(data)
 print(X.shape )<categorify>
train,val=train_test_split(data,test_size=0.3,random_state=42,stratify=data['Survived'])
 train_X=train[train.columns[1:]]
train_Y=train[train.columns[:1]]
val_X=val[val.columns[1:]]
val_Y=val[val.columns[:1]]
y_cat = to_categorical(y)
 print(y_cat.shape )<choose_model_class>
model = LogisticRegression()
model.fit(train_X,train_Y)
 prediction=model.predict(val_X)
 print('The accuracy of the Logistic Regression is',metrics.accuracy_score(prediction,val_Y))
model = Sequential()
model.add(Dense(2048,input_dim=192, init='uniform', activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1024, activation='sigmoid'))
model.add(Dropout(0.3))
model.add(Dense(99, activation='softmax'))<choose_model_class>
X=data[data.columns[1:]]
Y=data['Survived']
kfold = KFold(n_splits=10, random_state=22 )
model.compile(loss='categorical_crossentropy',optimizer='Adamax', metrics = ["accuracy"] )<train_model>
logistic_cv_result = cross_val_score(LogisticRegression() ,X,Y, cv = kfold,scoring = "accuracy")
 print('The mean accuracy of the Logistic Regression under 10-fold validation is: ', np.mean(logistic_cv_result),
'std is: ', np.std(logistic_cv_result))
start = time.time()
history = model.fit(X,y_cat,batch_size=100,
nb_epoch=125,verbose=0, validation_split=0.1)
 end = time.time()
print('runtime: ',"%.3f" %(end-start),' [sec]' )<train_model>
tree_cv_result = cross_val_score(DecisionTreeClassifier() ,X,Y, cv = kfold,scoring = "accuracy")
 print('The mean accuracy of the decision tree under 10-fold validation is: ', np.mean(tree_cv_result),
'std is: ', np.std(tree_cv_result))
print('---------------------------------------')
 print('acc: ',max(history.history['acc']))
print('loss: ',min(history.history['loss']))
print('---------------------------------------')
 print('val_acc: ',max(history.history['val_acc']))
print('val_loss: ',min(history.history['val_loss']))<load_from_csv>
forest_cv_result = cross_val_score(RandomForestClassifier(n_estimators=100),X,Y, cv = kfold,scoring = "accuracy")
 print('The mean accuracy of the random forest under 10-fold validation is: ', np.mean(forest_cv_result),
'std is: ', np.std(forest_cv_result))
test = pd.read_csv('.. /input/test.csv')
 index = test.pop('id')
 test = StandardScaler().fit(test ).transform(test)
 yPred = model.predict_proba(test )<create_dataframe>
forest = RandomForestClassifier(n_estimators=100, min_samples_leaf=1, min_samples_split=10)
 forest.fit(train_X,train_Y)
 prediction=forest.predict(val_X)
 print('The accuracy is',metrics.accuracy_score(prediction,val_Y))
yPred = pd.DataFrame(yPred,index=index,columns=sort(parent_data.species.unique()))<save_to_csv>
import xgboost as xgb
fp = open('submission_nn_kernel.csv','w')
 fp.write(yPred.to_csv() )<define_variables>
gbm = xgb.XGBClassifier(
 n_estimators= 2000,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
scale_pos_weight=1 ).fit(train_X, train_Y)
 predictions = gbm.predict(val_X)
 print('The accuracy of XG Boost is',metrics.accuracy_score(predictions,val_Y))
DEBUG = False<define_variables>
from catboost import CatBoostClassifier, Pool, cv
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<import_modules>
import skimage.io
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from efficientnet_pytorch import model as enet
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm 
<load_from_csv>
cat = CatBoostClassifier(
 l2_leaf_reg=1,
learning_rate=0.003842420425736234,
iterations=500,
eval_metric='Accuracy',
random_seed=42,
verbose=False,
loss_function='Logloss', 
)
 cv_data = cv(Pool(X, Y, cat_features=range(train_X.shape[1])) , cat.get_params() )
data_dir = '.. /input/prostate-cancer-grade-assessment'
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
model_dir = '.. /input/panda-public-models'
image_folder = os.path.join(data_dir, 'test_images')
 is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else os.path.join(data_dir, 'train_images')
 df = df_test if is_test else df_train.loc[:100]
tile_size = 256
image_size = 256
n_tiles = 36
batch_size = 8
num_workers = 4
device = torch.device('cuda')
 print(image_folder )<define_search_model>
print('Precise validation accuracy score: {}'.format(np.max(cv_data['test-Accuracy-mean'])) )
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b0'
model = enetv2(backbone, out_dim=5)
 model.load_state_dict(torch.load(model_f, map_location=lambda storage, loc: storage), strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'cls_effnet_b0_Rand36r36tiles256_big_bce_lr0.3_augx2_30epo_model_fold0.pth'
]
models = load_models(model_files )<categorify>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
def get_tiles(img, mode=0):
result = []
h, w, c = img.shape
pad_h =(tile_size - h % tile_size)% tile_size +(( tile_size * mode)// 2)
 pad_w =(tile_size - w % tile_size)% tile_size +(( tile_size * mode)// 2)
 img2 = np.pad(img,[[pad_h // 2, pad_h - pad_h // 2], [pad_w // 2,pad_w - pad_w//2], [0,0]], constant_values=255)
 img3 = img2.reshape(
 img2.shape[0] // tile_size,
tile_size,
img2.shape[1] // tile_size,
tile_size,
3 
)
 img3 = img3.transpose(0,2,1,3,4 ).reshape(-1, tile_size, tile_size,3)
 n_tiles_with_info =(img3.reshape(img3.shape[0],-1 ).sum(1)< tile_size ** 2 * 3 * 255 ).sum()
if len(img)< n_tiles:
img3 = np.pad(img3,[[0,N-len(img3)],[0,0],[0,0],[0,0]], constant_values=255)
 idxs = np.argsort(img3.reshape(img3.shape[0],-1 ).sum(-1)) [:n_tiles]
img3 = img3[idxs]
for i in range(len(img3)) :
result.append({'img':img3[i], 'idx':i})
 return result, n_tiles_with_info >= n_tiles
class PANDADataset(Dataset):
def __init__(self,
df,
image_size,
n_tiles=n_tiles,
tile_mode=0,
rand=False,
sub_imgs=False 
):
self.df = df.reset_index(drop=True)
 self.image_size = image_size
self.n_tiles = n_tiles
self.tile_mode = tile_mode
self.rand = rand
self.sub_imgs = sub_imgs
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index):
row = self.df.iloc[index]
img_id = row.image_id
tiff_file = os.path.join(image_folder, f'{img_id}.tiff')
 image = skimage.io.MultiImage(tiff_file)[1]
tiles, OK = get_tiles(image, self.tile_mode)
 if self.rand:
idxes = np.random.choice(list(range(self.n_tiles)) , self.n_tiles, replace=False)
 else:
idxes = list(range(self.n_tiles))
idxes = np.asarray(idxes)+ self.n_tiles if self.sub_imgs else idxes
n_row_tiles = int(np.sqrt(self.n_tiles))
images = np.zeros(( image_size * n_row_tiles, image_size * n_row_tiles, 3))
for h in range(n_row_tiles):
for w in range(n_row_tiles):
i = h * n_row_tiles + w
if len(tiles)> idxes[i]:
this_img = tiles[idxes[i]]['img']
else:
this_img = np.ones(( self.image_size, self.image_size, 3)).astype(np.uint8)* 255
this_img = 255 - this_img
h1 = h * image_size
w1 = w * image_size
images[h1:h1+image_size, w1:w1+image_size] = this_img
images = images.astype(np.float32)
 images /= 255
images = images.transpose(2, 0, 1)
 return torch.tensor(images)
<load_pretrained>
test_data['Sex'] = test_data['Sex'].map({'female': 0, 'male': 1} ).astype(int)
 test_data['Embarked'] = test_data['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test_data['Age_band']=0
test_data.loc[(test_data['Age']>16)&(test_data['Age']<=32),'Age_band']=1
test_data.loc[(test_data['Age']>32)&(test_data['Age']<=48),'Age_band']=2
test_data.loc[(test_data['Age']>48),'Age_band']=3
test_data['FamilySize'] = test_data['SibSp'] + test_data['Parch'] + 1
test_data['IsAlone'] = 1
test_data['IsAlone'].loc[test_data['FamilySize'] > 1] = 0
dataset = PANDADataset(df, image_size, n_tiles, 0)
 loader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False)
 dataset2 = PANDADataset(df, image_size, n_tiles, 2)
 loader2 = DataLoader(dataset2, batch_size=batch_size, num_workers=num_workers, shuffle=False )<save_to_csv>
features = ['Pclass', 'Sex', 'Embarked', 'Age_band', 'IsAlone']
X_test = pd.get_dummies(test_data[features])
LOGITS = []
LOGITS2 = []
with torch.no_grad() :
for data in tqdm(loader):
data = data.to(device)
 logits = models[0](data)
 LOGITS.append(logits)
 for data in tqdm(loader2):
data = data.to(device)
 logits = models[0](data)
 LOGITS2.append(logits)
 LOGITS =(torch.cat(LOGITS ).sigmoid().cpu() + torch.cat(LOGITS2 ).sigmoid().cpu())/ 2
PREDS = LOGITS.sum(1 ).round().numpy()
df['isup_grade'] = PREDS.astype(int)
 df[['image_id', 'isup_grade']].to_csv('submission.csv', index=False)
 print(df.head())
 print()
print(df.isup_grade.value_counts() )<define_variables>
DEBUG = False<define_variables>
cat = CatBoostClassifier(
 l2_leaf_reg=1,
learning_rate=0.003842420425736234,
iterations=500,
eval_metric='Accuracy',
random_seed=42,
verbose=False,
loss_function='Logloss', 
)
 forest = RandomForestClassifier(n_estimators=100, min_samples_leaf=1, min_samples_split=10 )
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<import_modules>
gbm = xgb.XGBClassifier(
 n_estimators= 2000,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
scale_pos_weight=1 )
import skimage.io
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from efficientnet_pytorch import model as enet
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm<load_from_csv>
from sklearn.ensemble import VotingClassifier
data_dir = '.. /input/prostate-cancer-grade-assessment'
df_train = pd.read_csv(os.path.join(data_dir, 'train.csv'))
df_test = pd.read_csv(os.path.join(data_dir, 'test.csv'))
df_sub = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
model_dir = '.. /input/ck-epoch6/'
image_folder = os.path.join(data_dir, 'test_images')
 is_test = os.path.exists(image_folder)
 image_folder = image_folder if is_test else os.path.join(data_dir, 'train_images')
 df = df_test if is_test else df_train.loc[:100]
tile_size = 256
image_size = 256
n_tiles = 36
batch_size = 2
num_workers = 4
device = torch.device('cuda')
 print(image_folder )<define_search_model>
votingC = VotingClassifier(estimators=[('rfc', forest),('xgb', gbm),('cat', cat)], voting='soft', n_jobs=4)
 votingC = votingC.fit(X, Y )
class enetv2(nn.Module):
def __init__(self, backbone, out_dim):
super(enetv2, self ).__init__()
self.enet = enet.EfficientNet.from_name(backbone)
 self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
 self.enet._fc = nn.Identity()
def extract(self, x):
return self.enet(x)
 def forward(self, x):
x = self.extract(x)
 x = self.myfc(x)
 return x
def load_models(model_files):
models = []
for model_f in model_files:
model_f = os.path.join(model_dir, model_f)
 backbone = 'efficientnet-b1'
model = enetv2(backbone, out_dim=5)
 ck = torch.load(model_f)
 model.load_state_dict(ck['model_state_dict'], strict=True)
 model.eval()
model.to(device)
 models.append(model)
 print(f'{model_f} loaded!')
 return models
model_files = [
'model_checkpoint_epoch6.pth'
]
models = load_models(model_files )<categorify>
predictions = votingC.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )
<load_pretrained><EOS>