Kaggle competition: Titanic - Machine Learning from Disaster
clf4 = GaussianNB(
 priors= None,
var_smoothing= 1e-09 )<choose_model_class>
warnings.filterwarnings('ignore' )
clf5 = RandomForestClassifier(
 bootstrap= True,
ccp_alpha= 0.0,
class_weight= None,
criterion= 'gini',
max_depth= 5,
max_features= 'auto',
max_leaf_nodes= 30,
max_samples= None,
min_impurity_decrease= 0.0,
min_impurity_split= None,
min_samples_leaf= 2,
min_samples_split= 100,
min_weight_fraction_leaf= 0.0,
n_estimators= 300,
n_jobs= None,
oob_score= False,
random_state= None,
verbose= 0,
warm_start= False )<choose_model_class>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
clf9 = KNeighborsRegressor(algorithm= 'auto',
leaf_size= 30,
metric= 'minkowski',
metric_params= None,
n_jobs= None,
n_neighbors= 10,
p= 5,
weights= 'uniform' )<choose_model_class>
passengerId = test.PassengerId
titanic = train.append(test, ignore_index=True )
clf10 = DecisionTreeRegressor(ccp_alpha= 0.0,
criterion= 'mse',
max_depth= 5,
max_features= 'auto',
max_leaf_nodes= 30,
min_impurity_decrease= 0.0,
min_impurity_split= None,
min_samples_leaf= 2,
min_samples_split= 100,
min_weight_fraction_leaf= 0.0,
presort= 'deprecated',
random_state= SEED,
splitter= 'best' )<choose_model_class>
train_idx = len(train)
 test_idx = len(titanic)- len(test )
clf11 = GradientBoostingRegressor()<choose_model_class>
titanic.drop('PassengerId', axis=1, inplace=True )
SCF = StackingClassifier(classifiers=[clf1, clf5],
meta_classifier=clf2,
use_probas=True,
average_probas=True )<choose_model_class>
titanic['Title'] = titanic.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 titanic.head()
if test_pipeline:
pipelines = []
pipelines.append(( 'LGBMClassifier', Pipeline([('LGBMClassifier',clf1)])))
 pipelines.append(( 'LogisticRegression', Pipeline([('LogisticRegression',clf2)])))
 pipelines.append(( 'XGBRegressor', Pipeline([('XGBRegressor',clf3)])))
 pipelines.append(( 'GaussianNB', Pipeline([('GaussianNB',clf4)])))
 pipelines.append(( 'RandomForestClassifier', Pipeline([('RandomForestClassifier',clf5)])))
 pipelines.append(( 'KNeighborsRegressor', Pipeline([('KNeighborsRegressor', clf9)])))
 pipelines.append(( 'DecisionTreeRegressor', Pipeline([('DecisionTreeRegressor', clf10)])))
 pipelines.append(( 'GradientBoostingRegressor', Pipeline([('GradientBoostingRegressor', clf11)])))
 pipelines.append(( 'StackingClassifier', Pipeline([('StackingClassifier', SCF)])))
 X_train = train_coded.drop('target',axis=1 ).iloc[:,1:]
y_train = train_coded['target']
M_name = []
M_auc_score = []
for name, model in pipelines:
cv_results = cross_val_score(model, X_train, y_train, cv=FOLDS, scoring='roc_auc')
 M_auc_score.append(np.mean(cv_results))
M_name.append(name)
 print("%s: %f +/- %f" %(name, cv_results.mean() , cv_results.std()))<find_best_params>
print("There are {} unique titles.".format(titanic.Title.nunique()))
print("
", titanic.Title.unique() )
select_classifier = clf2
select_classifier.get_params()<prepare_x_and_y>
normalized_titles = {
"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"
}
if Setup_Parameters:
X_train = train_coded.drop('target',axis=1 ).iloc[:,1:]
y_train = train_coded['target']
params = dict(
 C = [0.001, 0.01, 0.1, 1, 10],
max_iter = [100,150,50] 
)
 grid = GridSearchCV(estimator=select_classifier,
param_grid=params,
cv=FOLDS,
scoring='roc_auc',
refit='AUC',
n_jobs = -1)
 grid.fit(X_train, y_train)
 for r, _ in enumerate(grid.cv_results_['mean_test_score']):
print("AUC : %0.5f +/- %0.5f %r"
%(grid.cv_results_['mean_test_score'][r],
grid.cv_results_['std_test_score'][r] / 2.0,
grid.cv_results_['params'][r]))
print(f'
Best parameters: {grid.best_params_}')
 oof, submission, auc_score, model_name = crossValidate(grid,X=train_coded,X_test=test_coded,FOLDS=FOLDS,SEED=SEED,show_roc_curve=True )<set_options>
titanic.Title = titanic.Title.map(normalized_titles)
 print(titanic.Title.value_counts() )
%matplotlib inline<define_variables>
titanic.Age = grouped.Age.apply(lambda x: x.fillna(x.median()))
titanic.info()
np.random.seed(42)
 random.seed(42 )<load_from_csv>
titanic.Cabin = titanic.Cabin.fillna('U' )
oof_01 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_0.csv')
 test_01 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_0.csv')
 oof_01 = oof_01.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_01 = test_01.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_02 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_100.csv')
 test_02 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_100.csv')
 oof_02 = oof_02.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_02 = test_02.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_03 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_105.csv')
 test_03 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_105.csv')
 oof_03 = oof_03.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_03 = test_03.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_04 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_108.csv')
 test_04 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_108.csv')
 oof_04 = oof_04.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_04 = test_04.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_05 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_109.csv')
 test_05 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_109.csv')
 oof_05 = oof_05.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_05 = test_05.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_06 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_11.csv')
 test_06 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_11.csv')
 oof_06 = oof_06.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_06 = test_06.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_07 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_110.csv')
 test_07 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_110.csv')
 oof_07 = oof_07.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_07 = test_07.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_08 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_111.csv')
 test_08 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_111.csv')
 oof_08 = oof_08.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_08 = test_08.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_09 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_113.csv')
 test_09 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_113.csv')
 oof_09 = oof_09.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_09 = test_09.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_10 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_116.csv')
 test_10 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_116.csv')
 oof_10 = oof_10.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_10 = test_10.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_11 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_117.csv')
 test_11 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_117.csv')
 oof_11 = oof_11.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_11 = test_11.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_12 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_12.csv')
 test_12 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_12.csv')
 oof_12 = oof_12.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_12 = test_12.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_13 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_120.csv')
 test_13 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_120.csv')
 oof_13 = oof_13.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_13 = test_13.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_14 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_121.csv')
 test_14 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_121.csv')
 oof_14 = oof_14.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_14 = test_14.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_15 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_13.csv')
 test_15 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_13.csv')
 oof_15 = oof_15.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_15 = test_15.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_16 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_15.csv')
 test_16 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_15.csv')
 oof_16 = oof_16.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_16 = test_16.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_17 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_16.csv')
 test_17 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_16.csv')
 oof_17 = oof_17.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_17 = test_17.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_18 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_2.csv')
 test_18 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_2.csv')
 oof_18 = oof_18.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_18 = test_18.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_19 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_20.csv')
 test_19 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_20.csv')
 oof_19 = oof_19.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_19 = test_19.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_20 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_24.csv')
 test_20 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_24.csv')
 oof_20 = oof_20.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_20 = test_20.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_21 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_28.csv')
 test_21 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_28.csv')
 oof_21 = oof_21.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_21 = test_21.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_22 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_30.csv')
 test_22 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_30.csv')
 oof_22 = oof_22.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_22 = test_22.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_23 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_32.csv')
 test_23 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_32.csv')
 oof_23 = oof_23.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_23 = test_23.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_24 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_33.csv')
 test_24 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_33.csv')
 oof_24 = oof_24.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_24 = test_24.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_25 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_35.csv')
 test_25 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_35.csv')
 oof_25 = oof_25.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_25 = test_25.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_26 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_384.csv')
 test_26 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_384.csv')
 oof_26 = oof_26.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_26 = test_26.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_27 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_385.csv')
 test_27 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_385.csv')
 oof_27 = oof_27.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_27 = test_27.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_28 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_4.csv')
 test_28 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_4.csv')
 oof_28 = oof_28.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_28 = test_28.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_29 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_44.csv')
 test_29 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_44.csv')
 oof_29 = oof_29.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_29 = test_29.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_30 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_54.csv')
 test_30 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_54.csv')
 oof_30 = oof_30.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_30 = test_30.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_31 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_55.csv')
 test_31 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_55.csv')
 oof_31 = oof_31.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_31 = test_31.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_32 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_56.csv')
 test_32 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_56.csv')
 oof_32 = oof_32.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_32 = test_32.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_33 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_57.csv')
 test_33 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_57.csv')
 oof_33 = oof_33.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_33 = test_33.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_34 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_58.csv')
 test_34 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_58.csv')
 oof_34 = oof_34.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_34 = test_34.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_35 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_59.csv')
 test_35 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_59.csv')
 oof_35 = oof_35.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_35 = test_35.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_36 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_6.csv')
 test_36 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_6.csv')
 oof_36 = oof_36.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_36 = test_36.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_37 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_65.csv')
 test_37 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_65.csv')
 oof_37 = oof_37.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_37 = test_37.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_38 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_67.csv')
 test_38 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_67.csv')
 oof_38 = oof_38.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_38 = test_38.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 oof_39 = pd.read_csv('.. /input/melanoma-oof-and-sub/oof_77.csv')
 test_39 = pd.read_csv('.. /input/melanoma-oof-and-sub/sub_77.csv')
 oof_39 = oof_39.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True)
 test_39 = test_39.sort_values(by=['image_name'],
ascending=True ).reset_index(drop=True )<concatenate>
most_embarked = titanic.Embarked.value_counts().index[0]
titanic.Embarked = titanic.Embarked.fillna(most_embarked )
blend_train = []
blend_test = []
blend_train.append(oof_01.pred)
 blend_train.append(oof_02.pred)
 blend_train.append(oof_03.pred)
 blend_train.append(oof_04.pred)
 blend_train.append(oof_05.pred)
 blend_train.append(oof_06.pred)
 blend_train.append(oof_07.pred)
 blend_train.append(oof_08.pred)
 blend_train.append(oof_09.pred)
 blend_train.append(oof_10.pred)
 blend_train.append(oof_11.pred)
 blend_train.append(oof_12.pred)
 blend_train.append(oof_13.pred)
 blend_train.append(oof_14.pred)
 blend_train.append(oof_15.pred)
 blend_train.append(oof_16.pred)
 blend_train.append(oof_17.pred)
 blend_train.append(oof_18.pred)
 blend_train.append(oof_19.pred)
 blend_train.append(oof_20.pred)
 blend_train.append(oof_21.pred)
 blend_train.append(oof_22.pred)
 blend_train.append(oof_23.pred)
 blend_train.append(oof_24.pred)
 blend_train.append(oof_25.pred)
 blend_train.append(oof_26.pred)
 blend_train.append(oof_27.pred)
 blend_train.append(oof_28.pred)
 blend_train.append(oof_29.pred)
 blend_train.append(oof_30.pred)
 blend_train.append(oof_31.pred)
 blend_train.append(oof_32.pred)
 blend_train.append(oof_33.pred)
 blend_train.append(oof_34.pred)
 blend_train.append(oof_35.pred)
 blend_train.append(oof_36.pred)
 blend_train.append(oof_37.pred)
 blend_train.append(oof_38.pred)
 blend_train.append(oof_39.pred)
 blend_train = np.array(blend_train)
 blend_test.append(test_01.target)
 blend_test.append(test_02.target)
 blend_test.append(test_03.target)
 blend_test.append(test_04.target)
 blend_test.append(test_05.target)
 blend_test.append(test_06.target)
 blend_test.append(test_07.target)
 blend_test.append(test_08.target)
 blend_test.append(test_09.target)
 blend_test.append(test_10.target)
 blend_test.append(test_11.target)
 blend_test.append(test_12.target)
 blend_test.append(test_13.target)
 blend_test.append(test_14.target)
 blend_test.append(test_15.target)
 blend_test.append(test_16.target)
 blend_test.append(test_17.target)
 blend_test.append(test_18.target)
 blend_test.append(test_19.target)
 blend_test.append(test_20.target)
 blend_test.append(test_21.target)
 blend_test.append(test_22.target)
 blend_test.append(test_23.target)
 blend_test.append(test_24.target)
 blend_test.append(test_25.target)
 blend_test.append(test_26.target)
 blend_test.append(test_27.target)
 blend_test.append(test_28.target)
 blend_test.append(test_29.target)
 blend_test.append(test_30.target)
 blend_test.append(test_31.target)
 blend_test.append(test_32.target)
 blend_test.append(test_33.target)
 blend_test.append(test_34.target)
 blend_test.append(test_35.target)
 blend_test.append(test_36.target)
 blend_test.append(test_37.target)
 blend_test.append(test_38.target)
 blend_test.append(test_39.target)
 blend_test = np.array(blend_test )<compute_train_metric>
titanic.Survived.value_counts(normalize=True )
def roc_min_func(weights):
final_prediction = 0
for weight, prediction in zip(weights, blend_train):
final_prediction += weight * prediction
return roc_auc_score(np.array(oof_01.target), final_prediction)
 print('
Finding Blending Weights...')
 res_list = []
weights_list = []
for k in range(1000):
starting_values = np.random.uniform(size=len(blend_train)) * 1/len(blend_train)
 bounds = [(0, 1/len(blend_train)) for _ in range(len(blend_train)) ]
res = minimize(roc_min_func,
starting_values,
method='L-BFGS-B',
bounds=bounds,
options={'disp': False,
'maxiter': 100000})
 res_list.append(res['fun'])
 weights_list.append(res['x'])
 print('{iter}\tScore: {score}\tWeights: {weights}'.format(
 iter=(k + 1),
score=res['fun'],
weights='\t'.join([str(item)for item in res['x']])))
 bestSC = np.max(res_list)
 bestWght = weights_list[np.argmax(res_list)]
weights = bestWght
blend_score = round(bestSC, 6 )<train_model>
group_by_sex = titanic.groupby('Sex')
 group_by_sex.Survived.mean()
print('
Ensemble Score: {best_score}'.format(best_score=bestSC))
print('
Best Weights: {weights}'.format(weights=bestWght))
train_prices = np.zeros(len(blend_train[0]))
test_prices = np.zeros(len(blend_test[0]))
print('
Your final model:')
 for k in range(len(blend_test)) :
print(' %.6f * model-%d' %(weights[k],(k + 1)))
 test_prices += blend_test[k] * weights[k]
for k in range(len(blend_train)) :
train_prices += blend_train[k] * weights[k]<feature_engineering>
group_class_sex = titanic.groupby(['Pclass', 'Sex'])
 group_class_sex.Survived.mean()
test_01.target =(test_01.target.values*bestWght[0] +
test_02.target.values*bestWght[1] +
test_03.target.values*bestWght[2] +
test_04.target.values*bestWght[3] +
test_05.target.values*bestWght[4] +
test_06.target.values*bestWght[5] +
test_07.target.values*bestWght[6] +
test_08.target.values*bestWght[7] +
test_09.target.values*bestWght[8] +
test_10.target.values*bestWght[9] +
test_11.target.values*bestWght[10] +
test_12.target.values*bestWght[11] +
test_13.target.values*bestWght[12] +
test_14.target.values*bestWght[13] +
test_15.target.values*bestWght[14] +
test_16.target.values*bestWght[15] +
test_17.target.values*bestWght[16] +
test_18.target.values*bestWght[17] +
test_19.target.values*bestWght[18] +
test_20.target.values*bestWght[19] +
test_21.target.values*bestWght[20] +
test_22.target.values*bestWght[21] +
test_23.target.values*bestWght[22] +
test_24.target.values*bestWght[23] +
test_25.target.values*bestWght[24] +
test_26.target.values*bestWght[25] +
test_27.target.values*bestWght[26] +
test_28.target.values*bestWght[27] +
test_29.target.values*bestWght[28] +
test_30.target.values*bestWght[29] +
test_31.target.values*bestWght[30] +
test_32.target.values*bestWght[31] +
test_33.target.values*bestWght[32] +
test_34.target.values*bestWght[33] +
test_35.target.values*bestWght[34] +
test_36.target.values*bestWght[35] +
test_37.target.values*bestWght[36] +
test_38.target.values*bestWght[37] +
test_39.target.values*bestWght[38])/sum(bestWght)
 test_01.to_csv('final_weighted_average_ensemble.csv', index=False)
 test_01.head()<define_variables>
titanic['FamilySize'] = titanic.Parch + titanic.SibSp + 1
LABELS = ["target"]
all_files = glob.glob(".. /input/pseudolabelmodels/*.csv" )<load_from_csv>
titanic.Cabin = titanic.Cabin.map(lambda x: x[0])
 titanic.Cabin.value_counts(normalize=True )
outs = [pd.read_csv(f, index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "m" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True )<feature_engineering>
titanic.Sex = titanic.Sex.map({"male": 0, "female":1} )
warnings.filterwarnings("ignore")
 rank = np.tril(concat_sub.iloc[:,1:].corr().values,-1)
 m =(rank>0 ).sum()
m_gmean, s = 0, 0
for n in range(min(rank.shape[0],m)) :
mx = np.unravel_index(rank.argmin() , rank.shape)
 w =(m-n)/(m+n/10)
 m_gmean += w*(np.log(concat_sub.iloc[:,mx[0]+1])+np.log(concat_sub.iloc[:,mx[1]+1])) /2
s += w
rank[mx] = 1
m_gmean = np.exp(m_gmean/s ).clip(0.0,1.0 )<load_from_csv>
pclass_dummies = pd.get_dummies(titanic.Pclass, prefix="Pclass")
 title_dummies = pd.get_dummies(titanic.Title, prefix="Title")
 cabin_dummies = pd.get_dummies(titanic.Cabin, prefix="Cabin")
 embarked_dummies = pd.get_dummies(titanic.Embarked, prefix="Embarked" )
predict_list = []
predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9350-pseudo-rohit.csv')[LABELS].values)
 predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9351-pseudo-rohit.csv')[LABELS].values)
 predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9373-pseudo-vova.csv')[LABELS].values)
 predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9376-pseudo-rohit.csv')[LABELS].values)
 predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9379-pseudo-rohit.csv')[LABELS].values)
 predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9432-pseudo-vova.csv')[LABELS].values)
 predict_list.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9474-pseudo-rohit.csv')[LABELS].values)
 predict_list_2 = []
predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9477-pseudo-ashish.csv')[LABELS].values)
 predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9491-pseudo-rohit.csv')[LABELS].values)
 predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-94981-pseudo-ashish.csv')[LABELS].values)
 predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-94982-pseudo-ashish.csv')[LABELS].values)
 predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9508-pseudo-rohit.csv')[LABELS].values)
 predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9513-pseudo-ashish.csv')[LABELS].values)
 predict_list_2.append(pd.read_csv('.. /input/pseudolabelmodels/submission-9547-pseudo-rohit.csv')[LABELS].values )<feature_engineering>
titanic_dummies = pd.concat([titanic, pclass_dummies, title_dummies, cabin_dummies, embarked_dummies], axis=1)
 titanic_dummies.drop(['Pclass', 'Title', 'Cabin', 'Embarked', 'Name', 'Ticket'], axis=1, inplace=True)
 titanic_dummies.head()
warnings.filterwarnings("ignore")
 print("Rank averaging on ", len(predict_list), " files")
 predictions = np.zeros_like(predict_list[0])
 for predict in predict_list:
for i in range(1):
predictions[:, i] = np.add(predictions[:, i], rankdata(predict[:, i])/predictions.shape[0])
 predictions = predictions /len(predict_list )<feature_engineering>
train = titanic_dummies[ :train_idx]
test = titanic_dummies[test_idx: ]
train.Survived = train.Survived.astype(int )
warnings.filterwarnings("ignore")
 print("Rank averaging on ", len(predict_list_2), " files")
 predictions_2 = np.zeros_like(predict_list_2[0])
 for predict_2 in predict_list_2:
for i in range(1):
predictions_2[:, i] = np.add(predictions_2[:, i], rankdata(predict_2[:, i])/predictions_2.shape[0])
 predictions_2 = predictions_2 /len(predict_list_2 )<save_to_csv>
X = train.drop('Survived', axis=1 ).values
y = train.Survived.values
submission_1 = pd.read_csv('.. /input/siim-isic-melanoma-classification/sample_submission.csv')
 submission_1[LABELS] = predictions
submission_2 = pd.read_csv('.. /input/siim-isic-melanoma-classification/sample_submission.csv')
 submission_2[LABELS] = predictions_2
submission = pd.read_csv('.. /input/siim-isic-melanoma-classification/sample_submission.csv')
 submission[LABELS] = submission_2[LABELS]*.55 + submission_1[LABELS]*.45
submission.to_csv('submission.csv', index=False )<install_modules>
X_test = test.drop('Survived', axis=1 ).values
!pip install -q efficientnet >> /dev/null 
<define_variables>
log_params = dict(
 C = np.logspace(-5, 8, 15),
penalty = ['l1', 'l2']
)
DEVICE = "TPU"
SEED = 42
FOLDS = 5
IMG_SIZES = [256]*FOLDS
DROP_FREQ = [0.5]*FOLDS
DROP_CT = [10]*FOLDS
DROP_SIZE = [0.1]*FOLDS
INC2019 = [0]*FOLDS
INC2018 = [1]*FOLDS
M1 = [1]*FOLDS
M2 = [1]*FOLDS
M3 = [0]*FOLDS
M4 = [1]*FOLDS
BATCH_SIZES = [32]*FOLDS
EPOCHS = [12]*FOLDS
EFF_NETS = [6]*FOLDS
WGTS = [1/FOLDS]*FOLDS
TTA = 20
INFER_TEST = True<choose_model_class>
log = LogisticRegression()
logreg_cv = GridSearchCV(estimator=log, param_grid=log_params, cv=5)
 logreg_cv.fit(X, y )
if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}' )<feature_engineering>
print("Tuned Logistic Regression Parameters: {}".format(logreg_cv.best_params_))
print("Best score is {}".format(logreg_cv.best_score_))
GCS_PATH = [None]*FOLDS; GCS_PATH2 = [None]*FOLDS; GCS_PATH3 = [None]*FOLDS
for i,k in enumerate(IMG_SIZES[:FOLDS]):
GCS_PATH[i] = KaggleDatasets().get_gcs_path('melanoma-%ix%i'%(k,k))
GCS_PATH2[i] = KaggleDatasets().get_gcs_path('isic2019-%ix%i'%(k,k))
GCS_PATH3[i] = KaggleDatasets().get_gcs_path('malignant-v2-%ix%i'%(k,k))
files_train = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[0] + '/train*.tfrec')))
 files_test = np.sort(np.array(tf.io.gfile.glob(GCS_PATH[0] + '/test*.tfrec')) )<normalization>
log_pred = logreg_cv.predict(X_test )
ROT_ = 180.0; SHR_ = 2.0
HZOOM_ = 8.0; WZOOM_ = 8.0
HSHIFT_ = 8.0; WSHIFT_ = 8.0
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 def get_3x3_mat(lst):
return tf.reshape(tf.concat([lst],axis=0), [3,3])
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = get_3x3_mat([c1, s1, zero,
-s1, c1, zero,
zero, zero, one])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = get_3x3_mat([one, s2, zero,
zero, c2, zero,
zero, zero, one])
 zoom_matrix = get_3x3_mat([one/height_zoom, zero, zero,
zero, one/width_zoom, zero,
zero, zero, one])
 shift_matrix = get_3x3_mat([one, zero, height_shift,
zero, one, width_shift,
zero, zero, one])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, DIM=256):
XDIM = DIM%2
rot = ROT_ * tf.random.normal([1], dtype='float32')
 shr = SHR_ * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ HZOOM_
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ WZOOM_
h_shift = HSHIFT_ * tf.random.normal([1], dtype='float32')
 w_shift = WSHIFT_ * tf.random.normal([1], dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])
 z = tf.ones([DIM*DIM], dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d,[DIM, DIM,3] )<prepare_x_and_y>
forrest_params = dict(
 max_depth = [n for n in range(9, 14)],
min_samples_split = [n for n in range(4, 11)],
min_samples_leaf = [n for n in range(2, 5)],
n_estimators = [n for n in range(10, 60, 10)],
)
def dropout(image, DIM=256, PROBABILITY = 0.75, CT = 8, SZ = 0.2):
P = tf.cast(tf.random.uniform([],0,1)<PROBABILITY, tf.int32)
 if(P==0)|(CT==0)|(SZ==0): return image
for k in range(CT):
x = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 y = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 WIDTH = tf.cast(SZ*DIM,tf.int32)* P
ya = tf.math.maximum(0,y-WIDTH//2)
 yb = tf.math.minimum(DIM,y+WIDTH//2)
 xa = tf.math.maximum(0,x-WIDTH//2)
 xb = tf.math.minimum(DIM,x+WIDTH//2)
 one = image[ya:yb,0:xa,:]
two = tf.zeros([yb-ya,xb-xa,3])
 three = image[ya:yb,xb:DIM,:]
middle = tf.concat([one,two,three],axis=1)
 image = tf.concat([image[0:ya,:,:],middle,image[yb:DIM,:,:]],axis=0)
 image = tf.reshape(image,[DIM,DIM,3])
 return image<prepare_x_and_y>
forrest = RandomForestClassifier()
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
'patient_id' : tf.io.FixedLenFeature([], tf.int64),
'sex' : tf.io.FixedLenFeature([], tf.int64),
'age_approx' : tf.io.FixedLenFeature([], tf.int64),
'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),
'diagnosis' : tf.io.FixedLenFeature([], tf.int64),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example, return_image_name=True):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['image_name'] if return_image_name else 0
def prepare_image(img, augment=True, dim=256, droprate=0, dropct=0, dropsize=0):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
img = transform(img,DIM=dim)
 if(droprate!=0)&(dropct!=0)&(dropsize!=0):
img = dropout(img, DIM=dim, PROBABILITY=droprate, CT=dropct, SZ=dropsize)
 img = tf.image.random_flip_left_right(img)
 img = tf.image.random_saturation(img, 0.7, 1.3)
 img = tf.image.random_contrast(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 img = tf.reshape(img, [dim,dim, 3])
 return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n )<create_dataframe>
forest_cv = GridSearchCV(estimator=forrest, param_grid=forrest_params, cv=5)
 forest_cv.fit(X, y )
def get_dataset(files, augment = False, shuffle = False, repeat = False,
labeled=True, return_image_names=True, batch_size=16, dim=256,
droprate=0, dropct=0, dropsize=0):
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024*2)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 else:
ds = ds.map(lambda example: read_unlabeled_tfrecord(example, return_image_names),
num_parallel_calls=AUTO)
 ds = ds.map(lambda img, imgname_or_label:(
 prepare_image(img, augment=augment, dim=dim,
droprate=droprate, dropct=dropct, dropsize=dropsize),
imgname_or_label),
num_parallel_calls=AUTO)
 ds = ds.batch(batch_size * REPLICAS)
 ds = ds.prefetch(AUTO)
 return ds<prepare_x_and_y>
print("Best score: {}".format(forest_cv.best_score_))
print("Optimal params: {}".format(forest_cv.best_estimator_))
def show_dataset(thumb_size, cols, rows, ds):
mosaic = PIL.Image.new(mode='RGB', size=(thumb_size*cols +(cols-1),
thumb_size*rows +(rows-1)))
 for idx, data in enumerate(iter(ds)) :
img, target_or_imgid = data
ix = idx % cols
iy = idx // cols
img = np.clip(img.numpy() * 255, 0, 255 ).astype(np.uint8)
 img = PIL.Image.fromarray(img)
 img = img.resize(( thumb_size, thumb_size), resample=PIL.Image.BILINEAR)
 mosaic.paste(img,(ix*thumb_size + ix,
iy*thumb_size + iy))
nn = target_or_imgid.numpy().decode("utf-8")
 display(mosaic)
 return nn<choose_model_class>
forrest_pred = forest_cv.predict(X_test)
 print(forrest_pred )
EFNS = [efn.EfficientNetB0, efn.EfficientNetB1, efn.EfficientNetB2, efn.EfficientNetB3,
efn.EfficientNetB4, efn.EfficientNetB5, efn.EfficientNetB6]
def build_model(dim=128, ef=0):
inp = tf.keras.layers.Input(shape=(dim,dim,3))
base = EFNS[ef](input_shape=(dim,dim,3),weights='imagenet',include_top=False)
 x = base(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = tf.keras.layers.Dense(1,activation='sigmoid' )(x)
 model = tf.keras.Model(inputs=inp,outputs=x)
 opt = tf.keras.optimizers.Adam(learning_rate=0.001)
 loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05)
 model.compile(optimizer=opt,loss=loss,metrics=['AUC'])
 return model<init_hyperparams>
kaggle = pd.DataFrame({'PassengerId': passengerId, 'Survived': forrest_pred} )
def get_lr_callback(batch_size=8):
lr_start = 0.000005
lr_max = 0.00000125 * REPLICAS * batch_size
lr_min = 0.000001
lr_ramp_ep = 5
lr_sus_ep = 0
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback<define_variables>
filename = 'submit.csv'
kaggle.to_csv(filename, index=False )