Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
train_data[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean()<groupby>
!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
train_data[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean()<groupby>
xgb.__version__
train_data[['SibSp', 'Survived']].groupby('SibSp', as_index=False ).mean()<groupby>
import numpy as np
import pandas as pd
import cudf
import cupy as cp
from cuml.neighbors import KNeighborsClassifier
from cuml.linear_model import LogisticRegression
from sklearn.model_selection import KFold, train_test_split
from sklearn.metrics import roc_auc_score, log_loss
train_data[['Parch', 'Survived']].groupby('Parch', as_index=False ).mean()<feature_engineering>
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()
train_data['AgeBand'] = pd.cut(train_data['Age'], 9 )<groupby>
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' )
train_data[['AgeBand','Survived']].groupby('AgeBand', as_index=False ).mean()<feature_engineering>
ids = [int(x.split('.')[0])for x in test_image_list]
train_data.loc[ train_data['Age'] <= 18, 'Age'] = 0
train_data.loc[(train_data['Age'] > 18)&(train_data['Age'] <= 44), 'Age'] = 1
train_data.loc[(train_data['Age'] > 44)&(train_data['Age'] <= 53), 'Age'] = 2
train_data.loc[(train_data['Age'] > 53)&(train_data['Age'] <= 62), 'Age'] = 3
train_data.loc[ train_data['Age'] > 62, 'Age'] = 4<drop_column>
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )
train_data.drop(labels='AgeBand', axis=1, inplace=True )<feature_engineering>
train_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB7_ns.npy')
 test_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB7_ns.npy' )
train_data['FareBand'] = pd.qcut(train_data['Fare'], 5 )<groupby>
y_oof = np.zeros(train_EB7_ns.shape[0])
 y_test = 0
train_data[['FareBand', 'Survived']].groupby('FareBand', as_index=False ).mean()<feature_engineering>
NUM_FOLDS = 20
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=137 )
train_data.loc[ train_data['Fare'] <= 10, 'Fare'] = 0
train_data.loc[(train_data['Fare'] > 10)&(train_data['Fare'] <= 40), 'Fare'] = 1
train_data.loc[train_data['Fare'] > 40 , 'Fare'] = 2<drop_column>
%%time
for f,(train_ind, val_ind)in enumerate(kf.split(train_EB7_ns, target)) :
print(f)
 X_train, X_val = train_EB7_ns[train_ind], train_EB7_ns[val_ind]
y_train, y_val = target[train_ind], target[val_ind]
lr = LogisticRegression(C=0.026, max_iter=10000)
 lr.fit(X_train, y_train)
 val_preds = lr.predict_proba(X_val)[:,1]
test_preds = lr.predict_proba(test_EB7_ns)[:,1]
y_oof[val_ind] = val_preds
y_test += test_preds/NUM_FOLDS
train_data.drop(labels='FareBand', axis=1, inplace=True )<load_from_csv>
roc_auc_score(target, y_oof )
test_data = pd.read_csv(".. /input/titanic/test.csv" )<drop_column>
log_loss(target, y_oof )
test_data.drop(columns=['Name', 'Ticket', 'Cabin'], axis=1, inplace=True )<categorify>
y_oof_xgb_1 = np.zeros(train_EB7_ns.shape[0])
 y_test_xgb_1 = 0
test_data['Sex'] = test_data['Sex'].map({'male':0,'female':1} )<categorify>
%%time
xgb_params = {
'eta': 0.05,
'max_depth': 4,
'subsample': 0.8,
'colsample_bytree': 0.6,
'alpha': 0.01,
'lambda': 1.00,
'gamma' : 0.02,
'max_bin': 256,
'objective': 'reg:logistic',
'eval_metric': 'auc',
'verbosity': 0,
'tree_method': 'gpu_hist',
'predictor': 'gpu_predictor'
}
dtest = xgb.DMatrix(test_EB7_ns)
 for f,(train_ind, val_ind)in enumerate(kf.split(train_EB7_ns, target)) :
print(f)
 X_train, X_val = train_EB7_ns[train_ind], train_EB7_ns[val_ind]
y_train, y_val = target[train_ind], target[val_ind]
dtrain = xgb.DMatrix(X_train, y_train)
 dval = xgb.DMatrix(X_val, y_val)
 clf = xgb.train(xgb_params, dtrain, num_boost_round=100)
 val_preds = clf.predict(dval)
 test_preds = clf.predict(dtest)
 y_oof_xgb_1[val_ind] = val_preds
y_test_xgb_1 += test_preds/NUM_FOLDS
print(roc_auc_score(target, y_oof_xgb_1))
print(log_loss(target, y_oof_xgb_1))
test_data['Embarked'] = test_data['Embarked'].map({'S':0,'C':1,'Q':2} )<data_type_conversions>
%%time
y_oof_knn = np.zeros(train_EB7_ns.shape[0])
 y_test_knn = 0
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=137)
 for f,(train_ind, val_ind)in enumerate(kf.split(train_EB7_ns, target)) :
print(f)
 X_train, X_val = train_EB7_ns[train_ind], train_EB7_ns[val_ind]
y_train, y_val = target[train_ind], target[val_ind]
knc = KNeighborsClassifier(n_neighbors=400)
 knc.fit(X_train, y_train)
 val_preds = knc.predict_proba(X_val)[:,1]
test_preds = knc.predict_proba(test_EB7_ns)[:,1]
y_oof_knn[val_ind] = val_preds
y_test_knn += test_preds/NUM_FOLDS
print(roc_auc_score(target, y_oof_knn))
print(log_loss(target, y_oof_knn))
test_data['Fare'].fillna(value=test_data['Fare'].median() ,axis=0, inplace=True )<data_type_conversions>
print(roc_auc_score(target, 0.9*y_oof+0.1*y_oof_knn))
print(log_loss(target, 0.9*y_oof+0.1*y_oof_knn))
test_data.fillna(value=test_data['Age'].mean() , axis=0, inplace=True )<feature_engineering>
sample_submission['id'] = ids
sample_submission['label'] = y_test
sample_submission.to_csv('submission_lr.csv', index=False)
 sample_submission.head()
test_data.loc[ test_data['Age'] <= 18, 'Age'] = 0
test_data.loc[(test_data['Age'] > 18)&(test_data['Age'] <= 44), 'Age'] = 1
test_data.loc[(test_data['Age'] > 44)&(test_data['Age'] <= 53), 'Age'] = 2
test_data.loc[(test_data['Age'] > 53)&(test_data['Age'] <= 62), 'Age'] = 3
test_data.loc[ test_data['Age'] > 62, 'Age'] = 4<feature_engineering>
sample_submission['id'] = ids
sample_submission['label'] = y_test_xgb_1
sample_submission.to_csv('submission_xgb_1.csv', index=False)
 sample_submission.head()
test_data.loc[ test_data['Fare'] <= 10, 'Fare'] = 0
test_data.loc[(test_data['Fare'] > 10)&(test_data['Fare'] <= 40), 'Fare'] = 1
test_data.loc[test_data['Fare'] > 40 , 'Fare'] = 2<load_from_csv>
sample_submission['label'] = y_test_knn
sample_submission.to_csv('submission_knn.csv', index=False)
 sample_submission.head()
submission = pd.read_csv('.. /input/titanic/gender_submission.csv' )<prepare_x_and_y>
sample_submission['label'] = 0.9*y_test + 0.1* y_test_knn
sample_submission.to_csv('submission_blend_1.csv', index=False)
 sample_submission.head()
features = ["Pclass", "Sex", "Age","Fare","SibSp","Parch","Embarked"]
targets = ["Survived"]
X_train, X_test = train_data[features], test_data[features]
y_train, y_test = train_data[targets],submission[targets]<import_modules>
sample_submission['label'] = 0.95*y_test + 0.05* y_test_knn
sample_submission.to_csv('submission_blend_2.csv', index=False)
 sample_submission.head()
import statsmodels.api as sm
from sklearn.linear_model import LogisticRegression
from sklearn import metrics<choose_model_class>
sample_submission['label'] = 0.99*y_test + 0.01* y_test_knn
sample_submission.to_csv('submission_blend_3.csv', index=False)
 sample_submission.head()
lr = LogisticRegression()
model = lr.fit(X_train,y_train)
 predictions_lr = model.predict(X_test )<predict_on_test>
sample_submission['id'] = ids
sample_submission['label'] = y_test**1.001
sample_submission.to_csv('submission_lr_1.csv', index=False)
 sample_submission.head()
predictions_lr = model.predict(X_test )<compute_test_metric>
sample_submission['id'] = ids
sample_submission['label'] = y_test**1.005
sample_submission.to_csv('submission_lr_2.csv', index=False)
 sample_submission.head()
print(pd.crosstab(predictions_lr,submission['Survived'],margins=True,rownames=['Previsto'],colnames=[' Real']))
print(metrics.classification_report(y_test,predictions_lr))<create_dataframe>
sample_submission['id'] = ids
sample_submission['label'] = y_test**1.01
sample_submission.to_csv('submission_lr_3.csv', index=False)
 sample_submission.head()
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions_lr} )<save_to_csv>
sample_submission['id'] = ids
sample_submission['label'] = y_test**0.999
sample_submission.to_csv('submission_lr_4.csv', index=False)
 sample_submission.head()
output = output.to_csv("My_submission_Logistic.csv", index=False )<import_modules>
sample_submission['id'] = ids
sample_submission['label'] = y_test**0.995
sample_submission.to_csv('submission_lr_5.csv', index=False)
 sample_submission.head()
from sklearn.neighbors import KNeighborsClassifier<import_modules>
sample_submission['id'] = ids
sample_submission['label'] = y_test**0.99
sample_submission.to_csv('submission_lr_6.csv', index=False)
 sample_submission.head()
from sklearn.neighbors import KNeighborsClassifier<import_modules>
sample_submission['label'] = 0.5*y_test_xgb_1 + 0.5* y_test
sample_submission.to_csv('submission_blend_4.csv', index=False)
 sample_submission.head()
from sklearn.neighbors import KNeighborsClassifier<choose_model_class>
sample_submission['label'] =(0.5*y_test_xgb_1 + 0.5* y_test)**0.99
sample_submission.to_csv('submission_blend_5.csv', index=False)
 sample_submission.head()
<choose_model_class><EOS> 
sample_submission['label'] =(0.5*y_test_xgb_1 + 0.5* y_test)**1.01
sample_submission.to_csv('submission_blend_6.csv', index=False)
 sample_submission.head()