Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
item['sub_name_1'] = LabelEncoder().fit_transform(item.sub_name1)
 item['sub_name_2'] = LabelEncoder().fit_transform(item.sub_name2)

item.head()<concatenate>
df_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 df_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 id_train = df_train['PassengerId']
id_test = df_test['PassengerId']
df = pd.concat([df_train, df_test])
 df = df.set_index('PassengerId')
 df.info()
df
len(set(test.item_id)- set(test.item_id ).intersection(set(train.item_id)) )<data_type_conversions>
def category_to_one_hot(df, name_column):
one_hot = pd.get_dummies(df[name_column], prefix=name_column, prefix_sep='-')
 df = pd.concat([df, one_hot], axis=1)
 df = df.drop(name_column, axis=1)
 return df, one_hot
def exclude_low_category(df, name_column, threshold_using_category):
df_value_counts = df[name_column].value_counts()
excluding_categories = df_value_counts.loc[df_value_counts < threshold_using_category].index
for c in excluding_categories:
df.loc[df[name_column] == c, name_column] = None
return df
matrix = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train[train.date_block_num==i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix['date_block_num'] = matrix['date_block_num'].astype(np.int8)
 matrix['shop_id'] = matrix['shop_id'].astype(np.int8)
 matrix['item_id'] = matrix['item_id'].astype(np.int16)
 matrix.sort_values(cols,inplace=True )<feature_engineering>
name_column = 'Pclass'
print(df[name_column].value_counts(dropna=False))
df, one_hot = category_to_one_hot(df, name_column)
 one_hot
train['revenue'] = train['item_price'] * train['item_cnt_day']<groupby>
name_column = 'Name'
df[name_column] = df[name_column].str.extract(', (.*?\.) ')
 print(df[name_column].value_counts(dropna=False))
df = exclude_low_category(df, name_column, 10)
 print(df[name_column].value_counts(dropna=False))
df, one_hot = category_to_one_hot(df, name_column)
 one_hot
cols = ['date_block_num','shop_id','item_id']
group = train.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True )<merge>
name_column = 'Sex'
print(df[name_column].value_counts(dropna=False))
df, one_hot = category_to_one_hot(df, name_column)
 one_hot
matrix = pd.merge(matrix, group, on=cols, how='left')
 matrix['item_cnt_month'] =(matrix['item_cnt_month'] 
.astype(np.float32)
.fillna(0))<data_type_conversions>
name_column = 'Ticket'
df[name_column] = df[name_column].replace('(|^)[0-9]*', '', regex=True)
 df[name_column] = df[name_column].replace('(/|\.).*', '', regex=True)
 df.loc[df[name_column] == '', name_column] = None
print(df[name_column].value_counts(dropna=False))
df = exclude_low_category(df, name_column, 10)
 print(df[name_column].value_counts(dropna=False))
df, one_hot = category_to_one_hot(df, name_column)
 one_hot
test['date_block_num'] = 34
test['date_block_num'] = test['date_block_num'].astype(np.int8)
 test['shop_id'] = test['shop_id'].astype(np.int8)
 test['item_id'] = test['item_id'].astype(np.int16)
 test.head()<concatenate>
name_column = 'Cabin'
df['Cabin'] = df['Cabin'].replace('.*', '', regex=True)
 df['Cabin'] = df['Cabin'].replace('[0-9]*', '', regex=True)
 print(df[name_column].value_counts(dropna=False))
df = exclude_low_category(df, name_column, 10)
 print(df[name_column].value_counts(dropna=False))
df, one_hot = category_to_one_hot(df, name_column)
 one_hot
matrix = pd.concat([matrix, test], ignore_index = True, keys = cols, sort = False)
 matrix.fillna(0, inplace = True)
 matrix.head()<merge>
name_column = 'Embarked'
print(df[name_column].value_counts(dropna=False))
df, one_hot = category_to_one_hot(df, name_column)
 one_hot
matrix = pd.merge(matrix, shop, on = 'shop_id', how = 'left')
 matrix = pd.merge(matrix, item, on = 'item_id', how = 'left')
 matrix = pd.merge(matrix, item_category, on = 'item_category_id', how = 'left')
 matrix.head()<drop_column>
def standardize_df(df, name_column):
x = np.array(df.loc[df[name_column].notnull() , name_column])
 standardized_x =(x - x.mean())/ x.std(ddof=1)
 df.loc[df[name_column].notnull() , name_column] = standardized_x
df.loc[df[name_column].isnull() , name_column] = 888
return df
matrix.drop(['ID'], axis=1, inplace=True)
 matrix.head()<data_type_conversions>
name_column = 'Age'
df = standardize_df(df, name_column)
 df[name_column]
matrix['shop_city'] = matrix['shop_city'].astype(np.int8)
 matrix['shop_type'] = matrix['shop_type'].astype(np.int8)
 matrix['item_category_id'] = matrix['item_category_id'].astype(np.int8)
 matrix['sub_name_1'] = matrix['sub_name_1'].astype(np.int16)
 matrix['sub_name_2'] = matrix['sub_name_2'].astype(np.int16)

matrix['item_type_code'] = matrix['item_type_code'].astype(np.int8)
 matrix['item_name_code'] = matrix['item_name_code'].astype(np.int8)
 matrix.dtypes<feature_engineering>
name_column = 'SibSp'
df = standardize_df(df, name_column)
 df[name_column]
matrix['month'] = matrix['date_block_num'] % 12
matrix.head()<categorify>
name_column = 'Parch'
df = standardize_df(df, name_column)
 df[name_column]
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8)
 matrix.head()<categorify>
name_column = 'Fare'
df = standardize_df(df, name_column)
 df[name_column]
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted[col+'_lag_'+str(i)] = shifted[col+'_lag_'+str(i)].astype(np.float16)
 shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
matrix = lag_feature(matrix, [1,2,3,6,12], 'item_cnt_month')
 matrix.head()<merge>
name_class = 'Survived'
y_train = np.array(df_train[name_class])
 X_train = np.array(df_train.drop(name_class, axis=1))
X_test = np.array(df_test.drop(name_class, axis=1))
group = matrix.groupby(['date_block_num'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num'], how='left')
 matrix['date_avg_item_cnt'] = matrix['date_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_avg_item_cnt')
 matrix.drop(['date_avg_item_cnt'], axis=1, inplace=True)
 matrix.head(10 )<merge>
param_grid = {
'max_depth': [5, 10, 15],
'min_samples_split': [10, 20, 30],
'n_estimators': [100, 200, 300],
'min_samples_leaf': [5, 10, 15],
'n_jobs': [4],
"bootstrap": [True],
"criterion": ["entropy"]
}
grid = GridSearchCV(estimator=RandomForestClassifier(random_state=0),
param_grid=param_grid,
scoring="accuracy",
cv=10)
 grid.fit(X_train, y_train)
 print(f"Best Score: {grid.best_score_}, Param: {grid.best_params_}" )
<merge>
best_model = grid.best_estimator_
y_pred = best_model.predict(X_test)
 y_pred = np.array(y_pred, dtype=np.int )
group = matrix.groupby(['date_block_num','shop_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id'], how='left')
 matrix['date_shop_avg_item_cnt'] = matrix['date_shop_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_avg_item_cnt')
 matrix.head(10 )<merge>
submission_df = pd.DataFrame({'PassengerId': id_test, 'Survived': y_pred})
 submission_df.to_csv('submission.csv', index=False )