Kaggle competition: Titanic - Machine Learning from Disaster
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
train= pd.read_csv('.. /input/train.csv')
 train['Label'] = 'train'
test = pd.read_csv('.. /input/test.csv')
 test['Label'] = 'test'
train.dropna(axis=0, subset=['Survived'], inplace=True)
 X_full = pd.concat([train.drop('Survived', axis = 1), test], axis = 0)
 X_full.drop('PassengerId', axis = 1, inplace=True)
 print("Categorical features: ", [cname for cname in X_full.columns if X_full[cname].dtype == "object"])
 print("Numeric features: ", [cname for cname in X_full.columns if X_full[cname].dtype in ['int64', 'float64']])
 X_full.head()
roc_auc_score(y_val, val_preds )<compute_test_metric>
missing_val_count_by_column =(X_full.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column > 0] )
log_loss(y_val, val_preds )<predict_on_test>
X_full.drop('Name', axis=1, inplace=True)
 X_full.drop('Ticket', axis=1, inplace=True)
 X_full.drop('Cabin', axis=1, inplace=True)
 num_simple_imputer = SimpleImputer(strategy='median')
 numeric_features = ['Age', 'Fare', 'Pclass', 'SibSp', 'Parch']
num_transformed = pd.DataFrame(num_simple_imputer.fit_transform(X_full[numeric_features]))
num_transformed.columns = numeric_features
cat_simple_imputer = SimpleImputer(strategy='constant', fill_value='missing')
 categorical_features = ['Embarked','Sex', 'Label']
cat_transformed = pd.DataFrame(cat_simple_imputer.fit_transform(X_full[categorical_features]))
cat_transformed.columns = categorical_features
X_dummies = pd.get_dummies(cat_transformed, columns = categorical_features)
 X_full = pd.concat([num_transformed, X_dummies], axis = 1)
 print(X_full.dtypes)
 print(X_full.head() )
test_preds = lr.predict_proba(test_EB1)[:,1]<save_to_csv>
X = X_full[X_full['Label_train'] == 1].copy()
X_test = X_full[X_full['Label_test'] == 1].copy()
X.drop('Label_train', axis=1, inplace=True)
 X.drop('Label_test', axis=1, inplace=True)
 X_test.drop('Label_test', axis=1, inplace=True)
 X_test.drop('Label_train', axis=1, inplace=True)
 y = train.Survived
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<install_modules>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.7, test_size=0.3,
random_state=0 )
!pip install --use-feature=2020-resolver https://s3-us-west-2.amazonaws.com/xgboost-nightly-builds/xgboost-1.3.0_SNAPSHOT%2B00b0ad1293b4fa74d6aca5da4e9ab7a9d16777f0-py3-none-manylinux2010_x86_64.whl<import_modules>
model = XGBClassifier(random_state = 18)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_valid)
 predictions = [round(value)for value in y_pred]
accuracy = accuracy_score(y_valid, predictions)
 print("Accuracy: %.2f%%" %(accuracy * 100.0))
thresholds = sort(model.feature_importances_)
 models = []
for thresh in thresholds:
selection = SelectFromModel(model, threshold=thresh, prefit=True)
 select_X_train = selection.transform(X_train)
 selection_model = XGBClassifier()
selection_model.fit(select_X_train, y_train)
 models.append([selection_model, selection])
 select_X_valid = selection.transform(X_valid)
 select_y_pred = selection_model.predict(select_X_valid)
 predictions = [round(value)for value in select_y_pred]
accuracy = accuracy_score(y_valid, predictions)
 print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" %(thresh, select_X_train.shape[1], accuracy*100.0))
print("MAE: ", mean_absolute_error(y_valid, predictions))
xgb.__version__<import_modules>
final_model = models[3][0]
final_selection = models[3][1]
final_X_train = final_selection.transform(X_train)
 final_X_test = final_selection.transform(X_test)
 final_X_valid = final_selection.transform(X_valid)
 final_y_pred = final_model.predict(final_X_valid)
 final_predictions = [round(value)for value in final_y_pred]
accuracy = accuracy_score(y_valid, final_predictions)
 print("n=%d, Accuracy: %.2f%%" %(final_X_train.shape[1], accuracy*100.0))
print("MAE: ", mean_absolute_error(y_valid, final_predictions))
import numpy as np
import pandas as pd
import zipfile
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import log_loss, roc_auc_score
import gc
import os 
<load_from_csv>
grid_param = {
'n_estimators': [12, 25, 50, 75],
'max_depth': [3, 4, 5],
'learning_rate': [0.01, 0.05, 0.1],
'early_stopping_rounds': [3, 4, 5, 6]
}
gd_sr = GridSearchCV(estimator = final_model, param_grid = grid_param,
cv = 3, n_jobs = -1, verbose = 2)
 gd_sr.fit(X_train, y_train)
 best_parameters = gd_sr.best_params_
print(best_parameters )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
selection = SelectFromModel(model, threshold=0.027, prefit=True)
 select_X_train = selection.transform(X_train)
 select_X_test = selection.transform(X_test)
 select_X_valid = selection.transform(X_valid)
 another_model = XGBClassifier(early_stopping_rounds=3, learning_rate=0.01, max_depth=5, n_estimators=75)
 another_model.fit(select_X_train, y_train)
 select_y_pred = another_model.predict(select_X_valid)
 select_predictions = [round(value)for value in select_y_pred]
accuracy = accuracy_score(y_valid, select_predictions)
 print("n=%d, Accuracy: %.2f%%" %(select_X_train.shape[1], accuracy*100.0))
print("MAE: ", mean_absolute_error(y_valid, select_predictions))
TRAIN_FOLDER = '.. /input/cats-and-dogs-embedded-data/train/train/'
TEST_FOLDER = '.. /input/cats-and-dogs-embedded-data/test/test/'
IMG_SIZE = 224<load_pretrained>
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, y_train)
 Y_pred_rf = random_forest.predict(X_test)
 random_forest.score(X_train, y_train )
train_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_image_list.npy')
 test_image_list = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_image_list.npy' )<define_variables>
confusion_matrix(y_valid, final_y_pred )
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
final_preds = random_forest.predict(X_test)
 output = pd.DataFrame({'PassengerId':test.PassengerId,'Survived': final_preds})
 print(output.head(15))
output.to_csv('submission.csv', index=False )