Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<predict_on_test>
%matplotlib inline
y_pred = df_preds['has_tumor']
image_id = df_preds['id']<save_to_csv>
df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv' )
submission = pd.DataFrame({'id':image_id,
'label':y_pred,
} ).set_index('id')
 submission.to_csv('submission.csv', columns=['label'] )<drop_column>
embarked = df.groupby('Embarked' ).count() ['PassengerId']
embarked_max = embarked[embarked == embarked.max() ].index[0]
df.loc[df['Embarked'].isnull() , 'Embarked'] = embarked_max
embarked = test_df.groupby('Embarked' ).count() ['PassengerId']
embarked_max = embarked[embarked == embarked.max() ].index[0]
test_df.loc[test_df['Embarked'].isnull() , 'Embarked'] = embarked_max
shutil.rmtree('test_dir' )<import_modules>
avg = np.mean(df['Age'])
 test_avg = np.mean(test_df['Age'])
 df['Age'] = df['Age'].fillna(avg)
 test_df['Age'] = test_df['Age'].fillna(test_avg )
Conv2D, SeparableConv1D, Add, BatchNormalization, Activation, GlobalAveragePooling2D, LeakyReLU, Flatten
Lambda, Multiply, LSTM, Bidirectional, PReLU, MaxPooling1D
print(os.listdir(".. /input")) 
<load_from_csv>
test_avg = np.mean(test_df['Fare'])
 test_df['Fare'] = test_df['Fare'].fillna(test_avg )
df_train = pd.read_csv(".. /input/train_labels.csv")
 id_label_map = {k:v for k,v in zip(df_train.id.values, df_train.label.values)}
df_train.head()<define_variables>
le = LabelEncoder()
le.fit(df['Sex'])
 df['Sex label'] = le.transform(df['Sex'])
 le.fit(test_df['Sex'])
 test_df['Sex label'] = le.transform(test_df['Sex'])
 le.fit(df['Embarked'])
 df['Embarked label'] = le.transform(df['Embarked'])
 le.fit(test_df['Embarked'])
 test_df['Embarked label'] = le.transform(test_df['Embarked'] )
def get_id_from_file_path(file_path):
return file_path.split(os.path.sep)[-1].replace('.tif', '' )<define_variables>
x_tr = df[['Pclass', 'Sex label', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked label']]
y_tr = df['Survived']
x_t = test_df[['Pclass', 'Sex label', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked label']]
x_tr =(x_tr - np.mean(x_tr)) /np.std(x_tr)
 x_t =(x_t - np.mean(x_t)) /np.std(x_t )
labeled_files = glob('.. /input/train/*.tif')
 test_files = glob('.. /input/test/*.tif' )<split>
parameters = {
'max_depth': range(2, 5, 1),
'n_estimators': range(100, 600, 10),
'learning_rate': [0.01, 0.05],
'colsample_bytree': [.9, 1]
}
gbm = XGBClassifier()
model = GridSearchCV(
 estimator=gbm,
param_grid=parameters,
scoring = 'accuracy',
n_jobs = 10,
cv = 10,
verbose=True 
)
train, val = train_test_split(labeled_files, test_size=0.1, random_state=101010 )<choose_model_class>
print("Лучшие параметры: ", model.best_params_)
 print("Лучшая точность: ", xgb_random.best_score_ )
def get_model_classif_mobilenetv2() :
inputs = Input(( 96, 96, 3))
base_model = DenseNet169(include_top=False, input_shape=(96, 96, 3))
x = base_model(inputs)
 out1 = GlobalMaxPooling2D()(x)
 out2 = GlobalAveragePooling2D()(x)
 out3 = Flatten()(x)
 out = Concatenate(axis=-1 )([out1, out2, out3])
 out = Dropout(0.5 )(out)
 out = Dense(1, activation="sigmoid", name="3_" )(out)
 model = Model(inputs, out)
 model.compile(optimizer=Adam(0.0001), loss=binary_crossentropy, metrics=['acc'])
 model.summary()
return model<find_best_params>
model = XGBClassifier(n_estimators=180, max_depth = 3 , learning_rate= 0.01, colsample_bytree = 1)
 model.fit(x_tr, y_tr)
 predictions = model.predict(x_t)
 pr = pd.DataFrame(test_df['PassengerId'])
 pr['Survived'] = pd.DataFrame(predictions )
<train_model><EOS> 
pr.to_csv('submission.csv', index = False )