Kaggle competition: Titanic - Machine Learning from Disaster
%%time
test_shap_preds = model.predict(dtest, pred_contribs=True)
<predict_on_test>
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
%%time
train_shap_preds = model.predict(dtrain, pred_contribs=True )<predict_on_test>
data = pd.read_csv('.. /input/train.csv')
 data_test = pd.read_csv('.. /input/test.csv' )
test_preds = model.predict(dtest )<save_to_csv>
ID = data_test.PassengerId
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<install_modules>
y_train = data.Survived
%%time
!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>
print("Nan'ы присутсвуют в:")
 for x in data:
if data[x].isnull().sum() > 0:
print(x )
xgb.__version__<import_modules>
data.isnull().sum()
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>
data.isnull().sum()
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
print("А у нас ", int(data.Survived.value_counts(normalize=True)[1] * 100),'% выжило',sep = "")
 print("С родителями или детьми ", int(data.Parch[data.Parch > 0].count() /data.Parch.count() * 100),'%',sep = "")
 print("С братьями или сестрами ", int(data.SibSp[data.SibSp > 0].count() /data.SibSp.count() * 100),'%',sep = "")
 print("Пенсионеров ", int(data.Age[data.Age > 65].count() /data.Age.count() * 100),'%',sep = "")
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>
print("Посмотрим распределение выживших по полу")
 data.groupby('Sex' ).Survived.mean()
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>
data.groupby(data.Age[data.Age<18] ).Survived.mean()
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
data.drop(["Cabin"],axis=1, inplace=True)
 data_test.drop(["Cabin"],axis=1, inplace=True)
 data.drop(["Ticket"],axis=1, inplace=True)
 data_test.drop(["Ticket"],axis=1, inplace=True)
 data.drop(["PassengerId"],axis=1, inplace=True)
 data_test.drop(["PassengerId"],axis=1, inplace=True )
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
import seaborn as sns
import matplotlib.pyplot as plt
train_InceptionResNetV2 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_InceptionResNetV2.npy')
 test_InceptionResNetV2 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_InceptionResNetV2.npy' )<split>
data["Status"] = data.Name.str.extract(r', (.*)\.', expand=False)
 data_test["Status"] = data_test.Name.str.extract(r', (.*)\.', expand=False )
X_train, X_val, y_train, y_val = train_test_split(train_InceptionResNetV2, target, test_size=0.1, random_state=42 )<train_model>
data.groupby('Status' ).Survived.count()
xgb_params = {
'eta': 0.05,
'max_depth': 7,
'subsample': 0.85,
'colsample_bytree': 0.7,
'objective': 'reg:logistic',
'eval_metric': 'auc',
'verbosity': 0,
'tree_method': 'gpu_hist',
'predictor': 'gpu_predictor'
}
dtrain = xgb.DMatrix(X_train, y_train)
 dval = xgb.DMatrix(X_val, y_val)
 dtest = xgb.DMatrix(test_InceptionResNetV2 )<drop_column>
data.groupby('Status' ).Survived.count()
for x in data.Status:
if data[data.Status == x].Survived.count() < 10:
data.Status.replace(x, 'OTHER', inplace= True)
 data.groupby('Status' ).Survived.count()
del train_InceptionResNetV2, test_InceptionResNetV2, X_train, X_val
gc.collect()<train_model>
data.drop(["Name"],axis=1, inplace=True)
 data_test.drop(["Name"],axis=1, inplace=True)
 data.groupby('Status' ).Survived.mean()
%%time
model = xgb.train(xgb_params, dtrain, num_boost_round=100 )<predict_on_test>
data.Embarked.mode()
val_preds = model.predict(dval )<compute_test_metric>
data.Embarked.fillna('S', inplace=True)
 data_test.Embarked.fillna('S', inplace=True )
roc_auc_score(y_val, val_preds )<compute_test_metric>
data.Age[data.Age.notnull() ]
log_loss(y_val, val_preds )<predict_on_test>
from sklearn.preprocessing import OneHotEncoder
%%time
val_shap_preds = model.predict(dval, pred_contribs=True )<predict_on_test>
data = pd.get_dummies(data)
 data_test = pd.get_dummies(data_test )
%%time
test_shap_preds = model.predict(dtest, pred_contribs=True)
<predict_on_test>
from sklearn.linear_model import LinearRegression
%%time
train_shap_preds = model.predict(dtrain, pred_contribs=True )<predict_on_test>
testforfilna = data.drop(['Age', 'Survived'], axis= 1 )
test_preds = model.predict(dtest )<save_to_csv>
testforfilna[data.Age.isnull() ]
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
from sklearn import preprocessing
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
from sklearn.linear_model import LogisticRegression
import os 
<load_from_csv>
testforfilna =preprocessing.scale(testforfilna )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
data["FamSize"] = data.SibSp +data.Parch
data['Alone'] = data.FamSize == 0
data.Alone
data_test["FamSize"] = data_test.SibSp +data_test.Parch
data_test['Alone'] = data_test.FamSize == 0
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>
data.drop("AgeGroup", axis = 1, inplace =True)
 data_test.drop("AgeGroup", axis = 1, inplace =True )
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>
data.isna().sum()
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
data_test.isna().sum()
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
lr = LogisticRegression(C=2)
 lr.fit(data.drop('Survived', axis=1), data.Survived)
 kf = KFold(n_splits=10,shuffle=True,random_state=42)
 scores = cross_val_score(lr,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores.mean()
train_DenseNet169 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/train_DenseNet169.npy')
 test_DenseNet169 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_2/test_DenseNet169.npy' )<split>
data_test.drop(['Status_Col', 'Status_Dona', 'Status_Dr'], axis= 1, inplace=True )
X_train, X_val, y_train, y_val = train_test_split(train_DenseNet169, target, test_size=0.1, random_state=42 )<train_model>
KNN = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=7, p=2,
weights='uniform')
 KNN.fit(data.drop('Survived', axis=1), data.Survived)
 scoresKNN = cross_val_score(KNN,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scoresKNN.mean()
%%time
lr = LogisticRegression(C=0.002, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
tr = DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=5,
max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort=False, random_state=None,
splitter='best')
 tr.fit(data.drop('Survived', axis=1), data.Survived)
 scorestr = cross_val_score(tr,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scorestr.mean()
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
rf = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=5, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=20, n_jobs=None,
oob_score=False, random_state=None, verbose=0,
warm_start=False)
 rf.fit(data.drop('Survived', axis=1), data.Survived)
 scoresrf = cross_val_score(rf,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scoresrf.mean()
roc_auc_score(y_val, val_preds )<compute_test_metric>
gr = GradientBoostingClassifier(learning_rate=0.1, n_estimators=250,max_depth=10)
 gr.fit(data.drop('Survived', axis=1), data.Survived)
 scores_gr = cross_val_score(tr,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores_gr.mean()
log_loss(y_val, val_preds )<predict_on_test>
mlpc = MLPClassifier(max_iter=5000)
 mlpc.fit(data.drop('Survived', axis=1), data.Survived)
 scores_mlpc = cross_val_score(mlpc,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores_mlpc.mean()
test_preds = lr.predict_proba(test_DenseNet169)[:,1]<save_to_csv>
data.drop(['Status_OTHER'], inplace=True, axis=1)
 data_test.drop(['Status_Rev'], inplace=True, axis=1 )
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
XGBC = XGBClassifier(n_estimators=1000, learning_rate=0.05, max_depth=3)
 XGBC.fit(data.drop('Survived', axis=1), data.Survived);
XGBC_scores = cross_val_score(XGBC,data.drop(['Survived'], axis=1), data.Survived, cv = kf);
XGBC_scores.mean()
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
from sklearn.linear_model import LogisticRegression
import os 
<load_from_csv>
preds = XGBC.predict(data_test)
 submission = pd.DataFrame({
"PassengerId": ID,
"Survived": preds
})
 submission.to_csv('titanic.csv', index=False )
sample_submission = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')
 sample_submission.tail()<define_variables>
from sklearn.model_selection import GridSearchCV
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>
from sklearn.model_selection import GridSearchCV
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>
lgbm = LGBMClassifier(n_estimators=1000, random_state=5)
 lgbm.fit(data.drop('Survived', axis=1), data.Survived)
 scores_lgbm = cross_val_score(lgbm,data.drop(['Survived'], axis=1), data.Survived, cv = kf)
 scores_lgbm.mean()
ids = [int(x.split('.')[0])for x in test_image_list]
ids[:20]<load_pretrained>
params = {'n_estimators': list(range(1,100)) ,}
gs = GridSearchCV(RandomForestClassifier() , params, cv=5)
 gs.fit(data.drop('Survived', axis=1), data.Survived)
target = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/target.npy' )<load_pretrained>
gs.best_params_
train_InceptionResNetV2 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/train_InceptionResNetV2.npy')
 test_InceptionResNetV2 = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_1/test_InceptionResNetV2.npy' )<split>
VC = VotingClassifier([('lr', rf),('tr', lr),('lgbm', XGBC)]);
VC.fit(data.drop('Survived', axis=1), data.Survived);
scores_VC = cross_val_score(VC,data.drop(['Survived'], axis=1), data.Survived, cv = kf);
scores_VC.mean()
X_train, X_val, y_train, y_val = train_test_split(train_InceptionResNetV2, target, test_size=0.1, random_state=42 )<train_model>
svc = SVC()
svc.fit(data.drop('Survived', axis=1), data.Survived);
svc_scores = cross_val_score(svc,data.drop(['Survived'], axis=1), data.Survived, cv = kf);
svc_scores.mean()
%%time
lr = LogisticRegression(C=0.005, max_iter=10000)
 lr.fit(X_train, y_train )<predict_on_test>
from sklearn.linear_model import Lasso
val_preds = lr.predict_proba(X_val)[:,1]<compute_test_metric>
from sklearn.linear_model import Lasso
<compute_test_metric><EOS> 
ls = LogisticRegression(C=0.000002)
 ls.fit(data.drop('Survived', axis=1), data.Survived)
 ls.coef_