Kaggle competition: Titanic - Machine Learning from Disaster
test_df = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
df1=pd.read_csv('/kaggle/input/titanic/train.csv')
 df2=pd.read_csv('/kaggle/input/titanic/test.csv')
 df3=pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
X = train.drop(["ID_code", "target"], axis=1)
 Y = train["target"]
X_test = test_df.drop(["ID_code"], axis=1 )<categorify>
print(df1.nunique())
 print(df2.nunique() )
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y 
<choose_model_class>
df1.drop(['PassengerId','Name','Ticket'],axis=1,inplace=True)
 df2.drop(['Name','Ticket'],axis=1,inplace=True )
n_fold = 15
folds = StratifiedKFold(n_splits=n_fold, shuffle=True, random_state=42 )<init_hyperparams>
print(df1.isnull().sum())
 print(( df1.isnull().sum() /len(df1)) *100 )
params = {'num_leaves': 13,
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'objective': 'binary',
'boost_from_average': False,
'max_depth': -1,
'learning_rate': 0.0083,
'boost': 'gbdt',
'bagging_freq': 5,
'tree_learner': "serial",
'bagging_fraction': 0.335,
'feature_fraction': 0.041,
'metric': 'auc',
'num_threads': 8}<prepare_x_and_y>
df1.drop(['Cabin'],axis=1,inplace=True)
 df2.drop(['Cabin'],axis=1,inplace=True )
prediction = np.zeros(len(X_test))
oof = np.zeros(len(X))
for fold_n,(train_index, valid_index)in enumerate(folds.split(X,Y)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = Y.iloc[train_index], Y.iloc[valid_index]
X_tr, y_tr = augment(X_train.values, y_train.values)
 X_tr = pd.DataFrame(X_tr)
 train_data = lgb.Dataset(X_tr, label=y_tr)
 valid_data = lgb.Dataset(X_valid, label=y_valid)
 model = lgb.train(params,train_data,num_boost_round=1000000,
valid_sets = [train_data, valid_data],verbose_eval=3000,early_stopping_rounds = 4000)
 oof[valid_index] = model.predict(X.iloc[valid_index], num_iteration=model.best_iteration)
 prediction += model.predict(X_test, num_iteration=model.best_iteration)/15
print("CV score: {:<8.5f}".format(roc_auc_score(Y, oof)) )<save_to_csv>
df1.isnull().sum()
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = prediction
sub.to_csv("submission.csv", index=False )<load_from_csv>
df1['Age']=df1['Age'].fillna(method='ffill')
 df1['Embarked']=df1['Embarked'].fillna(method='ffill')
 df2['Age']=df1['Age'].fillna(method='ffill')
 df2['Embarked']=df1['Embarked'].fillna(method='ffill' )
df_train=pd.read_csv('.. /input/train.csv' )<load_from_csv>
df2['Fare']=df1['Fare'].fillna(method='ffill' )
df_test=pd.read_csv('.. /input/test.csv' )<data_type_conversions>
print(df1.isnull().sum())
 print(df2.isnull().sum() )
def memory_usage(df):
numerics = ['int8', 'int16', 'int32', 'int64', 'float16', 'float32', 'float64']
for col in df.columns:
if str(df[col].dtype)in numerics:
if str(df[col].dtype)[:3] == 'int':
if(( df[col].min() > np.iinfo(np.int8 ).min)and(df[col].max() < np.iinfo(np.int8 ).max)) :
df[col] = df[col].astype('int8')
 elif(( df[col].min() > np.iinfo(np.int16 ).min)and(df[col].max() < np.iinfo(np.int16 ).max)) :
df[col] = df[col].astype('int16')
 elif(( df[col].min() > np.iinfo(np.int32 ).min)and(df[col].max() < np.iinfo(np.int32 ).max)) :
df[col] = df[col].astype('int32')
 else:
df[col] = df[col].astype('int64')
 else:
if(( df[col].min() > np.finfo(np.float16 ).min)and df[col].max() < np.finfo(np.float16 ).max):
df[col] = df[col].astype('float16')
 elif(( df[col].min() > np.finfo(np.float32 ).min)and df[col].max() < np.finfo(np.float32 ).max):
df[col] = df[col].astype('float32')
 elif(( df[col].min() > np.finfo(np.float64 ).min)and df[col].max() < np.finfo(np.float64 ).max):
df[col] = df[col].astype('float64')
 else:
df[col] = df[col].astype('float128')
 return df<prepare_x_and_y>
df11=pd.get_dummies(data=df1,columns=['Sex','Embarked'],drop_first=True)
 df12=pd.get_dummies(data=df2,columns=['Sex','Embarked'],drop_first=True )
X_train=df_train.iloc[:,2:]
Y_train=df_train.iloc[:,1]
ID_train=df_train.iloc[:,0]
X_test=df_test.iloc[:,1:]
ID_test=df_test.iloc[:,0]<count_missing_values>
from sklearn.linear_model import LogisticRegression
df_null_train=X_train.isna().sum().reset_index()
df_null_train.columns=['Column_name','Null_Count']
df_null_train=df_null_train[df_null_train['Null_Count']>0]<count_missing_values>
y=df11['Survived']
X=df11.drop(['Survived'],axis=1)
df_null_test=X_test.isna().sum().reset_index()
df_null_test.columns=['Column_name','Null_Count']
df_null_test=df_null_test[df_null_test['Null_Count']>0]<count_unique_values>
from sklearn.ensemble import RandomForestClassifier
df_Unique_train=X_train.nunique().reset_index()
df_Unique_train.columns=['Column_name','Unique_Count']
df_Unique_train=df_Unique_train[df_Unique_train['Unique_Count']==1]<count_unique_values>
model=RF=RandomForestClassifier(max_depth=5,criterion='entropy',n_estimators=83)
 model.fit(X,y )
df_Unique_test=X_test.nunique().reset_index()
df_Unique_test.columns=['Column_name','Unique_Count']
df_Unique_test=df_Unique_test[df_Unique_test['Unique_Count']==1]<feature_engineering>
predictions=model.predict(X_test)
 output = pd.DataFrame({'PassengerId': df12.PassengerId, 'Survived': predictions})
 output.to_csv('first_submission.csv', index=False)
 print("First model submission done" )