Kaggle competition: Titanic - Machine Learning from Disaster
learn.load('ef5_400_1')
 gc.collect()<feature_engineering>
import seaborn as sns
import matplotlib.pyplot as plt
%%time
preds6 = learn.TTA(beta =.2, scale=1,ds_type=DatasetType.Test )<prepare_output>
gender_submission = pd.read_csv(".. /input/titanic/gender_submission.csv")
 test_raw = pd.read_csv(".. /input/titanic/test.csv", index_col='PassengerId')
 train_raw = pd.read_csv(".. /input/titanic/train.csv",index_col='PassengerId' )
test_preds = torch.argmax(preds,dim=1 )<save_to_csv>
X=train_raw.drop('Survived', axis=1)
 y=train_raw['Survived']
X_cb=pd.concat([X, test_raw], axis=0)
 test=test_raw
train_idx=list(train_raw.index)
 test_idx=list(test_raw.index )
test['diagnosis'] = np.array(test_preds,dtype = np.int)
 test.to_csv('submission.csv',index = False )<count_values>
X_cb.isnull().sum().sort_values(ascending=False )
test['diagnosis'].value_counts()<set_options>
class_fare=test.groupby(['Pclass','Embarked'])['Fare'].mean()
test['Fare'].fillna(class_fare[3]['S'], inplace=True )
%reload_ext autoreload
%autoreload 2 
<define_variables>
X['Embarked'].value_counts()
SEED = 1234
SIZE = 224
PATH = ".. /input/aptos2019-blindness-detection"<set_options>
X['Embarked'].fillna(X['Embarked'].mode() [0], inplace=True )
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
seed_everything(SEED )<load_from_csv>
X[['Last','First']]=X['Name'].str.split(',', expand=True)
 X[['Title', 'FName']]=X['First'].str.split('.', n=1, expand=True)
 X['Title']=X['Title'].str.strip()
test[['Last','First']]=test['Name'].str.split(',', expand=True)
 test[['Title', 'FName']]=test['First'].str.split('.', n=1, expand=True)
 test['Title']=test['Title'].str.strip()
train_df = pd.read_csv(PATH+"/train.csv")
 sub = pd.read_csv(PATH+"/sample_submission.csv" )<define_variables>
X['Adult']=''
for i in list(X.index):
if X['Age'].loc[i]>8:
X['Adult'].loc[i]=1
elif X['Age'].loc[i]<=8:
X['Adult'].loc[i]=0
else:
if X['Title'].loc[i]=='Master':
X['Adult'].loc[i]=0
else:
X['Adult'].loc[i]=1
test['Adult']=''
for i in list(test.index):
if test['Age'].loc[i]>8:
test['Adult'].loc[i]=1
elif test['Age'].loc[i]<=8:
test['Adult'].loc[i]=0
else:
if test['Title'].loc[i]=='Master':
test['Adult'].loc[i]=0
else:
test['Adult'].loc[i]=1
train = ImageList.from_df(train_df, path=PATH, cols='id_code', folder="train_images", suffix='.png')
 test = ImageList.from_df(sub, path=PATH, cols='id_code', folder="test_images", suffix='.png' )<compute_test_metric>
X['Family_Size']=X['SibSp']+X['Parch']+1
X["With_Family"]=X.apply(lambda row: 1 if row['Family_Size']>1 else 0, axis=1)
 test['Family_Size']=test['SibSp']+test['Parch']+1
test["With_Family"]=test.apply(lambda row: 1 if row['Family_Size']>1 else 0, axis=1)
 count=X['Ticket'].value_counts().to_frame().reset_index()
count.columns=['Ticket','Person_on_Ticket']
X=pd.merge(X,count,how='left',on='Ticket' ).set_index(X.index)
 count_t=test['Ticket'].value_counts().to_frame().reset_index()
count_t.columns=['Ticket','Person_on_Ticket']
test=pd.merge(test,count_t,how='left',on='Ticket' ).set_index(test.index )
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0' )<import_modules>
X.drop(['Name','Age','Ticket','First','FName','Cabin'], axis=1, inplace=True)
 test.drop(['Name','Age','Ticket','First','FName','Cabin'], axis=1, inplace=True )
from sklearn.model_selection import StratifiedKFold
from tqdm import tqdm<load_pretrained>
X=pd.get_dummies(X)
 test=pd.get_dummies(test)
 X.head()
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state = SEED)
 model_name = 'densenet201'
bs = 64
predictions = torch.from_numpy(np.zeros(( len(sub))))
for fold,(train_index, val_index)in tqdm(enumerate(skf.split(train_df["id_code"], train_df["diagnosis"]))):
print(fold)
 filename = model_name + "fold_" + str(fold)+".pkl"
print("Fold:", filename)
 print("TRAIN:", train_index, "VALIDATE:", val_index)
 data_fold =(ImageList.from_df(train_df,
PATH,
folder='train_images'
,cols="id_code",suffix='.png')
.split_by_idxs(train_index, val_index)
.label_from_df(cols='diagnosis', label_cls=FloatList)
.transform(get_transforms() , size=SIZE)
.databunch(bs=bs ).normalize(imagenet_stats)
)
 learn = cnn_learner(data_fold, models.densenet201, metrics=[quadratic_kappa], pretrained=True)
 learn.fit_one_cycle(5, 1e-2)
 learn.data.add_test(ImageList.from_df(sub ,PATH ,folder='test_images',suffix='.png'))
test_predsx, _ = learn.get_preds(ds_type=DatasetType.Test)
 if(fold == 0):
test_preds = test_predsx
else:
test_preds = test_predsx + test_preds
if(fold == 2):
valid_preds, valid_y = learn.get_preds(ds_type=DatasetType.Valid )<compute_test_metric>
only_in_train_col=list(set(X.columns)-set(test.columns))
only_in_test_col=list(set(test.columns)-set(X.columns))
for col in only_in_train_col:
test[col]=0
for col in only_in_test_col:
test.drop(col, axis=1, inplace=True)
 test=test[X.columns]
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']<compute_train_metric>
kf=KFold(n_splits=5, random_state=0, shuffle=True)
 X_train,X_valid, y_train,y_valid=train_test_split(X,y, test_size=0.3, random_state=0)
 def cv_scores(model, X):
cv_scores=cross_val_score(model, X, y, cv=5)
 return cv_scores
warnings.filterwarnings(action="ignore" )
test_preds = test_preds/5
optR = OptimizedRounder()
optR.fit(valid_preds, valid_y)
 coefficients = optR.coefficients()
valid_predictions = optR.predict(valid_preds, coefficients)[:,0].astype(int)
 test_predictions = optR.predict(test_preds, coefficients)[:,0].astype(int)
 valid_score = cohen_kappa_score(valid_y.numpy().astype(int), valid_predictions, weights="quadratic" )<compute_test_metric>
dt=DecisionTreeClassifier(random_state=0)
 rf=RandomForestClassifier(n_estimators=100, max_features=None, max_depth=None)
 ada=AdaBoostClassifier(n_estimators=100,learning_rate=0.4)
 gdb=GradientBoostingClassifier(n_estimators=100, learning_rate=0.4)
 xgb=XGBClassifier(learning_rate=0.5 )
valid_score = cohen_kappa_score(valid_y.numpy().astype(int), valid_predictions, weights="quadratic" )<compute_test_metric>
cv_score_dt=cv_scores(dt,X ).mean()
cv_score_rf=cv_scores(rf,X ).mean()
cv_score_ada=cv_scores(ada,X ).mean()
cv_score_gdb=cv_scores(gdb,X ).mean()
cv_score_xgb=cv_scores(xgb,X ).mean()
print("coefficients:", coefficients)
 print("validation score:", valid_score )<save_to_csv>
print(' dt cv score: ',cv_scores(dt,X ).mean() , 'std: ',cv_scores(dt,X ).std() ,'
',
'rf cv score: ',cv_scores(rf,X ).mean() , 'std: ',cv_scores(rf,X ).std() ,'
',
'ada cv score: ',cv_scores(ada,X ).mean() , 'std: ',cv_scores(ada,X ).std() ,'
',
'gdb cv score: ',cv_scores(gdb,X ).mean() , 'std: ',cv_scores(gdb,X ).std() ,'
',
'xgb cv score: ',cv_scores(xgb,X ).mean() , 'std: ',cv_scores(xgb,X ).std() ,
)
sub.diagnosis = test_predictions
sub.to_csv("submission.csv", index=None)
 sub.head()<set_options>
dt.fit(X_train,y_train)
 rf.fit(X_train,y_train)
 ada.fit(X_train,y_train)
 gdb.fit(X_train,y_train)
 xgb.fit(X_train,y_train )
%matplotlib inline
np.random.seed(2019)
 tf.set_random_seed(2019 )<load_from_csv>
y_dt=dt.predict(X_valid)
 y_rf=rf.predict(X_valid)
 y_ada=ada.predict(X_valid)
 y_gdb=gdb.predict(X_valid)
 y_xgb=xgb.predict(X_valid )
train_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 print(train_df.shape)
 print(test_df.shape)
 train_df.head()<prepare_x_and_y>
print(' dt accuracy: ',accuracy_score(y_valid,y_dt), '
',
'rf accuracy: ',accuracy_score(y_valid,y_rf), '
',
'ada accuracy: ',accuracy_score(y_valid,y_ada), '
',
'gdb accuracy: ',accuracy_score(y_valid,y_gdb), '
',
'xgb accuracy: ',accuracy_score(y_valid,y_xgb),
)
N = train_df.shape[0]
x_train = np.empty(( N, 224, 224, 3), dtype=np.uint8)
 for i, image_id in enumerate(tqdm(train_df['id_code'])) :
x_train[i, :, :, :] = preprocess_image(
 f'.. /input/aptos2019-blindness-detection/train_images/{image_id}.png'
)<prepare_x_and_y>
test_pred=gdb.predict(test )
<categorify><EOS> 
output=pd.DataFrame({'PassengerId': test.index, 'Survived': test_pred})
 output.to_csv('my_submission.csv', index=False )