{"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":"markdown","source":"<h1 align=\"center\" style=\"font-weight: bold\">Spaceship Titanic</h1>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport plotly.express as px\nimport plotly.graph_objects as go","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:26:20.384701Z","iopub.execute_input":"2022-07-20T10:26:20.385017Z","iopub.status.idle":"2022-07-20T10:26:23.362817Z","shell.execute_reply.started":"2022-07-20T10:26:20.384937Z","shell.execute_reply":"2022-07-20T10:26:23.362113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(r'../input/spaceship-titanic/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:40.324975Z","iopub.execute_input":"2022-07-20T10:43:40.325681Z","iopub.status.idle":"2022-07-20T10:43:40.374256Z","shell.execute_reply.started":"2022-07-20T10:43:40.325643Z","shell.execute_reply":"2022-07-20T10:43:40.373466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(r'../input/spaceship-titanic/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:40.554456Z","iopub.execute_input":"2022-07-20T10:43:40.554700Z","iopub.status.idle":"2022-07-20T10:43:40.590892Z","shell.execute_reply.started":"2022-07-20T10:43:40.554672Z","shell.execute_reply":"2022-07-20T10:43:40.589746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(r'../input/spaceship-titanic/sample_submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:40.777462Z","iopub.execute_input":"2022-07-20T10:43:40.777714Z","iopub.status.idle":"2022-07-20T10:43:40.793722Z","shell.execute_reply.started":"2022-07-20T10:43:40.777688Z","shell.execute_reply":"2022-07-20T10:43:40.793050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'train set have {train.shape[0]} rows and {train.shape[1]} columns.')\nprint(f'test set have {test.shape[0]} rows and {test.shape[1]} columns.') \nprint(f'sample_submission set have {sub.shape[0]} rows and {sub.shape[1]} columns.') ","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:40.966499Z","iopub.execute_input":"2022-07-20T10:43:40.966712Z","iopub.status.idle":"2022-07-20T10:43:40.972562Z","shell.execute_reply.started":"2022-07-20T10:43:40.966688Z","shell.execute_reply":"2022-07-20T10:43:40.971804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:41.166488Z","iopub.execute_input":"2022-07-20T10:43:41.167161Z","iopub.status.idle":"2022-07-20T10:43:41.175425Z","shell.execute_reply.started":"2022-07-20T10:43:41.167105Z","shell.execute_reply":"2022-07-20T10:43:41.174649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:41.386629Z","iopub.execute_input":"2022-07-20T10:43:41.387144Z","iopub.status.idle":"2022-07-20T10:43:41.412676Z","shell.execute_reply.started":"2022-07-20T10:43:41.387096Z","shell.execute_reply":"2022-07-20T10:43:41.412031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(['PassengerId','Name'],axis=1,inplace=True)\ntest.drop(['PassengerId','Name'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:41.591588Z","iopub.execute_input":"2022-07-20T10:43:41.591941Z","iopub.status.idle":"2022-07-20T10:43:41.599935Z","shell.execute_reply.started":"2022-07-20T10:43:41.591908Z","shell.execute_reply":"2022-07-20T10:43:41.598988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:41.798721Z","iopub.execute_input":"2022-07-20T10:43:41.799352Z","iopub.status.idle":"2022-07-20T10:43:41.812563Z","shell.execute_reply.started":"2022-07-20T10:43:41.799314Z","shell.execute_reply":"2022-07-20T10:43:41.811692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:42.006020Z","iopub.execute_input":"2022-07-20T10:43:42.006496Z","iopub.status.idle":"2022-07-20T10:43:42.040417Z","shell.execute_reply.started":"2022-07-20T10:43:42.006463Z","shell.execute_reply":"2022-07-20T10:43:42.039730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Referencce: https://www.kaggle.com/code/masatomurakawamm/tabnet-dnn-decisiontree-library-fromscratch\n## Handle 'Cabin' feature\ndef cabin_split(dataframe):\n    df = dataframe.copy()\n    \n    df['Cabin'] = df['Cabin'].astype(str)\n    cabins = df['Cabin'].str.split('/', expand=True)\n    cabins.columns = ['Cabin_deck', 'Cabin_num', 'Cabin_side']\n    \n    df = pd.concat([df, cabins], axis=1)\n    df = df.drop(['Cabin'], axis=1)\n    df['Cabin_deck'].astype(str)\n    df['Cabin_num'] = pd.to_numeric(df['Cabin_num'], errors='coerce')\n    df['Cabin_side'].astype(str)\n    df['Cabin_side'] = df['Cabin_side'].map(lambda x: 'missing' if x is None else x)\n    df['Cabin_deck'] = df['Cabin_deck'].replace('nan','missing')\n    \n    \n    return df\n\ntrain = cabin_split(train)\ntest = cabin_split(test)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:42.197215Z","iopub.execute_input":"2022-07-20T10:43:42.197649Z","iopub.status.idle":"2022-07-20T10:43:42.277685Z","shell.execute_reply.started":"2022-07-20T10:43:42.197617Z","shell.execute_reply":"2022-07-20T10:43:42.277022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['HomePlanet'].replace(np.nan,'missing',inplace=True)\ntrain['CryoSleep'].replace(np.nan,'missing',inplace=True)\ntrain['Destination'].replace(np.nan,'missing',inplace=True)\ntrain['VIP'].replace(np.nan,'missing',inplace=True)\n\ntest['HomePlanet'].replace(np.nan,'missing',inplace=True)\ntest['CryoSleep'].replace(np.nan,'missing',inplace=True)\ntest['Destination'].replace(np.nan,'missing',inplace=True)\ntest['VIP'].replace(np.nan,'missing',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:42.408956Z","iopub.execute_input":"2022-07-20T10:43:42.409192Z","iopub.status.idle":"2022-07-20T10:43:42.434834Z","shell.execute_reply.started":"2022-07-20T10:43:42.409167Z","shell.execute_reply":"2022-07-20T10:43:42.434205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Age'].mode()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:42.816488Z","iopub.execute_input":"2022-07-20T10:43:42.816998Z","iopub.status.idle":"2022-07-20T10:43:42.823338Z","shell.execute_reply.started":"2022-07-20T10:43:42.816962Z","shell.execute_reply":"2022-07-20T10:43:42.822503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['RoomService'].fillna(train['RoomService'].mean(),inplace=True)\ntrain['FoodCourt'].fillna(train['FoodCourt'].mean(),inplace=True)\ntrain['ShoppingMall'].fillna(train['ShoppingMall'].mean(),inplace=True)\ntrain['Spa'].fillna(train['Spa'].mean(),inplace=True)\ntrain['VRDeck'].fillna(train['VRDeck'].mean(),inplace=True)\ntrain['Age'].replace(np.nan,24.0,inplace=True)\n\ntest['RoomService'].fillna(test['RoomService'].mean(),inplace=True)\ntest['FoodCourt'].fillna(test['FoodCourt'].mean(),inplace=True)\ntest['ShoppingMall'].fillna(test['ShoppingMall'].mean(),inplace=True)\ntest['Spa'].fillna(test['Spa'].mean(),inplace=True)\ntest['VRDeck'].fillna(test['VRDeck'].mean(),inplace=True)\ntest['Age'].replace(np.nan,24.0,inplace=True)\n\ntrain['Cabin_side'] = train['Cabin_side'].replace({'P':'port','S':'starboard'})\ntest['Cabin_side'] = test['Cabin_side'].replace({'P':'port','S':'starboard'})","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:43.654208Z","iopub.execute_input":"2022-07-20T10:43:43.654921Z","iopub.status.idle":"2022-07-20T10:43:43.675421Z","shell.execute_reply.started":"2022-07-20T10:43:43.654869Z","shell.execute_reply":"2022-07-20T10:43:43.674508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:44.120594Z","iopub.execute_input":"2022-07-20T10:43:44.121280Z","iopub.status.idle":"2022-07-20T10:43:44.136039Z","shell.execute_reply.started":"2022-07-20T10:43:44.121243Z","shell.execute_reply":"2022-07-20T10:43:44.135391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Cabin_num.fillna(train['Cabin_num'].median(),inplace=True) \ntest.Cabin_num.fillna(test['Cabin_num'].median(),inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:44.358483Z","iopub.execute_input":"2022-07-20T10:43:44.359312Z","iopub.status.idle":"2022-07-20T10:43:44.368482Z","shell.execute_reply.started":"2022-07-20T10:43:44.359254Z","shell.execute_reply":"2022-07-20T10:43:44.367323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data visualization:\n#### **categorical features:**","metadata":{}},{"cell_type":"code","source":"fig = go.Figure(data=[go.Pie(labels=train.HomePlanet, hole=.4)])\nfig.add_annotation(text='HomePlanet',\n                   x=0.5,y=0.5,showarrow=False,font_size=14,opacity=0.7,font_family='monospace')\nfig.update_traces(hoverinfo='label+percent+value',\n                  marker=dict(colors=['darkorange','blue'], line=dict(color='#000000', width=2)))\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='HomePlanet',x=0.47,y=0.98,\n               font=dict(color='black',size=20)),\n    legend=dict(orientation='v',traceorder='reversed'),\n    hoverlabel=dict(bgcolor='white'))\nfig.update_traces(textposition='outside', textinfo='percent+label')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:44.815350Z","iopub.execute_input":"2022-07-20T10:43:44.815826Z","iopub.status.idle":"2022-07-20T10:43:44.871491Z","shell.execute_reply.started":"2022-07-20T10:43:44.815781Z","shell.execute_reply":"2022-07-20T10:43:44.870823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Pie(labels=train.CryoSleep, hole=.4)])\nfig.add_annotation(text='CryoSleep',\n                   x=0.5,y=0.5,showarrow=False,font_size=14,opacity=0.7,font_family='monospace')\nfig.update_traces(hoverinfo='label+percent+value',\n                  marker=dict(colors=['darkorange','blue'], line=dict(color='#000000', width=2)))\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='CryoSleep',x=0.47,y=0.98,\n               font=dict(color='black',size=20)),\n    legend=dict(orientation='v',traceorder='reversed'),\n    hoverlabel=dict(bgcolor='white'))\nfig.update_traces(textposition='outside', textinfo='percent+label')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:45.043807Z","iopub.execute_input":"2022-07-20T10:43:45.044195Z","iopub.status.idle":"2022-07-20T10:43:45.097857Z","shell.execute_reply.started":"2022-07-20T10:43:45.044165Z","shell.execute_reply":"2022-07-20T10:43:45.097219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Pie(labels=train.Destination, hole=.4)])\nfig.add_annotation(text='Destination',\n                   x=0.5,y=0.5,showarrow=False,font_size=14,opacity=0.7,font_family='monospace')\nfig.update_traces(hoverinfo='label+percent+value',\n                  marker=dict(colors=['darkorange','blue'], line=dict(color='#000000', width=2)))\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='Destination',x=0.47,y=0.98,\n               font=dict(color='black',size=20)),\n    legend=dict(orientation='v',traceorder='reversed'),\n    hoverlabel=dict(bgcolor='white'))\nfig.update_traces(textposition='outside', textinfo='percent+label')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:45.277103Z","iopub.execute_input":"2022-07-20T10:43:45.277772Z","iopub.status.idle":"2022-07-20T10:43:45.331861Z","shell.execute_reply.started":"2022-07-20T10:43:45.277737Z","shell.execute_reply":"2022-07-20T10:43:45.331225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Pie(labels=train.VIP, hole=.4)])\nfig.add_annotation(text='VIP',\n                   x=0.5,y=0.5,showarrow=False,font_size=14,opacity=0.7,font_family='monospace')\nfig.update_traces(hoverinfo='label+percent+value',\n                  marker=dict(colors=['darkorange','blue'], line=dict(color='#000000', width=2)))\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='VIP',x=0.47,y=0.98,\n               font=dict(color='black',size=20)),\n    legend=dict(orientation='v',traceorder='reversed'),\n    hoverlabel=dict(bgcolor='white'))\nfig.update_traces(textposition='outside', textinfo='percent+label')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:45.502840Z","iopub.execute_input":"2022-07-20T10:43:45.503396Z","iopub.status.idle":"2022-07-20T10:43:45.559534Z","shell.execute_reply.started":"2022-07-20T10:43:45.503362Z","shell.execute_reply":"2022-07-20T10:43:45.558842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Pie(labels=train.Cabin_deck, hole=.4)])\nfig.add_annotation(text='Cabin Deck',\n                   x=0.5,y=0.5,showarrow=False,font_size=14,opacity=0.7,font_family='monospace')\nfig.update_traces(hoverinfo='label+percent+value',\n                  marker=dict(colors=['darkorange','blue'], line=dict(color='#000000', width=2)))\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='Cabin Deck',x=0.47,y=0.98,\n               font=dict(color='black',size=20)),\n    legend=dict(orientation='v',traceorder='reversed'),\n    hoverlabel=dict(bgcolor='white'))\nfig.update_traces(textposition='outside', textinfo='percent+label')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:45.735544Z","iopub.execute_input":"2022-07-20T10:43:45.736279Z","iopub.status.idle":"2022-07-20T10:43:45.787998Z","shell.execute_reply.started":"2022-07-20T10:43:45.736234Z","shell.execute_reply":"2022-07-20T10:43:45.787428Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Pie(labels=train.Cabin_side, hole=.4)])\nfig.add_annotation(text='Cabin Side',\n                   x=0.5,y=0.5,showarrow=False,font_size=14,opacity=0.7,font_family='monospace')\nfig.update_traces(hoverinfo='label+percent+value',\n                  marker=dict(colors=['darkorange','blue'], line=dict(color='#000000', width=2)))\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='Cabin Side',x=0.47,y=0.98,\n               font=dict(color='black',size=20)),\n    legend=dict(orientation='v',traceorder='reversed'),\n    hoverlabel=dict(bgcolor='white'))\nfig.update_traces(textposition='outside', textinfo='percent+label')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:45.980338Z","iopub.execute_input":"2022-07-20T10:43:45.980819Z","iopub.status.idle":"2022-07-20T10:43:46.033931Z","shell.execute_reply.started":"2022-07-20T10:43:45.980783Z","shell.execute_reply":"2022-07-20T10:43:46.033307Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### **numerical features:**","metadata":{}},{"cell_type":"code","source":"fig = px.histogram(train,x='RoomService',template='plotly_dark',\n                  marginal='box',opacity=0.7,nbins=100,color_discrete_sequence=['#FF6692'],\n                  barmode='group',histfunc='count')\n\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='RoomService feature Distribution',x=0.53,y=0.95),\n    xaxis_title_text='RoomService',\n    yaxis_title_text='Count',\n    bargap=0.3,\n)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:46.490287Z","iopub.execute_input":"2022-07-20T10:43:46.490994Z","iopub.status.idle":"2022-07-20T10:43:46.607261Z","shell.execute_reply.started":"2022-07-20T10:43:46.490950Z","shell.execute_reply":"2022-07-20T10:43:46.606329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(train,x='FoodCourt',template='plotly_dark',\n                  marginal='box',opacity=0.7,nbins=100,color_discrete_sequence=['#FECB52'],\n                  barmode='group',histfunc='count')\n\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='FoodCourt feature Distribution',x=0.53,y=0.95),\n    xaxis_title_text='FoodCourt',\n    yaxis_title_text='Count',\n    bargap=0.3,\n)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:46.662957Z","iopub.execute_input":"2022-07-20T10:43:46.663192Z","iopub.status.idle":"2022-07-20T10:43:46.778295Z","shell.execute_reply.started":"2022-07-20T10:43:46.663165Z","shell.execute_reply":"2022-07-20T10:43:46.777489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(train,x='ShoppingMall',template='plotly_dark',\n                  marginal='box',opacity=0.7,nbins=100,color_discrete_sequence=['#FF97FF'],\n                  barmode='group',histfunc='count')\n\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='ShoppingMall feature Distribution',x=0.53,y=0.95),\n    xaxis_title_text='ShoppingMall',\n    yaxis_title_text='Count',\n    bargap=0.3,\n)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:46.885459Z","iopub.execute_input":"2022-07-20T10:43:46.886031Z","iopub.status.idle":"2022-07-20T10:43:47.000003Z","shell.execute_reply.started":"2022-07-20T10:43:46.885992Z","shell.execute_reply":"2022-07-20T10:43:46.999214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(train,x='Spa',template='plotly_dark',\n                  marginal='box',opacity=0.7,nbins=100,color_discrete_sequence=['#636EFA'],\n                  barmode='group',histfunc='count')\n\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='Spa feature Distribution',x=0.53,y=0.95),\n    xaxis_title_text='Spa',\n    yaxis_title_text='Count',\n    bargap=0.3,\n)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:47.198892Z","iopub.execute_input":"2022-07-20T10:43:47.199167Z","iopub.status.idle":"2022-07-20T10:43:47.314151Z","shell.execute_reply.started":"2022-07-20T10:43:47.199133Z","shell.execute_reply":"2022-07-20T10:43:47.313366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.histogram(train,x='VRDeck',template='plotly_dark',\n                  marginal='box',opacity=0.7,nbins=100,color_discrete_sequence=['#EF553B'],\n                  barmode='group',histfunc='count')\n\nfig.update_layout(\n    font_family='monospace',\n    title=dict(text='VRDeck feature Distribution',x=0.53,y=0.95),\n    xaxis_title_text='VRDeck',\n    yaxis_title_text='Count',\n    bargap=0.3,\n)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-20T10:43:47.545508Z","iopub.execute_input":"2022-07-20T10:43:47.545763Z","iopub.status.idle":"2022-07-20T10:43:47.661730Z","shell.execute_reply.started":"2022-07-20T10:43:47.545735Z","shell.execute_reply":"2022-07-20T10:43:47.661096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data pre-processing:","metadata":{}},{"cell_type":"code","source":"cat = ['HomePlanet','CryoSleep','Destination','VIP','Cabin_deck','Cabin_side']\ntrain[cat] = train[cat].astype(str)\ntest[cat] = test[cat].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:49.412776Z","iopub.execute_input":"2022-07-20T10:43:49.413426Z","iopub.status.idle":"2022-07-20T10:43:49.438209Z","shell.execute_reply.started":"2022-07-20T10:43:49.413386Z","shell.execute_reply":"2022-07-20T10:43:49.437345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nfor i in cat:\n    train[i] = le.fit_transform(train[i])\n    test[i] = le.transform(test[i])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:50.756003Z","iopub.execute_input":"2022-07-20T10:43:50.756574Z","iopub.status.idle":"2022-07-20T10:43:50.793317Z","shell.execute_reply.started":"2022-07-20T10:43:50.756538Z","shell.execute_reply":"2022-07-20T10:43:50.792503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['Transported'] = train['Transported'].astype(str)\ntrain['Transported'] = train['Transported'].replace({'False':0,'True':1})","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:54.489937Z","iopub.execute_input":"2022-07-20T10:43:54.490489Z","iopub.status.idle":"2022-07-20T10:43:54.511139Z","shell.execute_reply.started":"2022-07-20T10:43:54.490452Z","shell.execute_reply":"2022-07-20T10:43:54.510071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:43:55.953848Z","iopub.execute_input":"2022-07-20T10:43:55.954251Z","iopub.status.idle":"2022-07-20T10:43:55.974285Z","shell.execute_reply.started":"2022-07-20T10:43:55.954218Z","shell.execute_reply":"2022-07-20T10:43:55.973290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (16,10))\nsns.heatmap(train.corr(), annot = True, cmap=\"YlGnBu\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:44:04.099631Z","iopub.execute_input":"2022-07-20T10:44:04.099905Z","iopub.status.idle":"2022-07-20T10:44:05.204640Z","shell.execute_reply.started":"2022-07-20T10:44:04.099876Z","shell.execute_reply":"2022-07-20T10:44:05.204006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LGBMClassifier","metadata":{}},{"cell_type":"code","source":"y = train['Transported']\nX = train.drop('Transported',axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:44:10.838828Z","iopub.execute_input":"2022-07-20T10:44:10.839374Z","iopub.status.idle":"2022-07-20T10:44:10.845527Z","shell.execute_reply.started":"2022-07-20T10:44:10.839337Z","shell.execute_reply":"2022-07-20T10:44:10.844590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import KFold\n\nfolds = KFold(n_splits=5, shuffle=True)\n\nfor fold, (trn_idx, val_idx) in enumerate(folds.split(X)):\n    print(f\"Fold: {fold}\")\n    X_train, X_test = X.iloc[trn_idx], X.iloc[val_idx]\n    y_train, y_test = y.iloc[trn_idx], y.iloc[val_idx]\n\n    model = LGBMClassifier(n_estimators=2022,learning_rate=0.1)\n   \n    model.fit(X_train, y_train,\n              eval_set=[(X_test, y_test)],\n                early_stopping_rounds=400,\n                verbose=False)\n    y_pred = model.predict(X_test)\n    acc = accuracy_score(y_test, y_pred)\n    \n    print(f\" accuracy_score: {acc}\")\n    print(\"-\"*50)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:44:12.093367Z","iopub.execute_input":"2022-07-20T10:44:12.093954Z","iopub.status.idle":"2022-07-20T10:44:15.714384Z","shell.execute_reply.started":"2022-07-20T10:44:12.093920Z","shell.execute_reply":"2022-07-20T10:44:15.713828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Transported'] = model.predict(test)\nsub['Transported'] = sub['Transported'].replace({0:'False',1:'True'})\nsub.to_csv(f'lgb.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:44:37.921435Z","iopub.execute_input":"2022-07-20T10:44:37.921695Z","iopub.status.idle":"2022-07-20T10:44:37.955716Z","shell.execute_reply.started":"2022-07-20T10:44:37.921667Z","shell.execute_reply":"2022-07-20T10:44:37.955050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Pycaret","metadata":{}},{"cell_type":"code","source":"! pip install pycaret","metadata":{"scrolled":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-20T10:44:41.771710Z","iopub.execute_input":"2022-07-20T10:44:41.771978Z","iopub.status.idle":"2022-07-20T10:45:13.728306Z","shell.execute_reply.started":"2022-07-20T10:44:41.771949Z","shell.execute_reply":"2022-07-20T10:45:13.727472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pycaret.classification import setup, compare_models, blend_models, finalize_model, predict_model","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:45:13.730504Z","iopub.execute_input":"2022-07-20T10:45:13.730787Z","iopub.status.idle":"2022-07-20T10:45:16.804619Z","shell.execute_reply.started":"2022-07-20T10:45:13.730750Z","shell.execute_reply":"2022-07-20T10:45:16.803730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pycaret_model(train, target,test, n_select, fold,opt):\n    print('Setup Your Data....')\n    setup(data=train,\n          target=target,\n          silent= True,use_gpu = True)\n  \n    print('Comparing Models....')\n    best = compare_models(sort = opt,n_select=n_select, fold = fold)\n    \n    print('Blending Models....')\n    blended = blend_models(estimator_list= best, fold=fold, optimize=opt)\n    pred_holdout = predict_model(blended)\n    \n    print('Finallizing Models....')\n    final_model = finalize_model(blended)\n    print('Done...!!!')\n\n    pred_test = predict_model(final_model, test)\n    pred = pred_test['Label']\n    \n    return pred","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:45:16.806305Z","iopub.execute_input":"2022-07-20T10:45:16.806571Z","iopub.status.idle":"2022-07-20T10:45:16.813419Z","shell.execute_reply.started":"2022-07-20T10:45:16.806533Z","shell.execute_reply":"2022-07-20T10:45:16.812644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pycaret_model(train,'Transported',test, 3, 5,'Accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:45:16.815366Z","iopub.execute_input":"2022-07-20T10:45:16.815854Z","iopub.status.idle":"2022-07-20T10:49:00.895171Z","shell.execute_reply.started":"2022-07-20T10:45:16.815820Z","shell.execute_reply":"2022-07-20T10:49:00.893473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Transported'] = result\nsub['Transported'] = sub['Transported'].replace({0:'False',1:'True'})\nsub.to_csv('pycaret_pred.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T10:49:12.893598Z","iopub.execute_input":"2022-07-20T10:49:12.894407Z","iopub.status.idle":"2022-07-20T10:49:12.913294Z","shell.execute_reply.started":"2022-07-20T10:49:12.894371Z","shell.execute_reply":"2022-07-20T10:49:12.912516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"alert alert-info\">\n<h4>If you like this notebook, please upvote it! \n     Thank you! :)</h4>\n</div>","metadata":{}}]}