Kaggle competition: Titanic - Machine Learning from Disaster
train.dropna(axis=0,inplace=True )<count_missing_values>
fastai.__version__
test.isnull().sum()<count_missing_values>
pd.options.display.max_rows = 20
pd.options.display.max_columns = None
test.isnull().sum()<set_options>
path = Path('/kaggle/input/titanic-extended' )
plt.figure(figsize=(10,6))
sns.distplot(train["DBNOs"],hist=True)
 plt.show()<feature_engineering>
path.ls()
train['boosts+heals'] = train['boosts']+train['heals']
train['matchDuration_min'] = train['matchDuration']/60
train['teamwork'] = train['assists'] + train['revives']
train['revives-teamKills'] = train['revives'] - train['teamKills']
train['total_distance'] = train['swimDistance'] + train['rideDistance'] + train['walkDistance']
train['headshotKills/kills'] = train['headshotKills'] / train['kills']
train['headshotKills/kills'].fillna(0, inplace=True)
 train['headshotKills/kills'].replace(np.inf, 0, inplace=True)
 train['killPlace/maxPlace'] = train['killPlace'] / train['maxPlace']
train['killPlace/maxPlace'].fillna(0, inplace=True)
 train['killPlace/maxPlace'].replace(np.inf, 0, inplace=True)
 train['walkDistance/heals'] = train['walkDistance'] / train['heals']
train['walkDistance/heals'].fillna(0, inplace=True)
 train['walkDistance/heals'].replace(np.inf, 0, inplace=True)
 train['walkDistance/kills'] = train['walkDistance'] / train['kills']
train['walkDistance/kills'].fillna(0, inplace=True)
 train['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 train['killStreaks/kills'] = train['killStreaks'] / train['kills']
train['walkDistance/kills'].fillna(0, inplace=True)
 train['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 train['total_distance/weaponsAcquired'] = train['total_distance'] / train['weaponsAcquired']
train['total_distance/weaponsAcquired'].fillna(0, inplace=True)
 train['total_distance/weaponsAcquired'].replace(np.inf, 0, inplace=True)
 train['heals/walkDistance'] = train['heals'] /train['walkDistance']
train['heals/walkDistance'].fillna(0, inplace=True)
 train['heals/walkDistance'].replace(np.inf, 0, inplace=True)
 train['kills/walkDistance'] = train['kills'] / train['walkDistance']
train['kills/walkDistance'].fillna(0, inplace=True)
 train['kills/walkDistance'].replace(np.inf, 0, inplace=True)
 train['killPlace/kills'] = train['killPlace'] / train['kills']
train['killPlace/kills'].fillna(0, inplace=True)
 train['killPlace/kills'].replace(np.inf, 0, inplace=True)
 train['walkDistance/matchDuration'] = train["walkDistance"] / train["matchDuration"]
train['walkDistance/matchDuration'].fillna(0, inplace=True)
 train['walkDistance/matchDuration'].replace(np.inf, 0, inplace=True)
<feature_engineering>
df = pd.read_csv(path/'train.csv', low_memory=False)
 df_test = pd.read_csv(path/'test.csv', low_memory=False )
test['boosts+heals'] = test['boosts']+test['heals']
test['matchDuration_min'] = test['matchDuration']/60
test['teamwork'] = test['assists'] + test['revives']
test['revives-teamKills'] = test['revives'] - test['teamKills']
test['total_distance'] = test['swimDistance'] + test['rideDistance'] + test['walkDistance']
test['headshotKills/kills'] = test['headshotKills'] / test['kills']
test['headshotKills/kills'].fillna(0, inplace=True)
 test['headshotKills/kills'].replace(np.inf, 0, inplace=True)
 test['killPlace/maxPlace'] = test['killPlace'] / test['maxPlace']
test['killPlace/maxPlace'].fillna(0, inplace=True)
 test['killPlace/maxPlace'].replace(np.inf, 0, inplace=True)
 test['walkDistance/heals'] = test['walkDistance'] / test['heals']
test['walkDistance/heals'].fillna(0, inplace=True)
 test['walkDistance/heals'].replace(np.inf, 0, inplace=True)
 test['walkDistance/kills'] = test['walkDistance'] / test['kills']
test['walkDistance/kills'].fillna(0, inplace=True)
 test['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 test['killStreaks/kills'] = test['killStreaks'] / test['kills']
test['walkDistance/kills'].fillna(0, inplace=True)
 test['walkDistance/kills'].replace(np.inf, 0, inplace=True)
 test['total_distance/weaponsAcquired'] = test['total_distance'] / test['weaponsAcquired']
test['total_distance/weaponsAcquired'].fillna(0, inplace=True)
 test['total_distance/weaponsAcquired'].replace(np.inf, 0, inplace=True)
 test['heals/walkDistance'] = test['heals'] /test['walkDistance']
test['heals/walkDistance'].fillna(0, inplace=True)
 test['heals/walkDistance'].replace(np.inf, 0, inplace=True)
 test['kills/walkDistance'] = test['kills'] / test['walkDistance']
test['kills/walkDistance'].fillna(0, inplace=True)
 test['kills/walkDistance'].replace(np.inf, 0, inplace=True)
 test['killPlace/kills'] = test['killPlace'] / test['kills']
test['killPlace/kills'].fillna(0, inplace=True)
 test['killPlace/kills'].replace(np.inf, 0, inplace=True)
 test['walkDistance/matchDuration'] = test["walkDistance"] / test["matchDuration"]
test['walkDistance/matchDuration'].fillna(0, inplace=True)
 test['walkDistance/matchDuration'].replace(np.inf, 0, inplace=True)
<drop_column>
procs = [Categorify, FillMissing]
dropped_cols = ["Id", "matchId", "groupId", "matchType"]
train.drop(dropped_cols,axis=1,inplace=True)
 test.drop(dropped_cols,axis=1,inplace=True )<prepare_x_and_y>
splits = RandomSplitter(valid_pct=0.2 )(range_of(df))
X = train.drop('winPlacePerc',axis=1)
 y = train['winPlacePerc']<split>
dep_var='Survived'
test_size=0.20
seed=42
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed )<set_options>
cont,cat = cont_cat_split(df, 1, dep_var=dep_var )
del train
gc.collect()<init_hyperparams>
to = TabularPandas(df, procs, cat, cont, y_names=dep_var, splits=splits, y_block=CategoryBlock() )
params2 = {
"objective" : "regression",
"metric" : "mae",
"num_leaves" : 150,
"learning_rate" : 0.03,
"bagging_fraction" : 0.9,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.5,
'min_data_in_leaf':1900,
'lambda_l2':9
}<choose_model_class>
X_train, y_train = to.train.xs,to.train.y
X_valid, y_valid = to.valid.xs,to.valid.y
reg2 = lgb.LGBMRegressor(**params2, n_estimators=2000 )<train_model>
m = RandomForestClassifier(n_estimators=100, n_jobs=-1 )
reg2.fit(X_train, y_train )<predict_on_test>
m.fit(X_train,y_train )
pred2 = reg2.predict(X_test, num_iteration=reg2.best_iteration_ )<compute_test_metric>
from sklearn.metrics import accuracy_score
mean_absolute_error(y_test, pred2 )<predict_on_test>
y_pred=m.predict(X_valid )
predictions = reg2.predict(test, num_iteration=reg2.best_iteration_ )<prepare_output>
accuracy_score(y_valid, y_pred )
sample_submission['winPlacePerc'] = predictions<save_to_csv>
to_test = TabularPandas(df_test, procs, cat, cont )
sample_submission.to_csv('submission.csv',index=False )<import_modules>
predicted_result = m.predict(to_test.xs.drop('Fare_na', axis=1))
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from catboost import CatBoostRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import StandardScaler,MinMaxScaler<load_from_csv>
output= pd.DataFrame({'PassengerId':df_test.PassengerId, 'Survived': predicted_result.astype(int)})
 output.to_csv('submission_titanic.csv', index=False)
 output.head()