{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport os \nfor dirname,_,filenames in os.walk('/kaggle/input'): \n    for filename in filenames: \n        print(os.path.join(dirname,filename)) ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:36.412180Z","iopub.execute_input":"2022-07-16T06:36:36.414560Z","iopub.status.idle":"2022-07-16T06:36:36.448236Z","shell.execute_reply.started":"2022-07-16T06:36:36.412695Z","shell.execute_reply":"2022-07-16T06:36:36.447134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns \nimport io \nimport requests  \nimport re \nimport os \nimport warnings \nprint(os.listdir(\"../input\")) \nimport plotly.express as px \nimport plotly.graph_objects as go \nimport plotly.io as pio \npio.templates   \nimport matplotlib.pyplot as plt \n%matplotlib inline \nplt.style.use('seaborn-notebook') \nfrom matplotlib.ticker import StrMethodFormatter \nfrom sklearn.preprocessing import StandardScaler,MinMaxScaler,LabelBinarizer ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:37.001921Z","iopub.execute_input":"2022-07-16T06:36:37.002713Z","iopub.status.idle":"2022-07-16T06:36:39.059652Z","shell.execute_reply.started":"2022-07-16T06:36:37.002662Z","shell.execute_reply":"2022-07-16T06:36:39.058701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_csv(\"/kaggle/input/titanic/train.csv\")     \ntrain_data.head() ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.061238Z","iopub.execute_input":"2022-07-16T06:36:39.061537Z","iopub.status.idle":"2022-07-16T06:36:39.103618Z","shell.execute_reply.started":"2022-07-16T06:36:39.061506Z","shell.execute_reply":"2022-07-16T06:36:39.102515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=pd.read_csv(\"/kaggle/input/titanic/test.csv\")   \ntest_data.head() ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.104963Z","iopub.execute_input":"2022-07-16T06:36:39.105462Z","iopub.status.idle":"2022-07-16T06:36:39.147658Z","shell.execute_reply.started":"2022-07-16T06:36:39.105430Z","shell.execute_reply":"2022-07-16T06:36:39.146755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.columns ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.150119Z","iopub.execute_input":"2022-07-16T06:36:39.150883Z","iopub.status.idle":"2022-07-16T06:36:39.160040Z","shell.execute_reply.started":"2022-07-16T06:36:39.150833Z","shell.execute_reply":"2022-07-16T06:36:39.158894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gender_data=pd.read_csv(\"/kaggle/input/titanic/gender_submission.csv\") \ngender_data.head()    \ngender_data.columns ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.161812Z","iopub.execute_input":"2022-07-16T06:36:39.162525Z","iopub.status.idle":"2022-07-16T06:36:39.190759Z","shell.execute_reply.started":"2022-07-16T06:36:39.162478Z","shell.execute_reply":"2022-07-16T06:36:39.189584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"women=train_data.loc[train_data['Sex']=='female']['Survived']         \nrate_women=sum(women)/len(women) \nprint(\"% of women who survived:\",rate_women) ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.262019Z","iopub.execute_input":"2022-07-16T06:36:39.262454Z","iopub.status.idle":"2022-07-16T06:36:39.276382Z","shell.execute_reply.started":"2022-07-16T06:36:39.262420Z","shell.execute_reply":"2022-07-16T06:36:39.275029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"men=train_data.loc[train_data['Sex']=='male']['Survived']                 \nrate_men=sum(men)/len(men)   \nprint(\"% of men who survived\",rate_men)","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.661786Z","iopub.execute_input":"2022-07-16T06:36:39.662744Z","iopub.status.idle":"2022-07-16T06:36:39.671068Z","shell.execute_reply.started":"2022-07-16T06:36:39.662705Z","shell.execute_reply":"2022-07-16T06:36:39.669975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=[train_data,test_data] \nfor dataset in data: \n    mean=train_data['Age'].mean() \n    std=test_data['Age'].std() \n    is_null=dataset['Age'].isnull().sum()           \n    rand_age=np.random.randint(mean-std,mean+std,size=is_null) \n    age_slice=dataset['Age'].copy()     \n    age_slice[np.isnan(dataset['Age'])]=rand_age \n    dataset['Age']=age_slice\n    dataset['Age']=dataset['Age'].astype('int')    ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:39.822233Z","iopub.execute_input":"2022-07-16T06:36:39.822617Z","iopub.status.idle":"2022-07-16T06:36:39.838840Z","shell.execute_reply.started":"2022-07-16T06:36:39.822586Z","shell.execute_reply":"2022-07-16T06:36:39.837822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axes=plt.subplots(1,2,figsize=(16,8))     \nwomen=train_data[train_data['Sex']=='female']       \nmen=train_data[train_data['Sex']=='male']       \nax=sns.distplot(women[women['Survived']==1].Age.dropna(),bins=18,label=\"Survived\",ax=axes[0],kde=False,\n                color='green') \nax=sns.distplot(women[women['Survived']==0].Age.dropna(),bins=40,label=\"Not Survived\",ax=axes[0],kde=False,\n               color='red')    \nax.legend() \nax.set_title('Female')     \nax=sns.distplot(men[men['Survived']==1].Age.dropna(),bins=18,label='Survived',ax=axes[1],kde=False,\n               color='green')    \nax=sns.distplot(women[women['Survived']==0].Age.dropna(),bins=40,label=\"Not Survived\",ax=axes[1],kde=False,\n               color='red')       \nax.legend()    \nax.set_title('Male') ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:40.096858Z","iopub.execute_input":"2022-07-16T06:36:40.097511Z","iopub.status.idle":"2022-07-16T06:36:40.907256Z","shell.execute_reply.started":"2022-07-16T06:36:40.097477Z","shell.execute_reply":"2022-07-16T06:36:40.906046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=px.scatter_3d(train_data,x='Name',y='Sex',z='Age',color='Age')  \nfig.show() ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:40.909182Z","iopub.execute_input":"2022-07-16T06:36:40.909731Z","iopub.status.idle":"2022-07-16T06:36:42.108260Z","shell.execute_reply.started":"2022-07-16T06:36:40.909697Z","shell.execute_reply":"2022-07-16T06:36:42.107148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=px.scatter(train_data,x='PassengerId',y='Age',color='Survived',log_x=True,size_max=20,template='plotly',title='which Age Survived')   \nfig.show() ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:42.109629Z","iopub.execute_input":"2022-07-16T06:36:42.109999Z","iopub.status.idle":"2022-07-16T06:36:42.215335Z","shell.execute_reply.started":"2022-07-16T06:36:42.109950Z","shell.execute_reply":"2022-07-16T06:36:42.213797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embarked_mode=train_data['Embarked'].mode() \nfor data in [train_data,test_data]: \n    data['Embarked']=data['Embarked'].fillna(embarked_mode) ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:42.218144Z","iopub.execute_input":"2022-07-16T06:36:42.219318Z","iopub.status.idle":"2022-07-16T06:36:42.229654Z","shell.execute_reply.started":"2022-07-16T06:36:42.219280Z","shell.execute_reply":"2022-07-16T06:36:42.228422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FacetGrid=sns.FacetGrid(train_data,row='Embarked',height=4.5,aspect=1.6) \nFacetGrid.map(sns.pointplot,'Pclass','Survived','Sex',order=None,hue_order=None) \nFacetGrid.add_legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:42.230821Z","iopub.execute_input":"2022-07-16T06:36:42.231320Z","iopub.status.idle":"2022-07-16T06:36:43.892163Z","shell.execute_reply.started":"2022-07-16T06:36:42.231284Z","shell.execute_reply":"2022-07-16T06:36:43.891284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=[train_data,test_data] \nfor dataset in data: \n    dataset['relatives']=dataset['SibSp']+dataset['Parch']    \n    dataset.loc[dataset['relatives']>0,'travelled_alone']='No' \n    dataset.loc[dataset['relatives']==0,'travelled_alone']='Yes'     \n    \naxes=sns.factorplot('relatives','Survived',data=train_data,aspect=2.5) ","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:36:43.893404Z","iopub.execute_input":"2022-07-16T06:36:43.893795Z","iopub.status.idle":"2022-07-16T06:36:44.614492Z","shell.execute_reply.started":"2022-07-16T06:36:43.893762Z","shell.execute_reply":"2022-07-16T06:36:44.612594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier \nY=train_data['Survived'] \nfeatures=['Pclass','Sex','SibSp','Parch']    \nX=pd.get_dummies(train_data[features])     \nX_test=pd.get_dummies(test_data[features]) \nmodel=RandomForestClassifier(n_estimators=100,max_depth=3,random_state=1) \nmodel.fit(X,Y) \npredictions=model.predict(X_test)  \noutput=pd.DataFrame({'PassengerId':test_data.PassengerId,'Survived':predictions})     \noutput.to_csv('submission.csv',index=False) \noutput.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-16T06:37:28.873133Z","iopub.execute_input":"2022-07-16T06:37:28.873553Z","iopub.status.idle":"2022-07-16T06:37:29.132161Z","shell.execute_reply.started":"2022-07-16T06:37:28.873520Z","shell.execute_reply":"2022-07-16T06:37:29.131109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}