Kaggle competition: Titanic - Machine Learning from Disaster
pd.set_option('display.max_columns', None)
 pd.set_option('display.max_rows', None )<set_options>
%matplotlib inline
plt.style.use("ggplot")
 warnings.simplefilter("ignore" )
warnings.filterwarnings('ignore' )<count_missing_values>
df_train = pd.read_csv('/kaggle/input/titanic/train.csv')
 df_train.head()
d_train.isnull().sum()<count_missing_values>
df_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 df_test.head()
d_test.isnull().sum()<sort_values>
df_train['Age'].isna().sum()
d_train.corr() ['SalePrice'].sort_values(ascending=False )<drop_column>
df_train['Age'] = df_train['Age'].fillna(0)
 df_train['Age'].isna().sum()
d_train = d_train.drop(d_train[(d_train['SalePrice']>740000)&(d_train['SalePrice']<756000)].index ).reset_index(drop=True )<drop_column>
df_train.drop(columns = ['Name','Ticket' ,'Fare' , 'Cabin'] , inplace= True )
d_train = d_train.drop(d_train[(d_train['1stFlrSF']>4690)&(d_train['1stFlrSF']<4700)].index ).reset_index(drop=True )<drop_column>
cat_vars=['Sex' , 'Embarked']
for var in cat_vars:
cat_list='var'+'_'+var
cat_list = pd.get_dummies(df_train[var], prefix=var)
 df_train1=df_train.join(cat_list)
 df_train=df_train1
d_train = d_train.drop(d_train[(d_train['GrLivArea']>4000)&(d_train['SalePrice']<250000)].index ).reset_index(drop=True )<drop_column>
df_train.drop(columns = ['Sex','Embarked'] , inplace= True)
 df_train.head(5 )
d_train = d_train.drop(d_train[(d_train['GarageCars']>3)&(d_train['SalePrice']<290000)].index ).reset_index(drop=True )<drop_column>
s=0
d=0
for i in df_train['Survived']:
if i==0:
d+=1
else:
s+=1
print('Dead - ',d,' Survived -',s )
d_train = d_train.drop(d_train[(d_train['GarageArea']>1240)&(d_train['GarageArea']<1400)].index ).reset_index(drop=True )<compute_train_metric>
X = df_train.loc[:, df_train.columns != 'Survived']
Y = df_train.Survived
def Series_stats(var, category, prop1, prop2):
s1 = d_train[(d_train[category]==prop1)][var]
s2 = d_train[(d_train[category]==prop2)][var]
t, p = ttest_ind(s1,s2,equal_var = False)
 print("Two-sample t-test: t={}, p={}".format(round(t,5),p))
if(( p < 0.05)and(np.abs(t)> 1.96)) :
print("
REJECT the Null Hypothesis and state that:
at 5% significance level, the mean {} of {}-{} and {}-{} are not equal.".format(var, prop1, category, prop2, category))
print("
YES, the {} of {}-{} differ significantly from {}-{} in the current dataset.".format(var, prop1, category, prop2, category))
print("
The mean value of {} for {}-{} is {} and for {}-{} is {}".format(var, prop1, category, round(s1.mean() ,2), prop2, category, round(s2.mean() ,2)))
 else:
print("
FAIL to Reject the Null Hypothesis and state that:
at 5% significance level, the mean {} of {} - {} and {} - {} are equal.".format(var, prop1, category, prop2, category))
print("
NO, the {} of {}-{} NOT differ significantly from {}-{} in the current dataset".format(var, prop1, category, prop2, category))
print("
The mean value of {} for {}-{} is {} and for {}-{} is {}".format(var, prop1, category, round(s1.mean() ,2), prop2, category, round(s2.mean() ,2)) )<data_type_conversions>
df_test['Age'] = df_test['Age'].fillna(0)
 df_test.info()
d_train['Exterior1st'].fillna("VinylSd", inplace=True)
 d_train['Exterior2nd'].fillna("VinylSd", inplace=True)
 d_train['KitchenQual'].fillna("TA", inplace=True)
 d_train['Functional'].fillna("Typ", inplace=True)
 d_train['SaleType'].fillna("WD", inplace=True)
 d_test['MSZoning'].fillna('RL', inplace=True)
 d_test['Utilities'].fillna('NoSeWa', inplace=True)
 d_test['Exterior1st'].fillna("VinylSd", inplace=True)
 d_test['Exterior2nd'].fillna("VinylSd", inplace=True)
 d_test['KitchenQual'].fillna("TA", inplace=True)
 d_test['Functional'].fillna("Typ", inplace=True)
 d_test['SaleType'].fillna("WD", inplace=True)
 d_test['Electrical'].fillna("SBrkr")
 d_train = d_train.fillna("None")
 d_test = d_test.fillna("None" )<count_missing_values>
cat_vars=['Sex' , 'Embarked']
for var in cat_vars:
cat_list='var'+'_'+var
cat_list = pd.get_dummies(df_test[var], prefix=var)
 df_test1=df_test.join(cat_list)
 df_test=df_test1
d_train.isnull().sum()<count_missing_values>
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import export_graphviz
from IPython.display import SVG
from graphviz import Source
from IPython.display import display
from ipywidgets import interactive, IntSlider, FloatSlider, interact
import ipywidgets
from IPython.display import Image
from subprocess import call
import matplotlib.image as mpimg
<drop_column><EOS> 
estimator = plot_tree_rf(crit='gini', bootstrap='False' , depth =16 , forests=100 , min_split=3 , min_leaf= 3)
 y_pred_rf = estimator.predict(df_test)
 print('len',len(y_pred_rf))
sub = pd.DataFrame(columns=['PassengerId' , 'Survived'])
 sub['PassengerId'] = df_test['PassengerId'].astype(int)
 sub['Survived'] = y_pred_rf.astype(int)
 sub.to_csv('sub_rf.csv', index=False )