Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_output>
display.Image('https://raw.githubusercontent.com/Dutta-SD/Images_Unsplash/master/Kaggle/dorian-mongel-5Rgr_zI7pBw-unsplash.jpg', width = 3000, height = 500 )
submission['diagnosis'] = preds
submission.head()<set_options>
import pandas as pd
import numpy as np
warnings.simplefilter(action='ignore', category=FutureWarning )<save_to_csv>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
submission.to_csv('submission.csv', index = False )<import_modules>
PassengerID = test_data.PassengerId
train_data.drop(['Name', 'Ticket', 'Cabin', 'PassengerId'], inplace=True, axis=1)
 test_data.drop(['Name', 'Ticket', 'Cabin', 'PassengerId'], inplace=True, axis=1)
 test_data.head()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
print(train_data.isnull().any() )
train = pd.read_csv('.. /input/train_V2.csv')
 test = pd.read_csv('.. /input/test_V2.csv' )<drop_column>
test_data.isnull().any()
train['totalDistance']=train['rideDistance']+train['walkDistance']+train['swimDistance']
train['killsWithoutMoving']=(( train['kills']>0)&(train['totalDistance']==0))
train['headshot_rate']=train['headshotKills']/train['kills']
train['playersJoined'] = train.groupby('matchId')['matchId'].transform('count')
 print(train.shape)
 train.drop(train[train['winPlacePerc'].isnull() ].index,inplace=True)
 train.drop(train[train['killsWithoutMoving']==True].index,inplace=True)
 train.drop(train[train['roadKills']>10].index,inplace=True)
 train.drop(train[train['kills']>35].index,inplace=True)
 train.drop(train[(( train['headshot_rate']==1)&(train['kills']>9)) ].index,inplace=True)
 train.drop(train[train['longestKill']>=1000].index,inplace=True)
 train.drop(train[train['walkDistance']>=10000].index,inplace=True)
 train.drop(train[train['rideDistance']>=20000].index,inplace=True)
 train.drop(train[train['swimDistance']>=2000].index,inplace=True)
 train.drop(train[train['weaponsAcquired']>=80].index,inplace=True)
 train.drop(train[train['heals']>=40].index,inplace=True)
 train.drop(train[(( train['totalDistance']==0)&(train['weaponsAcquired']>3)) ].index,inplace=True)
 print(train.shape )<feature_engineering>
y = train_data.Survived
X = train_data.drop(['Survived'], axis = 1)
 print(y.head())
 print(X.head() )
train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['headshotKillsNorm'] = train['headshotKills']*(( 100-train['playersJoined'])/100 + 1)
 train['killPlaceNorm'] = train['killPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['killPointsNorm'] = train['killPoints']*(( 100-train['playersJoined'])/100 + 1)
 train['killStreaksNorm'] = train['killStreaks']*(( 100-train['playersJoined'])/100 + 1)
 train['longestKillNorm'] = train['longestKill']*(( 100-train['playersJoined'])/100 + 1)
 train['roadKillsNorm'] = train['roadKills']*(( 100-train['playersJoined'])/100 + 1)
 train['teamKillsNorm'] = train['teamKills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['DBNOsNorm'] = train['DBNOs']*(( 100-train['playersJoined'])/100 + 1)
 train['revivesNorm'] = train['revives']*(( 100-train['playersJoined'])/100 + 1)
 train = train.drop([ 'kills', 'headshotKills', 'killPlace', 'killPoints', 'killStreaks',
'longestKill', 'roadKills', 'teamKills', 'damageDealt', 'DBNOs', 'revives'],axis=1 )<prepare_x_and_y>
import matplotlib.pyplot as plt
import seaborn as sns
target='winPlacePerc'
features=list(train.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 features.remove("killsWithoutMoving")
 features.remove("headshot_rate")
 features.remove(target)
 sample = 500000
df_sample = train.sample(sample)
 x_train=df_sample[features]
y_train=df_sample[target]<split>
X_train, X_test, y_train, y_test = X, test_data, y, None
random_seed=1
sample = 500000
x_train,x_val,y_train,y_val=train_test_split(x_train,y_train,test_size=0.1,random_state=random_seed)
 model=RandomForestRegressor(n_estimators=70,min_samples_leaf=3,max_features=0.5,n_jobs=-1 )<train_model>
X_train.reset_index(drop=True, inplace=True)
 X_test.reset_index(drop=True, inplace=True)
 y_train.reset_index(drop=True, inplace=True)
 y_train.reset_index(drop=True, inplace=True)
 X_train.info()
%%time
model.fit(x_train,y_train )<compute_test_metric>
s =(X_train.dtypes=='object')
 categorical_cols = list(s[s].index)
 numerical_cols = [ i for i in X_train.columns if not i in categorical_cols ]
numerical_cols
print('mae train:',mean_absolute_error(model.predict(x_train),y_train))
print('mae train:',mean_absolute_error(model.predict(x_val),y_val))<feature_engineering>
nm_imputer = KNNImputer()
X_train_numerical = pd.DataFrame(nm_imputer.fit_transform(X_train[numerical_cols]),
columns = numerical_cols)
 X_test_numerical = pd.DataFrame(nm_imputer.transform(X_test[numerical_cols]),
columns = numerical_cols)
test['totalDistance']=test['rideDistance']+test['walkDistance']+test['swimDistance']
test['headshot_rate']=test['headshotKills']/test['kills']
test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['headshotKillsNorm'] = test['headshotKills']*(( 100-test['playersJoined'])/100 + 1)
 test['killPlaceNorm'] = test['killPlace']*(( 100-test['playersJoined'])/100 + 1)
 test['killPointsNorm'] = test['killPoints']*(( 100-test['playersJoined'])/100 + 1)
 test['killStreaksNorm'] = test['killStreaks']*(( 100-test['playersJoined'])/100 + 1)
 test['longestKillNorm'] = test['longestKill']*(( 100-test['playersJoined'])/100 + 1)
 test['roadKillsNorm'] = test['roadKills']*(( 100-test['playersJoined'])/100 + 1)
 test['teamKillsNorm'] = test['teamKills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['DBNOsNorm'] = test['DBNOs']*(( 100-test['playersJoined'])/100 + 1)
 test['revivesNorm'] = test['revives']*(( 100-test['playersJoined'])/100 + 1)
 test = test.drop([ 'kills', 'headshotKills', 'killPlace', 'killPoints', 'killStreaks',
'longestKill', 'roadKills', 'teamKills', 'damageDealt', 'DBNOs', 'revives'],axis=1 )<predict_on_test>
X_train = X_train.drop(numerical_cols, axis = 1)
 X_test = X_test.drop(numerical_cols, axis = 1)
 X_train = X_train.join(X_train_numerical)
 X_test = X_test.join(X_test_numerical)
 X_train.isnull().any()
x_test=test[features]
pred=model.predict(x_test)
 test['winPlacePerc']=pred
submission=test[['Id','winPlacePerc']]
submission.head()<save_to_csv>
nm_imputer = SimpleImputer(strategy='most_frequent')
 X_train_numerical = pd.DataFrame(nm_imputer.fit_transform(X_train[categorical_cols]),
columns = categorical_cols)
 X_test_numerical = pd.DataFrame(nm_imputer.transform(X_test[categorical_cols]),
columns = categorical_cols )
submission.to_csv('submission.csv',index=False )<import_modules>
X_train = X_train.drop(categorical_cols, axis = 1)
 X_test = X_test.drop(categorical_cols, axis = 1)
 X_train = X_train.join(X_train_numerical)
 X_test = X_test.join(X_test_numerical)
 X_train.isnull().any()
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt<load_from_csv>
OH_encoder = OneHotEncoder(handle_unknown = 'ignore', sparse=False)
 OH_cols_train = pd.DataFrame(OH_encoder.fit_transform(X_train[categorical_cols]))
OH_cols_test = pd.DataFrame(OH_encoder.transform(X_test[categorical_cols]))
OH_cols_train.index = X_train.index
OH_cols_test.index = X_test.index
num_X_train = X_train.drop(categorical_cols, axis = 1)
 num_X_test = X_test.drop(categorical_cols, axis = 1)
 X_train = num_X_train.join(OH_cols_train, how='left')
 X_test = num_X_test.join(OH_cols_test, how='left')
 X_train.head()
develop_mode = False
if develop_mode:
df_train = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv', nrows=5000))
df_test = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv'))
else:
df_train = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv'))
df_test = reduce_mem_usage(pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv'))<train_model>
X_train_2, X_val, y_train_2, y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state = 10 )
print('The sizes of the datasets are:')
 print('Training Dataset: ', df_train.shape)
 print('Testing Dataset: ', df_test.shape )<set_options>
from tensorflow import keras
warnings.filterwarnings('ignore' )<load_from_csv>
from keras import Sequential
from keras.layers import BatchNormalization, Dense
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv')
 test_idx = df.Id
df = reduce_mem_usage(df)
 print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx<prepare_x_and_y>
model = Sequential()
model.add(Dense(128, activation = 'relu', input_shape =(10,)))
 model.add(BatchNormalization())
 model.add(Dense(64, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dense(8, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dense(1, activation = 'sigmoid'))
model.summary()
X_train, y_train, train_columns, _ = feature_engineering(True,False)
 X_test, _, _ , test_idx = feature_engineering(False,True )<drop_column>
model.compile(optimizer='adam',
loss=keras.losses.BinaryCrossentropy() ,
metrics = ['accuracy'] )
X_train =reduce_mem_usage(X_train)
 X_test = reduce_mem_usage(X_test )<train_model>
history = model.fit(
 X_train_2,
y_train_2,
batch_size=32,
epochs=20,
validation_data=(X_val, y_val)
 )
LR_model = LinearRegression(n_jobs=4, normalize=True)
 LR_model.fit(X_train,y_train )<compute_test_metric>
model.compile(optimizer='adam',
loss=keras.losses.BinaryCrossentropy() ,
metrics = ['accuracy'])
 history = model.fit(
 X_train,
y_train,
batch_size=32,
epochs=20
)
LR_model.score(X_train,y_train )<predict_on_test>
y_preds = model.predict_classes(X_test )
<feature_engineering><EOS> 
file_name = "MyTitanicSubmission.csv"
y_pred_series = pd.Series(y_preds.flatten() , name = 'Survived')
 file = pd.concat([PassengerID, y_pred_series], axis = 1)
 file.to_csv(file_name, index = False )