{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-26T20:45:37.418455Z","iopub.execute_input":"2022-07-26T20:45:37.419602Z","iopub.status.idle":"2022-07-26T20:45:37.436010Z","shell.execute_reply.started":"2022-07-26T20:45:37.419501Z","shell.execute_reply":"2022-07-26T20:45:37.434845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.442724Z","iopub.execute_input":"2022-07-26T20:45:37.443432Z","iopub.status.idle":"2022-07-26T20:45:37.450175Z","shell.execute_reply.started":"2022-07-26T20:45:37.443389Z","shell.execute_reply":"2022-07-26T20:45:37.449266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset=pd.read_csv('/kaggle/input/titanic/train.csv')\ndataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.451735Z","iopub.execute_input":"2022-07-26T20:45:37.452809Z","iopub.status.idle":"2022-07-26T20:45:37.483676Z","shell.execute_reply.started":"2022-07-26T20:45:37.452767Z","shell.execute_reply":"2022-07-26T20:45:37.482526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.487385Z","iopub.execute_input":"2022-07-26T20:45:37.488000Z","iopub.status.idle":"2022-07-26T20:45:37.494269Z","shell.execute_reply.started":"2022-07-26T20:45:37.487968Z","shell.execute_reply":"2022-07-26T20:45:37.493428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv ('/kaggle/input/titanic/test.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.495359Z","iopub.execute_input":"2022-07-26T20:45:37.495816Z","iopub.status.idle":"2022-07-26T20:45:37.521768Z","shell.execute_reply.started":"2022-07-26T20:45:37.495788Z","shell.execute_reply":"2022-07-26T20:45:37.520684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.523237Z","iopub.execute_input":"2022-07-26T20:45:37.523525Z","iopub.status.idle":"2022-07-26T20:45:37.556391Z","shell.execute_reply.started":"2022-07-26T20:45:37.523499Z","shell.execute_reply":"2022-07-26T20:45:37.555439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset. size","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.557848Z","iopub.execute_input":"2022-07-26T20:45:37.558155Z","iopub.status.idle":"2022-07-26T20:45:37.564274Z","shell.execute_reply.started":"2022-07-26T20:45:37.558129Z","shell.execute_reply":"2022-07-26T20:45:37.563251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.ndim","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.565677Z","iopub.execute_input":"2022-07-26T20:45:37.565965Z","iopub.status.idle":"2022-07-26T20:45:37.576289Z","shell.execute_reply.started":"2022-07-26T20:45:37.565939Z","shell.execute_reply":"2022-07-26T20:45:37.575264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset. shape","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.577314Z","iopub.execute_input":"2022-07-26T20:45:37.578045Z","iopub.status.idle":"2022-07-26T20:45:37.586740Z","shell.execute_reply.started":"2022-07-26T20:45:37.578013Z","shell.execute_reply":"2022-07-26T20:45:37.585800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['PassengerId'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.587858Z","iopub.execute_input":"2022-07-26T20:45:37.588282Z","iopub.status.idle":"2022-07-26T20:45:37.600188Z","shell.execute_reply.started":"2022-07-26T20:45:37.588241Z","shell.execute_reply":"2022-07-26T20:45:37.599234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Analysis","metadata":{}},{"cell_type":"code","source":"columns = dataset.columns\ncolumns","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.601330Z","iopub.execute_input":"2022-07-26T20:45:37.601625Z","iopub.status.idle":"2022-07-26T20:45:37.611950Z","shell.execute_reply.started":"2022-07-26T20:45:37.601600Z","shell.execute_reply":"2022-07-26T20:45:37.611037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Age'].hist(bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.613395Z","iopub.execute_input":"2022-07-26T20:45:37.613900Z","iopub.status.idle":"2022-07-26T20:45:37.863115Z","shell.execute_reply.started":"2022-07-26T20:45:37.613858Z","shell.execute_reply":"2022-07-26T20:45:37.861893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Sex'].hist(bins=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:37.868136Z","iopub.execute_input":"2022-07-26T20:45:37.868477Z","iopub.status.idle":"2022-07-26T20:45:38.058734Z","shell.execute_reply.started":"2022-07-26T20:45:37.868449Z","shell.execute_reply":"2022-07-26T20:45:38.057652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Sex'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:38.060012Z","iopub.execute_input":"2022-07-26T20:45:38.060330Z","iopub.status.idle":"2022-07-26T20:45:38.070820Z","shell.execute_reply.started":"2022-07-26T20:45:38.060303Z","shell.execute_reply":"2022-07-26T20:45:38.069614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Fare'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:38.072118Z","iopub.execute_input":"2022-07-26T20:45:38.073166Z","iopub.status.idle":"2022-07-26T20:45:38.275316Z","shell.execute_reply.started":"2022-07-26T20:45:38.073132Z","shell.execute_reply":"2022-07-26T20:45:38.274547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Survived'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:38.276318Z","iopub.execute_input":"2022-07-26T20:45:38.277188Z","iopub.status.idle":"2022-07-26T20:45:38.490825Z","shell.execute_reply.started":"2022-07-26T20:45:38.277157Z","shell.execute_reply":"2022-07-26T20:45:38.489648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Survived'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:38.492460Z","iopub.execute_input":"2022-07-26T20:45:38.492975Z","iopub.status.idle":"2022-07-26T20:45:38.503996Z","shell.execute_reply.started":"2022-07-26T20:45:38.492928Z","shell.execute_reply":"2022-07-26T20:45:38.502869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Pclass'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:38.505585Z","iopub.execute_input":"2022-07-26T20:45:38.505940Z","iopub.status.idle":"2022-07-26T20:45:38.834427Z","shell.execute_reply.started":"2022-07-26T20:45:38.505905Z","shell.execute_reply":"2022-07-26T20:45:38.833167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Embarked'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:38.836242Z","iopub.execute_input":"2022-07-26T20:45:38.837420Z","iopub.status.idle":"2022-07-26T20:45:39.029786Z","shell.execute_reply.started":"2022-07-26T20:45:38.837365Z","shell.execute_reply":"2022-07-26T20:45:39.028674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Parch'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:39.031341Z","iopub.execute_input":"2022-07-26T20:45:39.031719Z","iopub.status.idle":"2022-07-26T20:45:39.211071Z","shell.execute_reply.started":"2022-07-26T20:45:39.031687Z","shell.execute_reply":"2022-07-26T20:45:39.210180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['SibSp'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:39.212390Z","iopub.execute_input":"2022-07-26T20:45:39.213389Z","iopub.status.idle":"2022-07-26T20:45:39.396214Z","shell.execute_reply.started":"2022-07-26T20:45:39.213344Z","shell.execute_reply":"2022-07-26T20:45:39.394778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dataset['Survived' and 'Sex'], 20, facecolor='b') \nplt.xlabel('Sex')\nplt.ylabel('Survived')\nplt.title('Histogram for Survived based on Sex') \nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:39.399398Z","iopub.execute_input":"2022-07-26T20:45:39.400041Z","iopub.status.idle":"2022-07-26T20:45:39.549841Z","shell.execute_reply.started":"2022-07-26T20:45:39.399987Z","shell.execute_reply":"2022-07-26T20:45:39.548996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dataset['Survived' and 'Age'], 20, facecolor='b') \nplt.xlabel('Age')\nplt.ylabel('Survived')\nplt.title('Histogram for Survived based on Age') \nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:39.551328Z","iopub.execute_input":"2022-07-26T20:45:39.552151Z","iopub.status.idle":"2022-07-26T20:45:39.788148Z","shell.execute_reply.started":"2022-07-26T20:45:39.552065Z","shell.execute_reply":"2022-07-26T20:45:39.786981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Age'].hist(bins=20) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:39.789774Z","iopub.execute_input":"2022-07-26T20:45:39.790204Z","iopub.status.idle":"2022-07-26T20:45:40.009052Z","shell.execute_reply.started":"2022-07-26T20:45:39.790163Z","shell.execute_reply":"2022-07-26T20:45:40.007873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.subplot(132)\ndataset['Sex'].value_counts(normalize=True).plot.bar(figsize=(20,10),title='Sex')\nplt.subplot(133)\ndataset['Survived'].value_counts(normalize=True).plot.bar(figsize=(20,10),title='Survived')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:40.010813Z","iopub.execute_input":"2022-07-26T20:45:40.011276Z","iopub.status.idle":"2022-07-26T20:45:40.290670Z","shell.execute_reply.started":"2022-07-26T20:45:40.011232Z","shell.execute_reply":"2022-07-26T20:45:40.289510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Survived=pd.crosstab(dataset['Survived'], dataset['Sex'])\nSurvived.div(Survived.sum(1).astype(float), axis=0).plot(kind='bar', stacked=True,figsize=(4,4))","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:40.291874Z","iopub.execute_input":"2022-07-26T20:45:40.292228Z","iopub.status.idle":"2022-07-26T20:45:40.491152Z","shell.execute_reply.started":"2022-07-26T20:45:40.292198Z","shell.execute_reply":"2022-07-26T20:45:40.490268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distribution of Data\ndataset.hist(bins=20, figsize=(9,7), grid=False);","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:40.492410Z","iopub.execute_input":"2022-07-26T20:45:40.492706Z","iopub.status.idle":"2022-07-26T20:45:41.538183Z","shell.execute_reply.started":"2022-07-26T20:45:40.492680Z","shell.execute_reply":"2022-07-26T20:45:41.536962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.539663Z","iopub.execute_input":"2022-07-26T20:45:41.540011Z","iopub.status.idle":"2022-07-26T20:45:41.559367Z","shell.execute_reply.started":"2022-07-26T20:45:41.539982Z","shell.execute_reply":"2022-07-26T20:45:41.558207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.apply(lambda x: sum(x.isnull()), axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.560811Z","iopub.execute_input":"2022-07-26T20:45:41.561149Z","iopub.status.idle":"2022-07-26T20:45:41.574380Z","shell.execute_reply.started":"2022-07-26T20:45:41.561114Z","shell.execute_reply":"2022-07-26T20:45:41.573162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.576084Z","iopub.execute_input":"2022-07-26T20:45:41.576399Z","iopub.status.idle":"2022-07-26T20:45:41.596247Z","shell.execute_reply.started":"2022-07-26T20:45:41.576372Z","shell.execute_reply":"2022-07-26T20:45:41.595242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" dataset['Age'].fillna(dataset['Age'].mean(), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.597478Z","iopub.execute_input":"2022-07-26T20:45:41.597838Z","iopub.status.idle":"2022-07-26T20:45:41.607724Z","shell.execute_reply.started":"2022-07-26T20:45:41.597810Z","shell.execute_reply":"2022-07-26T20:45:41.606613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.609022Z","iopub.execute_input":"2022-07-26T20:45:41.609399Z","iopub.status.idle":"2022-07-26T20:45:41.632707Z","shell.execute_reply.started":"2022-07-26T20:45:41.609371Z","shell.execute_reply":"2022-07-26T20:45:41.631292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.apply(lambda x: sum(x.isnull()), axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.634655Z","iopub.execute_input":"2022-07-26T20:45:41.634976Z","iopub.status.idle":"2022-07-26T20:45:41.646206Z","shell.execute_reply.started":"2022-07-26T20:45:41.634949Z","shell.execute_reply":"2022-07-26T20:45:41.645147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.647170Z","iopub.execute_input":"2022-07-26T20:45:41.647470Z","iopub.status.idle":"2022-07-26T20:45:41.659846Z","shell.execute_reply.started":"2022-07-26T20:45:41.647444Z","shell.execute_reply":"2022-07-26T20:45:41.658726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.apply(lambda x: sum(x.isnull()), axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.661030Z","iopub.execute_input":"2022-07-26T20:45:41.661387Z","iopub.status.idle":"2022-07-26T20:45:41.676137Z","shell.execute_reply.started":"2022-07-26T20:45:41.661357Z","shell.execute_reply":"2022-07-26T20:45:41.675145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:41.677558Z","iopub.execute_input":"2022-07-26T20:45:41.678060Z","iopub.status.idle":"2022-07-26T20:45:42.040332Z","shell.execute_reply.started":"2022-07-26T20:45:41.678028Z","shell.execute_reply":"2022-07-26T20:45:42.039103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = list(dataset)\nprint(columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.042027Z","iopub.execute_input":"2022-07-26T20:45:42.042914Z","iopub.status.idle":"2022-07-26T20:45:42.049387Z","shell.execute_reply.started":"2022-07-26T20:45:42.042855Z","shell.execute_reply":"2022-07-26T20:45:42.048245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" #columns = list(data.select_dtypes(exclude=['float64','int64']))","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.050723Z","iopub.execute_input":"2022-07-26T20:45:42.051054Z","iopub.status.idle":"2022-07-26T20:45:42.067069Z","shell.execute_reply.started":"2022-07-26T20:45:42.051024Z","shell.execute_reply":"2022-07-26T20:45:42.066137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_columns = ['Name', 'Sex', 'Ticket',  'Cabin', 'Embarked']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.068320Z","iopub.execute_input":"2022-07-26T20:45:42.069281Z","iopub.status.idle":"2022-07-26T20:45:42.085541Z","shell.execute_reply.started":"2022-07-26T20:45:42.069245Z","shell.execute_reply":"2022-07-26T20:45:42.084463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.095783Z","iopub.execute_input":"2022-07-26T20:45:42.096738Z","iopub.status.idle":"2022-07-26T20:45:42.104237Z","shell.execute_reply.started":"2022-07-26T20:45:42.096699Z","shell.execute_reply":"2022-07-26T20:45:42.103321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset['Name'].fillna(dataset['Name'].mode()[0], inplace = True)\n#0:gets the mode of each column, 1: for each row\ndataset['Sex'].fillna(dataset['Sex'].mode()[0], inplace = True) \ndataset['Cabin'].fillna(dataset['Cabin'].mode()[0], inplace = True)\ndataset['Age'].fillna(dataset['Age'].mode()[0], inplace = True) \ndataset['Fare'].fillna(dataset['Fare'].mode()[0], inplace = True)\ndataset['Pclass'].fillna(dataset['Pclass'].mode()[0], inplace = True)\ndataset['Embarked'].fillna(dataset['Embarked'].mode()[0], inplace = True)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.105466Z","iopub.execute_input":"2022-07-26T20:45:42.106549Z","iopub.status.idle":"2022-07-26T20:45:42.122115Z","shell.execute_reply.started":"2022-07-26T20:45:42.106514Z","shell.execute_reply":"2022-07-26T20:45:42.121164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.apply(lambda x: sum(x.isnull()), axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.123281Z","iopub.execute_input":"2022-07-26T20:45:42.124363Z","iopub.status.idle":"2022-07-26T20:45:42.135934Z","shell.execute_reply.started":"2022-07-26T20:45:42.124329Z","shell.execute_reply":"2022-07-26T20:45:42.135150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.137368Z","iopub.execute_input":"2022-07-26T20:45:42.137732Z","iopub.status.idle":"2022-07-26T20:45:42.148935Z","shell.execute_reply.started":"2022-07-26T20:45:42.137699Z","shell.execute_reply":"2022-07-26T20:45:42.147992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder() \nfor i in c_columns:\n    #print(i)\n    dataset[i] = le.fit_transform(dataset[i])\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.150497Z","iopub.execute_input":"2022-07-26T20:45:42.151208Z","iopub.status.idle":"2022-07-26T20:45:42.168659Z","shell.execute_reply.started":"2022-07-26T20:45:42.151162Z","shell.execute_reply":"2022-07-26T20:45:42.167363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head(25)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.170508Z","iopub.execute_input":"2022-07-26T20:45:42.171341Z","iopub.status.idle":"2022-07-26T20:45:42.194887Z","shell.execute_reply.started":"2022-07-26T20:45:42.171298Z","shell.execute_reply":"2022-07-26T20:45:42.193549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Normalisation","metadata":{}},{"cell_type":"code","source":"#from sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import normalize\n \n ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.196550Z","iopub.execute_input":"2022-07-26T20:45:42.196992Z","iopub.status.idle":"2022-07-26T20:45:42.202218Z","shell.execute_reply.started":"2022-07-26T20:45:42.196950Z","shell.execute_reply":"2022-07-26T20:45:42.200933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_data = dataset.copy()\noriginal_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.203633Z","iopub.execute_input":"2022-07-26T20:45:42.204075Z","iopub.status.idle":"2022-07-26T20:45:42.229068Z","shell.execute_reply.started":"2022-07-26T20:45:42.204031Z","shell.execute_reply":"2022-07-26T20:45:42.227860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_data[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.230302Z","iopub.execute_input":"2022-07-26T20:45:42.230705Z","iopub.status.idle":"2022-07-26T20:45:42.245578Z","shell.execute_reply.started":"2022-07-26T20:45:42.230678Z","shell.execute_reply":"2022-07-26T20:45:42.244420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.247337Z","iopub.execute_input":"2022-07-26T20:45:42.248032Z","iopub.status.idle":"2022-07-26T20:45:42.268114Z","shell.execute_reply.started":"2022-07-26T20:45:42.247990Z","shell.execute_reply":"2022-07-26T20:45:42.267377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.269336Z","iopub.execute_input":"2022-07-26T20:45:42.269832Z","iopub.status.idle":"2022-07-26T20:45:42.283675Z","shell.execute_reply.started":"2022-07-26T20:45:42.269802Z","shell.execute_reply":"2022-07-26T20:45:42.282839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_for_norm = dataset.drop(['Sex', 'PassengerId'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.284981Z","iopub.execute_input":"2022-07-26T20:45:42.285790Z","iopub.status.idle":"2022-07-26T20:45:42.295830Z","shell.execute_reply.started":"2022-07-26T20:45:42.285753Z","shell.execute_reply":"2022-07-26T20:45:42.295025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data = normalize(data_for_norm)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.297398Z","iopub.execute_input":"2022-07-26T20:45:42.298074Z","iopub.status.idle":"2022-07-26T20:45:42.308697Z","shell.execute_reply.started":"2022-07-26T20:45:42.298029Z","shell.execute_reply":"2022-07-26T20:45:42.307626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(normalized_data[0:5])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.310346Z","iopub.execute_input":"2022-07-26T20:45:42.311047Z","iopub.status.idle":"2022-07-26T20:45:42.320687Z","shell.execute_reply.started":"2022-07-26T20:45:42.311002Z","shell.execute_reply":"2022-07-26T20:45:42.319651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.322196Z","iopub.execute_input":"2022-07-26T20:45:42.323183Z","iopub.status.idle":"2022-07-26T20:45:42.333941Z","shell.execute_reply.started":"2022-07-26T20:45:42.323139Z","shell.execute_reply":"2022-07-26T20:45:42.332927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data = pd.DataFrame(normalized_data, columns=data_for_norm.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.335629Z","iopub.execute_input":"2022-07-26T20:45:42.336363Z","iopub.status.idle":"2022-07-26T20:45:42.345244Z","shell.execute_reply.started":"2022-07-26T20:45:42.336319Z","shell.execute_reply":"2022-07-26T20:45:42.343865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.346875Z","iopub.execute_input":"2022-07-26T20:45:42.347699Z","iopub.status.idle":"2022-07-26T20:45:42.369661Z","shell.execute_reply.started":"2022-07-26T20:45:42.347654Z","shell.execute_reply":"2022-07-26T20:45:42.368643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data['Survived'] = dataset['Survived']\n ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.371028Z","iopub.execute_input":"2022-07-26T20:45:42.371849Z","iopub.status.idle":"2022-07-26T20:45:42.377578Z","shell.execute_reply.started":"2022-07-26T20:45:42.371815Z","shell.execute_reply":"2022-07-26T20:45:42.376368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.378845Z","iopub.execute_input":"2022-07-26T20:45:42.379703Z","iopub.status.idle":"2022-07-26T20:45:42.401219Z","shell.execute_reply.started":"2022-07-26T20:45:42.379668Z","shell.execute_reply":"2022-07-26T20:45:42.400328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" normalized_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.402662Z","iopub.execute_input":"2022-07-26T20:45:42.403288Z","iopub.status.idle":"2022-07-26T20:45:42.444764Z","shell.execute_reply.started":"2022-07-26T20:45:42.403255Z","shell.execute_reply":"2022-07-26T20:45:42.443824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Building a Decision Tree classifier using sklearn","metadata":{}},{"cell_type":"code","source":"\n\nfrom sklearn.model_selection import train_test_split \nfrom sklearn import tree\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn import metrics\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import classification_report \nfrom sklearn.tree import export_graphviz\nfrom sklearn.metrics import ConfusionMatrixDisplay \nimport graphviz","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.446021Z","iopub.execute_input":"2022-07-26T20:45:42.446392Z","iopub.status.idle":"2022-07-26T20:45:42.502706Z","shell.execute_reply.started":"2022-07-26T20:45:42.446363Z","shell.execute_reply":"2022-07-26T20:45:42.501497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = list(normalized_data.columns)\ncolumns","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.504384Z","iopub.execute_input":"2022-07-26T20:45:42.505014Z","iopub.status.idle":"2022-07-26T20:45:42.511585Z","shell.execute_reply.started":"2022-07-26T20:45:42.504980Z","shell.execute_reply":"2022-07-26T20:45:42.510652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.513346Z","iopub.execute_input":"2022-07-26T20:45:42.513986Z","iopub.status.idle":"2022-07-26T20:45:42.534865Z","shell.execute_reply.started":"2022-07-26T20:45:42.513951Z","shell.execute_reply":"2022-07-26T20:45:42.534076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data['PassengerId'] = dataset['PassengerId']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.535872Z","iopub.execute_input":"2022-07-26T20:45:42.536437Z","iopub.status.idle":"2022-07-26T20:45:42.542295Z","shell.execute_reply.started":"2022-07-26T20:45:42.536405Z","shell.execute_reply":"2022-07-26T20:45:42.541261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.543453Z","iopub.execute_input":"2022-07-26T20:45:42.543742Z","iopub.status.idle":"2022-07-26T20:45:42.567476Z","shell.execute_reply.started":"2022-07-26T20:45:42.543715Z","shell.execute_reply":"2022-07-26T20:45:42.566646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data['Sex'] = dataset['Sex']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.568621Z","iopub.execute_input":"2022-07-26T20:45:42.569117Z","iopub.status.idle":"2022-07-26T20:45:42.577267Z","shell.execute_reply.started":"2022-07-26T20:45:42.569064Z","shell.execute_reply":"2022-07-26T20:45:42.576466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.578625Z","iopub.execute_input":"2022-07-26T20:45:42.578928Z","iopub.status.idle":"2022-07-26T20:45:42.603271Z","shell.execute_reply.started":"2022-07-26T20:45:42.578900Z","shell.execute_reply":"2022-07-26T20:45:42.602158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.604850Z","iopub.execute_input":"2022-07-26T20:45:42.605554Z","iopub.status.idle":"2022-07-26T20:45:42.654813Z","shell.execute_reply.started":"2022-07-26T20:45:42.605512Z","shell.execute_reply":"2022-07-26T20:45:42.653248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = normalized_data.drop(['Survived'], axis = 1)\n    \nclasses = pd.DataFrame(normalized_data['Survived'])","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.656352Z","iopub.execute_input":"2022-07-26T20:45:42.656667Z","iopub.status.idle":"2022-07-26T20:45:42.663680Z","shell.execute_reply.started":"2022-07-26T20:45:42.656638Z","shell.execute_reply":"2022-07-26T20:45:42.662917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Features:')\nprint(features.head())\n\nprint('Classes:')\nprint(classes.head())","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.664664Z","iopub.execute_input":"2022-07-26T20:45:42.665306Z","iopub.status.idle":"2022-07-26T20:45:42.680376Z","shell.execute_reply.started":"2022-07-26T20:45:42.665272Z","shell.execute_reply":"2022-07-26T20:45:42.679168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.682329Z","iopub.execute_input":"2022-07-26T20:45:42.682786Z","iopub.status.idle":"2022-07-26T20:45:42.703201Z","shell.execute_reply.started":"2022-07-26T20:45:42.682744Z","shell.execute_reply":"2022-07-26T20:45:42.701946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.706748Z","iopub.execute_input":"2022-07-26T20:45:42.707076Z","iopub.status.idle":"2022-07-26T20:45:42.713837Z","shell.execute_reply.started":"2022-07-26T20:45:42.707046Z","shell.execute_reply":"2022-07-26T20:45:42.712485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data['PassengerId'] = data['PassengerId']","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.715310Z","iopub.execute_input":"2022-07-26T20:45:42.716069Z","iopub.status.idle":"2022-07-26T20:45:42.724668Z","shell.execute_reply.started":"2022-07-26T20:45:42.716024Z","shell.execute_reply":"2022-07-26T20:45:42.723735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalized_data.head(10)\n ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.725646Z","iopub.execute_input":"2022-07-26T20:45:42.726457Z","iopub.status.idle":"2022-07-26T20:45:42.753289Z","shell.execute_reply.started":"2022-07-26T20:45:42.726425Z","shell.execute_reply":"2022-07-26T20:45:42.752007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://machinelearningmastery.com/calculate-feature-importance-with-python/","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.754774Z","iopub.execute_input":"2022-07-26T20:45:42.755952Z","iopub.status.idle":"2022-07-26T20:45:42.762573Z","shell.execute_reply.started":"2022-07-26T20:45:42.755900Z","shell.execute_reply":"2022-07-26T20:45:42.761748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.763599Z","iopub.execute_input":"2022-07-26T20:45:42.764311Z","iopub.status.idle":"2022-07-26T20:45:42.774934Z","shell.execute_reply.started":"2022-07-26T20:45:42.764281Z","shell.execute_reply":"2022-07-26T20:45:42.774164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(features, classes, test_size= .33,\n                                                      random_state = 1)\n\nprint(x_train.shape, x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.776740Z","iopub.execute_input":"2022-07-26T20:45:42.777493Z","iopub.status.idle":"2022-07-26T20:45:42.788462Z","shell.execute_reply.started":"2022-07-26T20:45:42.777452Z","shell.execute_reply":"2022-07-26T20:45:42.787253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decisionTree = DecisionTreeClassifier(criterion='entropy')\nprint(decisionTree)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.790243Z","iopub.execute_input":"2022-07-26T20:45:42.790992Z","iopub.status.idle":"2022-07-26T20:45:42.798909Z","shell.execute_reply.started":"2022-07-26T20:45:42.790951Z","shell.execute_reply":"2022-07-26T20:45:42.798012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtc_model = decisionTree.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.801783Z","iopub.execute_input":"2022-07-26T20:45:42.802755Z","iopub.status.idle":"2022-07-26T20:45:42.817101Z","shell.execute_reply.started":"2022-07-26T20:45:42.802710Z","shell.execute_reply":"2022-07-26T20:45:42.815990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# feature importance\nimportance =dtc_model.feature_importances_ \n\nfor i,v in enumerate(importance):\n    print('Feature: %0d, Score: %.5f' % (i,v))\n    \n# Barchat for feature importance\npyplot.bar([x for x in range(len(importance))], importance) \npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:42.818482Z","iopub.execute_input":"2022-07-26T20:45:42.818782Z","iopub.status.idle":"2022-07-26T20:45:43.010548Z","shell.execute_reply.started":"2022-07-26T20:45:42.818756Z","shell.execute_reply":"2022-07-26T20:45:43.009402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = dtc_model.predict(x_test) #prediction stores the predicted targets/classes\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.011744Z","iopub.execute_input":"2022-07-26T20:45:43.012054Z","iopub.status.idle":"2022-07-26T20:45:43.019129Z","shell.execute_reply.started":"2022-07-26T20:45:43.012026Z","shell.execute_reply":"2022-07-26T20:45:43.017914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_true = le.inverse_transform(y_test['Survived'])\ny_pred = le.inverse_transform(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.021387Z","iopub.execute_input":"2022-07-26T20:45:43.021873Z","iopub.status.idle":"2022-07-26T20:45:43.030632Z","shell.execute_reply.started":"2022-07-26T20:45:43.021809Z","shell.execute_reply":"2022-07-26T20:45:43.029496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_true, y_pred)\nlabels = ['N', 'Y']\nConfusionMatrixDisplay(cm, display_labels=labels).plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.032569Z","iopub.execute_input":"2022-07-26T20:45:43.032980Z","iopub.status.idle":"2022-07-26T20:45:43.243233Z","shell.execute_reply.started":"2022-07-26T20:45:43.032939Z","shell.execute_reply":"2022-07-26T20:45:43.242146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_true, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.244555Z","iopub.execute_input":"2022-07-26T20:45:43.244856Z","iopub.status.idle":"2022-07-26T20:45:43.267014Z","shell.execute_reply.started":"2022-07-26T20:45:43.244828Z","shell.execute_reply":"2022-07-26T20:45:43.266227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nos.environ[\"PATH\"] += os.pathsep ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.268246Z","iopub.execute_input":"2022-07-26T20:45:43.268749Z","iopub.status.idle":"2022-07-26T20:45:43.272660Z","shell.execute_reply.started":"2022-07-26T20:45:43.268720Z","shell.execute_reply":"2022-07-26T20:45:43.271903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from graphviz import Source\nfrom sklearn import tree\ngraph = Source( tree.export_graphviz(dtc_model, out_file=None, feature_names=features.columns))","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.273775Z","iopub.execute_input":"2022-07-26T20:45:43.274342Z","iopub.status.idle":"2022-07-26T20:45:43.300908Z","shell.execute_reply.started":"2022-07-26T20:45:43.274308Z","shell.execute_reply":"2022-07-26T20:45:43.299916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier, plot_tree\nplt.figure(figsize=(20,12))\nplot_tree(dtc_model, filled=True, rounded=True, class_names=[\"No Survived\", \"Survived\"], feature_names=features.columns) ","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:43.302590Z","iopub.execute_input":"2022-07-26T20:45:43.302900Z","iopub.status.idle":"2022-07-26T20:45:50.882547Z","shell.execute_reply.started":"2022-07-26T20:45:43.302872Z","shell.execute_reply":"2022-07-26T20:45:50.881273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_decision_tree = round(dtc_model.score(x_train, y_train) * 100, 2)\nacc_decision_tree","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:50.884313Z","iopub.execute_input":"2022-07-26T20:45:50.885376Z","iopub.status.idle":"2022-07-26T20:45:50.895195Z","shell.execute_reply.started":"2022-07-26T20:45:50.885339Z","shell.execute_reply":"2022-07-26T20:45:50.894117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = normalized_data[['PassengerId','Survived']]\nsubmit.to_csv(\"../working/submit.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-26T20:45:50.896736Z","iopub.execute_input":"2022-07-26T20:45:50.897296Z","iopub.status.idle":"2022-07-26T20:45:50.907359Z","shell.execute_reply.started":"2022-07-26T20:45:50.897266Z","shell.execute_reply":"2022-07-26T20:45:50.906371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}