Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
!pip install seaborn==0.11.0
%%time
train,n_col=create_lag_features(train,"total_cnt_for_item_categories")
 train=train.fillna(0)
 train=train.drop(["total_cnt_for_item_categories"],axis=1)
 numeric=numeric+n_col<groupby>
pd.set_option('display.max_colwidth', None)
 pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None)
 print(sns.__version__)
 sns.set(rc={"font.size":18,"axes.titlesize":30,"axes.labelsize":18,
"axes.titlepad":22, "axes.labelpad":18, "legend.fontsize":15,
"legend.title_fontsize":15, "figure.titlesize":35})
 init_notebook_mode(connected=True)
mean_price=sales_train_df[["date_block_num","shop_id","item_id","item_price"]].groupby(["date_block_num","shop_id","item_id"] ).mean()
mean_price.columns=["mean_price"]
mean_price.head()<merge>
df_train = pd.read_csv("/kaggle/input/titanic/train.csv")
 df_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 df_train.head(5 )
train=train.join(mean_price,on=["date_block_num","shop_id","item_id"],how="left")
 train.fillna(0,inplace=True)
 train["mean_price"]=train["mean_price"].astype(np.float16)
 train.head().T<drop_column>
for col in ['Sex', 'Cabin', 'Ticket', 'Embarked']:
df_train[col] = df_train[col].astype('category')
 df_test[col] = df_test[col].astype('category')
 df_train.info(verbose=True )
%%time
train,n_col=create_lag_features(train,"mean_price")
 train=train.fillna(0)
 train=train.drop(["mean_price"],axis=1)
 numeric=numeric+n_col<groupby>
full_df = pd.concat([df_train, df_test] ).reset_index(drop=True)
 train_shape = df_train.shape
test_shape = df_test.shape
mean_price_for_items=sales_train_df[["date_block_num","item_id","item_price"]].groupby(["date_block_num","item_id"] ).mean()
mean_price_for_items.columns=["mean_price_for_items"]
mean_price_for_items.head()<merge>
full_df.loc[:, 'Sex'] =(full_df.loc[:, 'Sex'] == 'female' ).astype(int )
train=train.join(mean_price_for_items,on=["date_block_num","item_id"],how="left")
 train.fillna(0,inplace=True)
 train["mean_price_for_items"]=train["mean_price_for_items"].astype(np.float16)
 train.head().T<drop_column>
full_df['Title_C'] = full_df['Title']
full_df = pd.get_dummies(full_df, columns=["Embarked","Title_C"],\
prefix=["Emb","Title"], drop_first=False)
 title_dict = {'Mr': 1, 'Miss': 2, 'Mrs': 3, 'Other': 4, 'Royal': 5, 'Master': 6}
full_df['Title'] = full_df['Title'].map(title_dict ).astype('int' )
%%time
train,n_col=create_lag_features(train,"mean_price_for_items")
 train=train.fillna(0)
 train=train.drop(["mean_price_for_items"],axis=1)
 numeric=numeric+n_col<groupby>
Fsize_dict = {'Alone':3, 'Small':2, 'Big':1}
full_df['Fsize_Cat'] = full_df['Fsize_Cat'].map(Fsize_dict ).astype('int' )
mean_price_for_item_categories=sales_train_df[["date_block_num","item_category_id","item_price"]].groupby(["date_block_num","item_category_id"] ).mean()
mean_price_for_item_categories.columns=["mean_price_for_item_categories"]
mean_price_for_item_categories.head()<merge>
full_df['Family_Name'] = full_df['Name'].str.extract('([A-Za-z]+.[A-Za-z]+)\,', expand=True )
train=train.join(mean_price_for_item_categories,on=["date_block_num","item_category_id"],how="left")
 train.fillna(0,inplace=True)
 train["mean_price_for_item_categories"]=train["mean_price_for_item_categories"].astype(np.float16)
 train.head().T<drop_column>
MEAN_SURVIVAL_RATE = round(np.mean(df_train['Survived']), 4)
 full_df['Family_Friends_Surv_Rate'] = MEAN_SURVIVAL_RATE
full_df['Surv_Rate_Invalid'] = 1
for _, grp_df in full_df[['Survived', 'Family_Name', 'Fare', 'Ticket', 'PassengerId']].groupby(['Family_Name', 'Fare']):
if(len(grp_df)> 1):
if(grp_df['Survived'].isnull().sum() != len(grp_df)) :
for ind, row in grp_df.iterrows() :
full_df.loc[full_df['PassengerId'] == row['PassengerId'],
'Family_Friends_Surv_Rate'] = round(grp_df['Survived'].mean() , 4)
 full_df.loc[full_df['PassengerId'] == row['PassengerId'],
'Surv_Rate_Invalid'] = 0
for _, grp_df in full_df[['Survived', 'Family_Name', 'Fare', 'Ticket', 'PassengerId', 'Family_Friends_Surv_Rate']].groupby('Ticket'):
if(len(grp_df)> 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Friends_Surv_Rate'] == 0.) |(row['Family_Friends_Surv_Rate'] == MEAN_SURVIVAL_RATE):
if(grp_df['Survived'].isnull().sum() != len(grp_df)) :
full_df.loc[full_df['PassengerId'] == row['PassengerId'],
'Family_Friends_Surv_Rate'] = round(grp_df['Survived'].mean() , 4)
 full_df.loc[full_df['PassengerId'] == row['PassengerId'],
'Surv_Rate_Invalid'] = 0
%%time
train,n_col=create_lag_features(train,"mean_price_for_item_categories")
 train=train.fillna(0)
 train=train.drop(["mean_price_for_item_categories"],axis=1)
 numeric=numeric+n_col<feature_engineering>
full_df['Cabin'] = full_df['Cabin'].astype('category')
 full_df['Cabin'] = full_df['Cabin'].cat.add_categories('U')
 full_df['Cabin_Clean'] = full_df['Cabin'].fillna('U')
 full_df['Cabin_Clean'] = full_df['Cabin_Clean'].str.strip(' ' ).str[0]
cabin_dict = {'A':9, 'B':8, 'C':7, 'D':6, 'E':5, 'F':4, 'G':3, 'T':2, 'U':1}
full_df['Cabin_Clean'] = full_df['Cabin_Clean'].map(cabin_dict ).astype('int')
sales_train_df["revenue"]=sales_train_df["item_price"]*sales_train_df["item_cnt_day"]
total_revenue=sales_train_df[["date_block_num","shop_id","revenue"]].groupby(["date_block_num","shop_id"] ).sum()
total_revenue.columns=["total_shop_revenue"]
total_revenue.head()<merge>
full_df = pd.get_dummies(full_df, columns=["Tkt_Clean"],\
prefix=["Tkt"], drop_first=True )
train=train.join(total_revenue,on=["date_block_num","shop_id"],how="left")
 train["total_shop_revenue"]=train["total_shop_revenue"].astype(np.float32)
 train.fillna(0,inplace=True)
 train.head().T<drop_column>
def fare_cat(fare):
if fare <= 7.0:
return 1
elif fare <= 39 and fare > 7.0:
return 2
else:
return 3
full_df.loc[:, 'Fare_Cat'] = full_df['Fare'].apply(fare_cat ).astype('int' )
%%time
train,n_col=create_lag_features(train,"total_shop_revenue")
 train=train.fillna(0.0)
 train=train.drop(["total_shop_revenue"],axis=1)
 numeric=numeric+n_col<groupby>
full_df.loc[:, 'Fare_Family_Size'] = full_df['Fare']/full_df['Family_Size']
full_df.loc[:, 'Fare_Cat_Pclass'] = full_df['Fare_Cat']*full_df['Pclass']
full_df.loc[:, 'Fare_Cat_Title'] = full_df['Fare_Cat']*full_df['Title']
full_df.loc[:, 'Fsize_Cat_Title'] = full_df['Fsize_Cat']*full_df['Title']
full_df.loc[:, 'Fsize_Cat_Fare_Cat'] = full_df['Fare_Cat']/full_df['Fsize_Cat'].astype('int')
 full_df.loc[:, 'Pclass_Title'] = full_df['Pclass']*full_df['Title']
full_df.loc[:, 'Fsize_Cat_Pclass'] = full_df['Fsize_Cat']*full_df['Pclass']
groups = train.groupby(train.date_block_num ).groups
sorted_groups = [value for(key, value)in sorted(groups.items())]
cv=[(np.concatenate(sorted_groups[:8]),np.concatenate(sorted_groups[8:])) , 
(np.concatenate(sorted_groups[:16]),np.concatenate(sorted_groups[16:])) , 
(np.concatenate(sorted_groups[:24]),np.concatenate(sorted_groups[24:])) ]<prepare_x_and_y>
colsToRemove = []
cols = ['Tkt_AQ', 'Tkt_AS', 'Tkt_C', 'Tkt_CA',
'Tkt_CASOTON', 'Tkt_FC', 'Tkt_FCC', 'Tkt_Fa', 'Tkt_LINE', 'Tkt_LP',
'Tkt_NUM', 'Tkt_PC', 'Tkt_PP', 'Tkt_PPP', 'Tkt_SC', 'Tkt_SCA',
'Tkt_SCAH', 'Tkt_SCAHBasle', 'Tkt_SCOW', 'Tkt_SCPARIS', 'Tkt_SCParis',
'Tkt_SOC', 'Tkt_SOP', 'Tkt_SOPP', 'Tkt_SOTONO', 'Tkt_SOTONOQ',
'Tkt_SP', 'Tkt_STONO', 'Tkt_STONOQ', 'Tkt_SWPP', 'Tkt_WC',
'Tkt_WEP', 'Fare_Cat', 'Fare_Family_Size', 'Fare_Cat_Pclass',
'Fare_Cat_Title', 'Fsize_Cat_Title', 'Fsize_Cat_Fare_Cat',
'Pclass_Title', 'Fsize_Cat_Pclass']
for col in cols:
if full_df[col][:train_shape[0]].std() == 0:
colsToRemove.append(col)
 full_df.drop(colsToRemove, axis=1, inplace=True)
 print("Removed `{}` Constant Columns
".format(len(colsToRemove)))
 print(colsToRemove )
y_train=train["item_cnt_day"]<data_type_conversions>
imp_features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Title',
'Name_Length', 'Emb_C', 'Emb_Q', 'Emb_S','Family_Size',
'Fsize_Cat', 'Family_Friends_Surv_Rate', 'Surv_Rate_Invalid',
'Cabin_Clean','Ticket_Frequency', 'Tkt_AS', 'Tkt_C', 'Tkt_CA',
'Tkt_CASOTON', 'Tkt_FC', 'Tkt_FCC', 'Tkt_Fa', 'Tkt_LINE',
'Tkt_NUM', 'Tkt_PC', 'Tkt_PP', 'Tkt_PPP', 'Tkt_SC', 'Tkt_SCA',
'Tkt_SCAH', 'Tkt_SCAHBasle', 'Tkt_SCOW', 'Tkt_SCPARIS', 'Tkt_SCParis',
'Tkt_SOC', 'Tkt_SOP', 'Tkt_SOPP', 'Tkt_SOTONO', 'Tkt_SOTONOQ',
'Tkt_SP', 'Tkt_STONO', 'Tkt_SWPP', 'Tkt_WC',
'Tkt_WEP', 'Fare_Cat', 'Fare_Family_Size', 'Fare_Cat_Pclass',
'Fare_Cat_Title', 'Fsize_Cat_Title', 'Fsize_Cat_Fare_Cat',
'Pclass_Title', 'Fsize_Cat_Pclass']
imputer = KNNImputer(n_neighbors=10, missing_values=np.nan)
 imputer.fit(full_df[imp_features] )
X_train_categorical=train[categorical]
X_test_categorical=test_df[categorical]
X_train_categorical["subcategory"]=X_train_categorical["subcategory"].astype(str)
 X_test_categorical["subcategory"]=X_test_categorical["subcategory"].astype(str)
 X_test_categorical["year"]=X_test_categorical["year"].astype(np.int16)
 X_test_categorical["month"]=X_test_categorical["month"].astype(np.int8)
 X_test_categorical.loc[X_test_categorical.category=="PC ","category"]="Игры PC "
X_test_categorical.loc[X_test_categorical.subcategory==" Гарнитуры/Наушники","subcategory"]=" Аксессуары для игр"<categorify>
full_df.loc[:, imp_features] = pd.DataFrame(imputer.transform(full_df[imp_features]), index=full_df.index, columns = imp_features )
for feature in categorical:
le=LabelEncoder()
print(feature)
 X_train_categorical[feature]=le.fit_transform(X_train_categorical[feature])
 X_test_categorical[feature]=le.transform(X_test_categorical[feature] )<prepare_x_and_y>
df_train_final = full_df[:train_shape[0]]
df_test_final = full_df[train_shape[0]:]
X_train_numeric=train[numeric]
X_test_numeric=test_df[numeric]<concatenate>
viz_features = ['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex',
'Age', 'SibSp','Parch', 'Ticket', 'Fare', 'Cabin',
'Title', 'Name_Length', 'Family_Friends_Surv_Rate',
'Ticket_Frequency']
train_viz = df_train_final[viz_features]
label_cat_not_num_train=pd.concat([X_train_categorical,X_train_numeric],axis=1)
 label_cat_not_num_train.head().T<concatenate>
full_df.to_pickle("full_df")
 df_train_final.to_pickle("df_train_final")
 df_test_final.to_pickle("df_test_final" )
label_cat_not_num_test=pd.concat([X_test_categorical,X_test_numeric],axis=1)
 label_cat_not_num_test.head().T<data_type_conversions>
scaler_cols = ['Age', 'Fare', 'Name_Length', 'Family_Size', 'Name_Length',
'Ticket_Frequency', 'Fare_Family_Size', 'Fare_Cat_Pclass']
std = StandardScaler()
std.fit(df_train_final[scaler_cols])
 df_train_final.loc[:, scaler_cols] = pd.DataFrame(std.transform(df_train_final[scaler_cols]), index=df_train_final.index, columns = scaler_cols)
 df_test_final.loc[:, scaler_cols] = pd.DataFrame(std.transform(df_test_final[scaler_cols]), index=df_test_final.index, columns = scaler_cols )
def downcast_type(df):
for feature in categorical:
df[feature]=df[feature].astype(np.int8 )<compute_test_metric>
features = ['Pclass', 'Sex', 'Age', 'Fare', 'Title', 'Name_Length', 'Emb_C',
'Emb_Q', 'Emb_S', 'Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Mrs',
'Title_Other', 'Title_Royal', 'Family_Size', 'Fsize_Cat',
'Family_Friends_Surv_Rate', 'Surv_Rate_Invalid', 'Cabin_Clean',
'Ticket_Frequency', 'Tkt_AS', 'Tkt_C', 'Tkt_CA',
'Tkt_CASOTON', 'Tkt_FC', 'Tkt_FCC', 'Tkt_Fa', 'Tkt_LINE',
'Tkt_NUM', 'Tkt_PC', 'Tkt_PP', 'Tkt_PPP', 'Tkt_SC', 'Tkt_SCA',
'Tkt_SCAH', 'Tkt_SCAHBasle', 'Tkt_SCOW', 'Tkt_SCPARIS', 'Tkt_SCParis',
'Tkt_SOC', 'Tkt_SOP', 'Tkt_SOPP', 'Tkt_SOTONO', 'Tkt_SOTONOQ', 'Tkt_SP',
'Tkt_STONO', 'Tkt_SWPP', 'Tkt_WC', 'Tkt_WEP', 'Fare_Cat',
'Fare_Family_Size', 'Fare_Cat_Pclass', 'Fare_Cat_Title',
'Fsize_Cat_Title', 'Fsize_Cat_Fare_Cat', 'Pclass_Title',
'Fsize_Cat_Pclass', 'Child', 'Senior']
features_train = ['Survived', 'Pclass', 'Sex', 'Age', 'Fare', 'Title', 'Name_Length', 'Emb_C',
'Emb_Q', 'Emb_S', 'Title_Master', 'Title_Miss', 'Title_Mr', 'Title_Mrs',
'Title_Other', 'Title_Royal', 'Family_Size', 'Fsize_Cat',
'Family_Friends_Surv_Rate', 'Surv_Rate_Invalid', 'Cabin_Clean',
'Ticket_Frequency', 'Tkt_AS', 'Tkt_C', 'Tkt_CA',
'Tkt_CASOTON', 'Tkt_FC', 'Tkt_FCC', 'Tkt_Fa', 'Tkt_LINE',
'Tkt_NUM', 'Tkt_PC', 'Tkt_PP', 'Tkt_PPP', 'Tkt_SC', 'Tkt_SCA',
'Tkt_SCAH', 'Tkt_SCAHBasle', 'Tkt_SCOW', 'Tkt_SCPARIS', 'Tkt_SCParis',
'Tkt_SOC', 'Tkt_SOP', 'Tkt_SOPP', 'Tkt_SOTONO', 'Tkt_SOTONOQ', 'Tkt_SP',
'Tkt_STONO', 'Tkt_SWPP', 'Tkt_WC', 'Tkt_WEP', 'Fare_Cat',
'Fare_Family_Size', 'Fare_Cat_Pclass', 'Fare_Cat_Title',
'Fsize_Cat_Title', 'Fsize_Cat_Fare_Cat', 'Pclass_Title',
'Fsize_Cat_Pclass', 'Child', 'Senior']
df_train_final = df_train_final[features_train]
df_test_final = df_test_final[features]
def RMSE(y,predictions):
return np.sqrt(mean_squared_error(y,predictions))
scorer=make_scorer(RMSE,False )<compute_train_metric>
corr_mat = df_train_final.astype(float ).corr()
corr_mat_fil = corr_mat.loc[:, 'Survived'].sort_values(ascending=False)
 corr_mat_fil = pd.DataFrame(data=corr_mat_fil[1:] )
%%time
baseline = -cross_val_score(
 XGBRegressor(max_depth=10,
subsample=0.8,
colsample_bytree=0.9,
colsample_bylevel=0.7,
min_child_weight=200,
n_estimators=1000,
learning_rate=0.025,
objective="reg:squarederror",
tree_method="hist"), label_cat_not_num_train, y_train, scoring=scorer,cv=cv
).mean()
print(baseline )<choose_model_class>
features = df_test_final.columns.to_list()
X_train = df_train_final[features]
Y_train = df_train_final['Survived']
X_test = df_test_final
%%time
estimator=XGBRegressor(max_depth=10,
subsample=0.8,
colsample_bytree=0.9,
colsample_bylevel=0.7,
min_child_weight=200,
n_estimators=1000,
learning_rate=0.025,
objective="reg:squarederror",
tree_method="hist")
 estimator.fit(label_cat_not_num_train,y_train)
 importances=estimator.feature_importances_
predictions=estimator.predict(label_cat_not_num_test )<prepare_output>
from sklearn.model_selection import cross_val_predict, cross_val_score, cross_validate
from sklearn.model_selection import StratifiedKFold, StratifiedShuffleSplit
from sklearn.metrics import confusion_matrix, roc_curve
from sklearn.metrics import precision_score, recall_score, f1_score
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import plot_model
from tensorflow.keras.layers import Input, Dense, Dropout, AlphaDropout, BatchNormalization,Concatenate, concatenate
from tensorflow.keras.optimizers import SGD, RMSprop, Adamax, Adagrad, Adam, Nadam, SGD
from tensorflow.keras.regularizers import l2
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.metrics import *
sample_submissions["item_cnt_month"]=predictions
sample_submissions.head()<save_to_csv>
metrics = ['accuracy',
Precision() ,
Recall() ]
sample_submissions.to_csv("xgboost_lagged_features_6.csv",index=False )<set_options>
def create_model() :
model = Sequential()
model.add(Input(shape=X_train.shape[1], name='Input_'))
model.add(Dense(8, activation='relu', kernel_initializer='glorot_normal', kernel_regularizer=l2(0.001)))
 model.add(Dense(16, activation='relu', kernel_initializer='glorot_normal', kernel_regularizer=l2(0.1)))
 model.add(Dropout(0.5))
model.add(Dense(16, activation='relu', kernel_initializer='glorot_normal', kernel_regularizer=l2(0.1)))
 model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid', kernel_initializer='glorot_normal'))
model.summary()
optimize = Adam(lr = 0.0001)
 model.compile(optimizer = optimize,
loss = 'binary_crossentropy',
metrics = metrics)
 return model
%matplotlib inline<load_from_csv>
estimator = KerasClassifier(build_fn = create_model, epochs = 600, batch_size = 32, verbose = 1)
 kfold = StratifiedKFold(n_splits = 3)
 results = cross_val_score(estimator, X_train, Y_train, cv = kfold )
shops = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/shops.csv')
 items = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/items.csv')
 catgs = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv')
 sales = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv')
 testd = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv')
 sampl = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/sample_submission.csv' )<count_missing_values>
train_history = estimator.fit(X_train, Y_train, epochs = 600, batch_size = 32 )
<feature_engineering><EOS> 
y_preds = estimator.predict(X_test)
 submission = pd.read_csv(".. /input/titanic/gender_submission.csv", index_col='PassengerId')
 submission['Survived'] = y_preds.astype(int)
 submission.to_csv('submission.csv' )