Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<choose_model_class>
%matplotlib inline
titanic_df = pd.read_csv('.. /input/titanic/train.csv')
 titanic_df.head(5 )
segmmodel = timm.create_model('mixnet_xl', pretrained=False)
 for param in segmmodel.parameters() :
param.requires_grad = False
segmmodel.classifier=nn.Linear(1536, 1)
 segmmodel.fc = nn.Linear(1536, 1 )<normalization>
titanic_df['Age'].fillna(titanic_df['Age'].mean() , inplace=True)
 titanic_df['Cabin'].fillna('N',inplace=True)
 titanic_df['Embarked'].fillna('N',inplace=True)
 titanic_df.isnull().sum()
checkpoint = torch.load(mixnet_pth, map_location=device)
 segmmodel.load_state_dict(checkpoint)
 segmmodel.eval()
segmmodel.cuda()
del checkpoint<load_pretrained>
print('Sex
','--------------
', titanic_df['Sex'].value_counts() ,'
')
 print('Cabin
','--------------
', titanic_df['Cabin'].value_counts() ,'
')
 print('Embarked
','--------------
', titanic_df['Embarked'].value_counts() ,'
' )
aknell_models = []
for model_index, path in enumerate(MODELS):
print("path",path)
 state_dict = torch.load(path,map_location=torch.device(device))
model = Model(arch=ensemble_list[model_index]['arch'])
 model.load_state_dict(state_dict)
 model.float()
model.eval()
model.cuda()
aknell_models.append(model)
 del state_dict<load_from_csv>
titanic_df['Cabin'] = titanic_df['Cabin'].str[:1]
print(titanic_df['Cabin'].value_counts() )
test=pd.read_csv(TEST)
 if chk:
pass
test=test[:][:nchk]<define_variables>
titanic_df.groupby(['Sex','Survived'])['Survived'].count()
test_image_dir='/kaggle/input/prostate-cancer-grade-assessment/test_images'
if os.path.exists(test_image_dir):
print('test set exist')
 chk=False
mode='test'
test_image_dir='/kaggle/input/prostate-cancer-grade-assessment/{}_images'.format(mode)
 csv_path='/kaggle/input/prostate-cancer-grade-assessment/{}.csv'.format(mode)
 else:
print('test set not exist')
 chk=True
mode='train'
test_image_dir='/kaggle/input/prostate-cancer-grade-assessment/{}_images'.format(mode)
 csv_path='/kaggle/input/prostate-cancer-grade-assessment/{}.csv'.format(mode)
 submission_path='/kaggle/input/prostate-cancer-grade-assessment/sample_submission.csv'
df=pd.read_csv(csv_path)
 if chk:
pass
df=df[:][:nchk]
mean_256 = [0.85841531, 0.78983734, 0.89534979]
std_256 = [0.16897951, 0.25758676, 0.13730845]
first_effb3_path_256 = '/kaggle/input/pandalayertest4wunertaintyacc/pandalayertest4wunertaintyacc/fold{}top{}.ckpt'
ensemble_shujun_1 ={"mean":mean_256,"std":std_256,"tileImageSize":256,"isCutThreshold":False,"arch":"resnext50_32x4d","path":first_effb3_path_256,"TILE_SIZE":36,"TILE_ALL":False,"CVT_COLOR":True}
mean_224= [0.81702482, 0.6299724, 0.75427603]
std_224=[0.40702703, 0.52099041, 0.42290275]
second_resnext50_path_224 = '/kaggle/input/pandaakselltest3/fold{}top{}.ckpt'
ensemble_shujun_2 ={"mean":mean_224,"std":std_224,"tileImageSize":224,"isCutThreshold":True,"arch":"resnext50_32x4d","path":second_resnext50_path_224,"TILE_SIZE":20,"TILE_ALL":False,"CVT_COLOR":True}
mean_224_30tile= [0.82514849, 0.64166622, 0.76170417]
std_224_30tile=[0.40444484, 0.52074353, 0.42308628]
third_resnext50_path_224 = '/kaggle/input/pandaakselltest4/fold{}top{}.ckpt'
ensemble_shujun_3 ={"mean":mean_224_30tile,"std":std_224_30tile,"tileImageSize":224,"isCutThreshold":True,"arch":"resnext50_32x4d","path":third_resnext50_path_224,"TILE_SIZE":30,"TILE_ALL":False,"CVT_COLOR":False}
mean_288 = mean_224
std_288 = std_224
fourth_resnext50_path_288 = '/kaggle/input/pandaakselltest5/fold{}top{}.ckpt'
ensemble_shujun_4 ={"mean":mean_288,"std":std_288,"tileImageSize":288,"isCutThreshold":True,"arch":"resnext50_32x4d","path":fourth_resnext50_path_288,"TILE_SIZE":20,"TILE_ALL":False,"CVT_COLOR":False}
mean_224_32= [0.82514849, 0.64166622, 0.76170417]
std_224_32=[0.40444484, 0.52074353, 0.42308628]
fifth_resnext50_path_224 = '/kaggle/input/pandaakselltest7/fold{}top{}.ckpt'
ensemble_shujun_5 ={"mean":mean_224_32,"std":std_224_32,"tileImageSize":224,"isCutThreshold":True,"arch":"resnext50_32x4d","path":fifth_resnext50_path_224,"TILE_SIZE":32,"TILE_ALL":True,"CVT_COLOR":True}
mean_256_64= [0.8021112084388733, 0.7040286064147949, 0.8552106022834778]
std_256_64=[0.18155023455619812, 0.27437373995780945, 0.15957728028297424]
sixth_resnext50_path_256 = '/kaggle/input/resnext50-new/fold{}top{}.ckpt'
ensemble_shujun_6 ={"mean":mean_256_64,"std":std_256_64,"tileImageSize":256,"isCutThreshold":False,"arch":"resnext50_32x4d","path":sixth_resnext50_path_256,"TILE_SIZE":64,"TILE_ALL":False,"CVT_COLOR":True}
ensemble_shujun_list = []
ensemble_shujun_list.append(ensemble_shujun_1)
 ensemble_shujun_list.append(ensemble_shujun_2)
 ensemble_shujun_list.append(ensemble_shujun_4)
 ensemble_shujun_list.append(ensemble_shujun_5)
 ensemble_shujun_list.append(ensemble_shujun_6 )<normalization>
def encode_feature(dataDF):
features = ['Cabin','Sex','Embarked']
for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(dataDF[feature])
 dataDF[feature] = le.transform(dataDF[feature])
 return dataDF
titanic_df = encode_feature(titanic_df)
 titanic_df.head()
class Mish(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x *(torch.tanh(F.softplus(x)))
 def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class MultiheadAttentionClassifier(nn.Module):
def __init__(self,num_classes,out_features,ninp,nhead,dropout,attention_dropout=0.1):
super(MultiheadAttentionClassifier, self ).__init__()
self.attention=nn.MultiheadAttention(ninp, nhead, dropout=attention_dropout)
 self.classifier=nn.Linear(ninp*2,num_classes)
 self.dropout=nn.Dropout(dropout)
 self.mish=Mish()
def forward(self,x):
x=x.permute(1,0,2)
 x,_=self.attention(x,x,x)
 x=self.mish(x)
 x=x.permute(1,0,2)
 max_x,_=torch.max(x,dim=1)
 x=torch.cat([torch.mean(x,dim=1),max_x],dim=-1)
 x=self.dropout(x)
 x=self.classifier(x)
 return x
def _resnext(url, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 return model
class Network(nn.Module):
def __init__(self,num_classes,arch='efficientnet-b0',dropout_p=0.5,nhead=8,ninp=512, nhid=512,attention_dropout=0.1):
super(Network, self ).__init__()
if debug:
print('arch',arch)
 if arch.startswith('efficient'):
self.backbone=EfficientNet.from_name(arch)
 elif arch == 'resnet34':
self.backbone=models.resnet34(pretrained=False,)
 elif arch == 'resnext50_32x4d':
self.backbone=models.resnext50_32x4d(pretrained=False)
 self.backbone.avgpool=GeM()
self.num_classes=num_classes
self.out_features = self.backbone.fc.in_features
self.ninp=ninp
self.backbone.fc=nn.Linear(self.out_features,ninp)
 self.regression_head=MultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 self.classifier_head=MultiheadAttentionClassifier(num_classes[0],self.out_features,ninp,nhead,dropout_p)
 self.regression_head2=MultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 self.regression_head3=MultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 def forward(self,x):
shape = x[0].shape
bs=x.shape[0]
n = len(x)
 x = x.reshape(( -1,shape[1],shape[2],shape[3]))
features=self.backbone(x)
 features=features.reshape(bs,shape[0],self.ninp)
 outputs=[self.regression_head(features ).squeeze() ,F.log_softmax(self.classifier_head(features),dim=1),
self.regression_head2(features ).squeeze() ,self.regression_head3(features ).squeeze() ]
return outputs
def tile_shujun(img, sz=224,N=20):
if debug:
print('tile_shujun')
 result = []
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz,(sz - shape[1]%sz)%sz
img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],
constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1)) [:N]
img = img[idxs]
return img
def tile_all(img, N, sz=256):
if debug:
print('tile_all_shujun')
 print('N',N)
 result = []
shape = img.shape
pad0,pad1 =(sz - shape[0]%sz)%sz,(sz - shape[1]%sz)%sz
img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],
constant_values=255)
 img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)
 img = img.transpose(0,2,1,3,4 ).reshape(-1,sz,sz,3)
 if len(img)< N:
img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)
 idxs = np.argsort(img.reshape(img.shape[0],-1 ).sum(-1))
return img, idxs
def standardize_batch(batch,mean,std,isCutThresholdTile):
if debug:
print("test:",batch.shape[1])
 if batch.shape[1]==64:
if debug:
print("st_01")
 batch=batch/255.0
batch=(batch-mean)/std
elif isCutThresholdTile:
if debug:
print("st_02")
 batch=batch/255.0
batch=(mean-batch)/std
else:
if debug:
print("st_03")
 batch=batch/255.0
return batch
def get_batch_of_image(paths, sz, N, phase = 0, index_df = None):
isCutThreshold = ensemble_shujun_list[phase]['isCutThreshold']
isTileAll = ensemble_shujun_list[phase]['TILE_ALL']
if debug:
print('sz',sz)
 print('N',N)
 print('isCutThreshold',isCutThreshold)
 images=[]
for path in paths:
if isCutThreshold:
tile_idx = index_df.loc[index_df['image_id']== path]['tile_idx'].tolist() [0]
tile_idx=[int(i)for i in tile_idx]
if debug:
print(tile_idx)
 path=os.path.join(test_image_dir,path+'.tiff')
 img = skimage.io.MultiImage(path,conserve_memory=False,dtype='tiff')[1]
if isCutThreshold:
if isTileAll:
tile_idx = tile_idx[:20]
if debug:
print(tile_idx)
 Max_Indices_N = max(tile_idx)+1
if debug:
print("Max_Indices_N",Max_Indices_N)
 img, idxs = tile_all(img, sz=sz, N=Max_Indices_N)
 tiles = []
for sorted_index in idxs:
if sorted_index not in tile_idx:
tiles.append(img[sorted_index])
 if len(tiles)>=N-20:
break
if len(tiles)< N-20:
tiles = np.pad(tiles,[[0,N-len(tiles)],[0,0],[0,0],[0,0]],constant_values=255)
 tiles = np.asarray(tiles)
 if debug:
print("before_concate_tiles_shape",tiles.shape)
 print("aksell_indices.shape",img[tile_idx].shape)
 tiles = np.concatenate([tiles,img[tile_idx]] ).astype("uint8")
 if debug:
print("tiles",tiles.shape)
 img = tiles
else:
if sz == 224:
img = tile(img,tile_idx, N=NN)
 else:
img = expand_tile(img,tile_idx, sz=224, N=NN, expand_sz = sz)
 else:
img = tile_shujun(img, sz=sz, N=N)
 if(ensemble_shujun_list[phase]['CVT_COLOR']==True):
if debug:
print("CVT_COLOR is True")
 for j in range(img.shape[0]):
img[j] = cv2.cvtColor(img[j],cv2.COLOR_RGB2BGR)
 else:
if debug:
print("CVT_COLOR is False")
 img=np.asarray(img,dtype='uint8')
 images.append(img)
 images=np.asarray(images,dtype='uint8' ).transpose(( 0,1,4,2,3))
return images
def classification_threshold(tensor):
for i in range(len(tensor)) :
if tensor[i] > 4.5:
tensor[i]=5
elif tensor[i] > 3.5:
tensor[i]=4
elif tensor[i] > 2.5:
tensor[i]=3
elif tensor[i] > 1.5:
tensor[i]=2
elif tensor[i] > 0.6:
tensor[i]=1
elif tensor[i] < 0.6:
tensor[i]=0
return tensor.astype('int')
 def classification_threshold_item(item):
if item > 4.5:
item=5
elif item > 3.5:
item=4
elif item > 2.5:
item=3
elif item > 1.5:
item=2
elif item > 0.6:
item=1
elif item < 0.6:
item=0
return int(item)
 def hflip(tensor):
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
tensor[i,j]=np.flip(tensor[i,j],axis=0)
 return tensor
def vflip(tensor):
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
tensor[i,j]=np.flip(tensor[i,j],axis=1)
 return tensor<categorify>
def fill_na(df):
df['Age'].fillna(titanic_df['Age'].mean() , inplace=True)
 df['Cabin'].fillna('N',inplace=True)
 df['Embarked'].fillna('N',inplace=True)
 df['Fare'].fillna(0, inplace=True)
 return df
def drop_feature(df):
df.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
 return df
def format_feature(df):
df['Cabin'] = df['Cabin'].str[:1]
features = ['Cabin','Sex','Embarked']
for feature in features:
le = preprocessing.LabelEncoder()
le = le.fit(df[feature])
 df[feature] = le.transform(df[feature])
 return df
def transform_features(df):
df = fill_na(df)
 df = drop_feature(df)
 df = format_feature(df)
 return df
class JaneyMultiheadAttentionClassifier(nn.Module):
def __init__(self,num_classes,out_features,ninp,nhead,dropout,nlayers=1,attention_dropout=0.1):
super(JaneyMultiheadAttentionClassifier, self ).__init__()
encoder_layers = nn.TransformerEncoderLayer(ninp, nhead, ninp*4, attention_dropout)
 encoder_layers.activation=Mish()
self.attention = nn.TransformerEncoder(encoder_layers, nlayers)
 self.classifier=nn.Linear(ninp*2,num_classes)
 self.dropout=nn.Dropout(dropout)
 def forward(self,x):
x=self.dropout(x)
 x=x.permute(1,0,2)
 x=self.attention(x)
 x=x.permute(1,0,2)
 max_x,_=torch.max(x,dim=1)
 x=torch.cat([torch.mean(x,dim=1),max_x],dim=-1)
 x=self.dropout(x)
 x=self.classifier(x)
 return x
class JaneyNetwork(nn.Module):
def __init__(self,num_classes,efficientNet_name='efficientnet-b0',dropout_p=0.5,nhead=8,ninp=512, nhid=512,attention_dropout=0.1):
super(JaneyNetwork, self ).__init__()
self.backbone=models.resnext50_32x4d(pretrained=False)
 self.backbone.avgpool=GeM()
self.num_classes=num_classes
self.out_features = self.backbone.fc.in_features
self.ninp=ninp
self.backbone.fc=nn.Identity()
regression_head=JaneyMultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 classifier_head=JaneyMultiheadAttentionClassifier(num_classes[0],self.out_features,ninp,nhead,dropout_p)
 regression_head2=JaneyMultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 regression_head3=JaneyMultiheadAttentionClassifier(1,self.out_features,ninp,nhead,dropout_p)
 self.heads=nn.ModuleList([regression_head,classifier_head,regression_head2,regression_head3,nn.Linear(self.out_features,ninp)])
 def forward(self,x):
shape = x[0].shape
bs=x.shape[0]
n = len(x)
 x = x.reshape(( -1,shape[1],shape[2],shape[3]))
features=self.backbone(x)
 features=self.heads[4](features)
 features=features.reshape(bs,shape[0],self.ninp)
 outputs=[self.heads[0](features ).squeeze(-1),F.log_softmax(self.heads[1](features),dim=1),
self.heads[2](features ).squeeze(-1),self.heads[3](features ).squeeze(-1)]
return outputs 
<feature_engineering>
titanic_df = pd.read_csv("/kaggle/input/titanic/train.csv")
 y_titanic_df = titanic_df['Survived']
X_titanic_df = titanic_df.drop('Survived', axis=1)
 transform_features(X_titanic_df )
df=df.set_index('image_id')
 df["tile_idx"]=np.nan
df=df.astype(object)
 iuniq=index_df.image_id.unique()
for i in tqdm(iuniq):
idxs=index_df.loc[index_df.image_id==i].idxs.values
df.at[i,"tile_idx"]=idxs
df = df.reset_index()
del dataset
del loader
del index_df
del y
gc.collect()<define_variables>
X_train, X_test, y_train, y_test = train_test_split(X_titanic_df, y_titanic_df,
test_size=0.2, random_state=1 )
Shujun_PRED_LIST=[]
for pred_list_index in range(len(ensemble_shujun_list)) :
Shujun_PRED_LIST.append([])
 for phase_index, ensemble_recipe in enumerate(ensemble_shujun_list):
print("shujun_ensemble_phase",phase_index)
 MODELS=[]
top=3
num_classes=[6,4,4,]
if ensemble_recipe['tileImageSize'] == 288:
print("top_fold is 3")
 top_fold = 3
else:
top_fold = 2
for i in range(top_fold,top_fold+1):
for j in range(top):
if ensemble_recipe['TILE_SIZE'] == 64:
if debug:
print("Janey Network")
 model = JaneyNetwork(num_classes ).to(device)
 else:
model = Network(num_classes,arch = ensemble_recipe['arch'] ).to(device)
 model = model.to(device)
 model = nn.DataParallel(model)
 weights_paths=ensemble_recipe['path'].format(i,j+1)
 if debug:
print(weights_paths)
 model.load_state_dict(torch.load(weights_paths))
model.eval()
MODELS.append(model)
 total_images=0
if ensemble_recipe['TILE_SIZE'] == 64 or ensemble_recipe['tileImageSize'] == 288:
batch_size=8
else:
batch_size=16
if debug:
print("batch_size",batch_size)
 if len(df)%batch_size==0:
batches=int(len(df)/batch_size)
 else:
batches=int(len(df)/batch_size)+1
model.eval()
predictions=[]
mean=torch.Tensor(ensemble_recipe['mean'] ).to(device ).reshape(1,1,3,1,1)
 std=torch.Tensor(ensemble_recipe['std'] ).to(device ).reshape(1,1,3,1,1)
 if debug:
print(mean)
 print(std)
 with torch.no_grad() :
for i in tqdm(range(batches)) :
paths=df.image_id[i*batch_size:(i+1)*batch_size].to_list()
x=get_batch_of_image(paths, sz = ensemble_recipe['tileImageSize'], N = ensemble_recipe['TILE_SIZE'], phase=phase_index, index_df = df)
 x0=torch.Tensor(x ).to(device)
 x_h=torch.Tensor(x ).to(device ).flip(-1)
 x_v=torch.Tensor(x ).to(device ).flip(-2)
 x_vh=torch.Tensor(x ).to(device ).flip(-1,-2)
 x0t=torch.Tensor(x ).to(device ).transpose(-1,-2)
 x_ht=torch.Tensor(x ).to(device ).flip(-1 ).transpose(-1,-2)
 x_vt=torch.Tensor(x ).to(device ).flip(-2 ).transpose(-1,-2)
 x_vht=torch.Tensor(x ).to(device ).flip(-1,-2 ).transpose(-1,-2)
 outputs=[]
isCutThresholdTile = ensemble_recipe['isCutThreshold']
if debug:
print('isCutThresholdTile',isCutThresholdTile)
 for model in MODELS:
output = model(standardize_batch(x0,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_h,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_v,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_vh,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x0t,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_ht,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_vt,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]+\
model(standardize_batch(x_vht,mean,std,isCutThresholdTile=isCutThresholdTile)) [0]
output=output.cpu().numpy() /8
outputs.append(output)
 outputs=np.asarray(outputs)
 outputs=np.mean(outputs,axis=0)
 predictions.append(output)
 for pred in predictions:
Shujun_PRED_LIST[phase_index].extend(pred)
 del output, predictions, MODELS
torch.cuda.empty_cache()
if debug:
for check_p_index in range(len(Shujun_PRED_LIST)) :
print(len(Shujun_PRED_LIST[check_p_index]))
print("at last ensemble phase")
 shujun_pred_list = np.asarray(Shujun_PRED_LIST)
 print("shape",shujun_pred_list.shape)
 if debug:
print(Aksell_PRED_LIST)
 print(Shujun_PRED_LIST)
 total_list = np.vstack(( np.asarray(Aksell_PRED_LIST),np.asarray(Shujun_PRED_LIST)) ).transpose() 
<create_dataframe>
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
label_list = []
for total_list_index, list_item in enumerate(total_list.tolist()):
predictions = list(list_item)
 final_value = np.mean(predictions)
 label=classification_threshold_item(final_value)
 if debug:
print("final_value",final_value)
 print("!!label!!",label)
 label_list.append(label)
 predictions_output=np.asarray(label_list,dtype='int')
 sub_df = pd.DataFrame({'image_id':df.image_id.tolist() , 'isup_grade': list(predictions_output)})
 if chk:
ground_truths=np.asarray(df.isup_grade,dtype='int')
 score=metrics.cohen_kappa_score(ground_truths,predictions_output,weights='quadratic')
 print(score)
 sub_df['ig']=df.isup_grade[:nchk]
sub_df.to_csv('submission.csv',index=False )<save_to_csv>
dt_ = DecisionTreeClassifier(random_state=1)
 dt_.fit(X_train, y_train)
 rf_ = RandomForestClassifier(random_state=1)
 rf_.fit(X_train, y_train)
 lr_ = LogisticRegression(random_state=1)
 lr_.fit(X_train, y_train)
<define_variables>
dt_pred = dt_.predict(X_test)
 rf_pred = rf_.predict(X_test)
 lr_pred = lr_.predict(X_test )
DEBUG = False<define_variables>
print('Accuarcy of decision tree: ', accuracy_score(y_test,dt_pred))
print('Accuracy of random forest: ', accuracy_score(y_test,rf_pred))
print('Accuracy of logistic regression: ', accuracy_score(y_test, lr_pred))
sys.path = [
'.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',
] + sys.path<import_modules>
def KFold_test(clf, folds=5):
kfold = KFold(n_splits=folds)
 scores = []
for iter_count,(train_index, test_index)in enumerate(kfold.split(X_titanic_df)) :
X_train, X_test = X_titanic_df.values[train_index], X_titanic_df.values[test_index]
y_train, y_test = y_titanic_df.values[train_index], y_titanic_df.values[test_index]
clf.fit(X_train,y_train)
 pred = clf.predict(X_test)
 accuracy = accuracy_score(y_test,pred)
 scores.append(accuracy)
 print("cross-validation {0} accuracy {1}".format(iter_count+1,accuracy))
mean_score = np.mean(scores)
 print("mean accuracy: ",mean_score)
 KFold_test(dt_,folds=5 )
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>
parameters={'max_depth':[2,3,5,10],
'min_samples_split':[2,3,5],
'min_samples_leaf':[1,4,5,8]}
grid_dt = GridSearchCV(dt_,param_grid=parameters,scoring='accuracy',cv=5, refit=True)
 grid_dt.fit(X_train,y_train)
 print('best parameter: ', grid_dt.best_params_)
 print('best accuracy: ', grid_dt.best_score_)
 best_dt=grid_dt.best_estimator_
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>
titanic_test = pd.read_csv('.. /input/titanic/test.csv')
 transform_features(titanic_test)
 y_pred = grid_dt.predict(titanic_test)
 test_df = pd.read_csv('.. /input/titanic/test.csv')
 output = pd.DataFrame({'PassengerId': test_df.PassengerId, 'Survived': y_pred})
 output.to_csv('submission.csv', index=False )