{"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":{"execution":{"iopub.status.busy":"2022-07-27T12:45:20.861195Z","iopub.execute_input":"2022-07-27T12:45:20.862303Z","iopub.status.idle":"2022-07-27T12:45:20.895274Z","shell.execute_reply.started":"2022-07-27T12:45:20.862198Z","shell.execute_reply":"2022-07-27T12:45:20.894540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport scipy.stats as stats","metadata":{"execution":{"iopub.status.busy":"2022-07-27T12:46:21.488630Z","iopub.execute_input":"2022-07-27T12:46:21.489062Z","iopub.status.idle":"2022-07-27T12:46:22.209308Z","shell.execute_reply.started":"2022-07-27T12:46:21.489027Z","shell.execute_reply":"2022-07-27T12:46:22.208017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T12:46:25.113673Z","iopub.execute_input":"2022-07-27T12:46:25.114536Z","iopub.status.idle":"2022-07-27T12:46:25.156003Z","shell.execute_reply.started":"2022-07-27T12:46:25.114495Z","shell.execute_reply":"2022-07-27T12:46:25.154902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Some Necessary Functions","metadata":{}},{"cell_type":"code","source":"def plot_correlation_heatmap(df):\n    corr = df.corr()\n    sns.heatmap(corr, annot=True)\n    plt.show()\ndef total_spent(df):\n    df2 = df.copy()\n    df2[\"TotalSpent\"] = df2[\"RoomService\"] + df2[\"FoodCourt\"] + df2[\"ShoppingMall\"] + df2[\"Spa\"] + df2[\"VRDeck\"]\n    return df2\n\ndef name_splitter(df):\n    df2 = df.copy()\n    df2[\"Name\"] = df[\"Name\"].str.split(\" \")\n    df2[\"FirstName\"] = df2[\"Name\"].str.get(0)\n    df2[\"LastName\"] = df2[\"Name\"].str.get(1)\n    df2.drop([\"Name\"], axis=1, inplace=True)\n    return df2\n\ndef cabin_splitter(df):\n    df2 = df.copy()\n    df2[\"Cabin\"] = df2[\"Cabin\"].astype('category')\n    df2[\"Deck\"] = df2[\"Cabin\"].apply(lambda x: x.split(\"/\")[0])\n    df2[\"CabinNumber\"] = df2[\"Cabin\"].apply(lambda x: x.split(\"/\")[1])\n    df2[\"Side\"] = df2[\"Cabin\"].apply(lambda x: x.split(\"/\")[2])\n    df2.drop(columns=[\"Cabin\"], inplace=True)\n    return df2\n    \ndef has_spent(df):\n    df2 = df.copy()\n    for i in range(len(df2)):\n        if df2.at[i,\"RoomService\"] > 0 or df2.at[i,\"FoodCourt\"] > 0 or df2.at[i,\"ShoppingMall\"] > 0 or df2.at[i,\"Spa\"] > 0 or df2.at[i,\"VRDeck\"] > 0 :\n            df2.at[i, \"HasSpent\"] = True\n        else:\n            df2.at[i, \"HasSpent\"] = False\n    return df2","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = pd.read_csv('train.csv')\ndf = name_splitter(df)\ndf = cabin_splitter(df)\ndf = total_spent(df)\ndf = has_spent(df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Correlation","metadata":{}},{"cell_type":"code","source":"plot_correlation_heatmap(df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Column Explanations\nPassengerId - A unique Id for each passenger. Each Id takes the form gggg_pp where gggg indicates a group the passenger is travelling with and pp is their number within the group. People in a group are often family members, but not always.\n\nHomePlanet - The planet the passenger departed from, typically their planet of permanent residence.\n\nCryoSleep - Indicates whether the passenger elected to be put into suspended animation for the duration of the voyage. Passengers \nin cryosleep are confined to their cabins.\n\nCabin - The cabin number where the passenger is staying. Takes the form deck/num/side, where side can be either P for Port or S for Starboard.\n\nDestination - The planet the passenger will be debarking to.\n\nAge - The age of the passenger.\n\nVIP - Whether the passenger has paid for special VIP service during the voyage.\n\nRoomService, FoodCourt, ShoppingMall, Spa, VRDeck - Amount the passenger has billed at each of the Spaceship Titanic's many luxury amenities.\n\nName - The first and last names of the passenger.\n\nTransported - Whether the passenger was transported to another dimension. This is the target, the column you are trying to predict.","metadata":{}},{"cell_type":"markdown","source":"- Age Distribution","metadata":{}},{"cell_type":"code","source":"sns.histplot(df['Age']);","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Categorical and Numerical Columns","metadata":{}},{"cell_type":"code","source":"categorical_features = df.select_dtypes(include=['object']).columns\nnumerical_features = df.select_dtypes(include=['int64', 'float64']).columns","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Categorical Features\n1. PassengerId\n2. HomePlanet\n3. CryoSleep\n4. Destination\n5. VIP\n6. FirstName\n7. LastName\n8. Deck\n9. CabinNumber\n10. Side\n11. HasSpent","metadata":{}},{"cell_type":"markdown","source":"### Numerical Features\n1. Age\n2. RoomService\n3. FoodCourt\n4. ShoppingMall\n5. Spa\n6. VRDeck\n7. TotalSpent","metadata":{}},{"cell_type":"code","source":"numerical_features","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[categorical_features].describe()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[numerical_features].describe()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Visualization","metadata":{}},{"cell_type":"code","source":"# Total amount of money spent by VIP, HomePlanet and Destination\nfig, ax = plt.subplots(1,3)\n\nfig.set_figheight(5)\nfig.set_figwidth(18)\n\nplt1 = sns.stripplot(x=\"VIP\", y=\"TotalSpent\", data=df,ax=ax[0])\nplt2 = sns.stripplot(x=\"HomePlanet\", y=\"TotalSpent\", data=df,ax=ax[1])\nplt3 = sns.stripplot(x=\"Destination\", y=\"TotalSpent\", data=df,ax=ax[2])\nplt1.set_title(\"VIP\")\nplt2.set_title(\"HomePlanet\")\nplt3.set_title(\"Destination\")\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *Europa spent more than other planets*\n- *There is no obvious relationship betweeen VIP and money*\n- *People that goes to PSO spent less*","metadata":{}},{"cell_type":"code","source":"# Relationship between VIP and [CryoSleep,HomePlanet and Destination]\nfig, ax = plt.subplots(1,3)\n\nfig.set_figheight(5)\nfig.set_figwidth(15)\n\nplt1 = sns.countplot(x=\"CryoSleep\", data=df,hue=\"VIP\",ax=ax[0])\nplt2 = sns.countplot(x=\"HomePlanet\", data=df,hue=\"VIP\",ax=ax[1])\nplt3 = sns.countplot(x=\"Destination\", data=df,hue=\"VIP\",ax=ax[2])\n \nplt1.set_title(\"CryoSleep\")\nplt2.set_title(\"HomePlanet\")\nplt3.set_title(\"Destination\")\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- **People in CryoSleep are not VIP**\n- **People from Earth are not VIP**","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=\"HomePlanet\", data=df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *Most of the people come from Earth*","metadata":{}},{"cell_type":"code","source":"sns.barplot(x=\"CryoSleep\", y=\"TotalSpent\", data=df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- **People in CryoSleep do not spent money**","metadata":{}},{"cell_type":"code","source":"# Relationship between VIP and money spent on(except age) [Age,RoomService,FoodCourt,ShoppingMall,Spa,VRDeck]\nfig, ax = plt.subplots(2,3)\n\nfig.set_figheight(10)\nfig.set_figwidth(15)\n\nplt1 = sns.boxenplot(x=\"VIP\", y=\"Age\", data=df,ax=ax[0,0])\nplt2 = sns.boxenplot(x=\"VIP\", y=\"RoomService\", data=df,ax=ax[0,1])\nplt3 = sns.boxenplot(x=\"VIP\", y=\"FoodCourt\", data=df,ax=ax[0,2])\nplt4 = sns.boxenplot(x=\"VIP\", y=\"ShoppingMall\", data=df,ax=ax[1,0])\nplt5 = sns.boxenplot(x=\"VIP\", y=\"Spa\", data=df,ax=ax[1,1])\nplt6 = sns.boxenplot(x=\"VIP\", y=\"VRDeck\", data=df,ax=ax[1,2])\n#make bigger plots\nplt1.set_title(\"Age\")\nplt2.set_title(\"RoomService\")\nplt3.set_title(\"FoodCourt\")\nplt4.set_title(\"ShoppingMall\")\nplt5.set_title(\"Spa\")\nplt6.set_title(\"VRDeck\")\nplt2.set_ylim(0,5000)\nplt3.set_ylim(0,15000)\nplt4.set_ylim(0,5000)\nplt5.set_ylim(0,10000)\nplt6.set_ylim(0,10000)\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *VIP members spent money at FoodCourt, Spa and VRDeck generally*\n- *VIP members are older*\n","metadata":{}},{"cell_type":"code","source":"sns.boxenplot(x=\"VIP\", y=\"TotalSpent\", data=df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *VIP members spent more money on average*","metadata":{}},{"cell_type":"code","source":"# Relationship between HomePlanet and money spent on(except age) [Age,RoomService,FoodCourt,ShoppingMall,Spa,VRDeck]\nfig, ax = plt.subplots(2,3)\n\nfig.set_figheight(10)\nfig.set_figwidth(15)\n\nplt1 = sns.boxenplot(x=\"HomePlanet\", y=\"Age\", data=df,ax=ax[0,0])\nplt2 = sns.boxenplot(x=\"HomePlanet\", y=\"RoomService\", data=df,ax=ax[0,1])\nplt3 = sns.boxenplot(x=\"HomePlanet\", y=\"FoodCourt\", data=df,ax=ax[0,2])\nplt4 = sns.boxenplot(x=\"HomePlanet\", y=\"ShoppingMall\", data=df,ax=ax[1,0])\nplt5 = sns.boxenplot(x=\"HomePlanet\", y=\"Spa\", data=df,ax=ax[1,1])\nplt6 = sns.boxenplot(x=\"HomePlanet\", y=\"VRDeck\", data=df,ax=ax[1,2])\n#make bigger plots\nplt1.set_title(\"Age\")\nplt2.set_title(\"RoomService\")\nplt3.set_title(\"FoodCourt\")\nplt4.set_title(\"ShoppingMall\")\nplt5.set_title(\"Spa\")\nplt6.set_title(\"VRDeck\")\nplt2.set_ylim(0,5000)\nplt3.set_ylim(0,15000)\nplt4.set_ylim(0,5000)\nplt5.set_ylim(0,10000)\nplt6.set_ylim(0,10000)\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *Average Age for Homeplanet is Europa > Mars > Earth*\n- *People from Europa does not spent for RoomService and ShoppingMall generally*\n- *People from Mars and Earth does not spent for Spa,VRDeck and FoodCourt generally*\n","metadata":{}},{"cell_type":"code","source":"# Relationship between Destination and money spent on(except age) [Age,RoomService,FoodCourt,ShoppingMall,Spa,VRDeck]\nfig, ax = plt.subplots(2,3)\n\nfig.set_figheight(10)\nfig.set_figwidth(15)\n\nplt1 = sns.boxenplot(x=\"Destination\", y=\"Age\", data=df,ax=ax[0,0],hue=\"VIP\")\nplt2 = sns.boxenplot(x=\"Destination\", y=\"RoomService\", data=df,ax=ax[0,1],hue=\"VIP\")\nplt3 = sns.boxenplot(x=\"Destination\", y=\"FoodCourt\", data=df,ax=ax[0,2],hue=\"VIP\")\nplt4 = sns.boxenplot(x=\"Destination\", y=\"ShoppingMall\", data=df,ax=ax[1,0],hue=\"VIP\")\nplt5 = sns.boxenplot(x=\"Destination\", y=\"Spa\", data=df,ax=ax[1,1],hue=\"VIP\")\nplt6 = sns.boxenplot(x=\"Destination\", y=\"VRDeck\", data=df,ax=ax[1,2],hue=\"VIP\")\n#make bigger plots\nplt1.set_title(\"Age\")\nplt2.set_title(\"RoomService\")\nplt3.set_title(\"FoodCourt\")\nplt4.set_title(\"ShoppingMall\")\nplt5.set_title(\"Spa\")\nplt6.set_title(\"VRDeck\")\nplt2.set_ylim(0,5000)\nplt3.set_ylim(0,15000)\nplt4.set_ylim(0,5000)\nplt5.set_ylim(0,10000)\nplt6.set_ylim(0,10000)\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *This seems very complex and there is only small informations like not VIP persons that goes to PSO does not spent at FoodCourt*","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=\"HomePlanet\", data=df,hue=\"Destination\")\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- **People from Europa does not go to PSO**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2)\n\nfig.set_figheight(5)\nfig.set_figwidth(15)\n\nplt1 = sns.countplot(x=\"Deck\", data=df,hue=\"HomePlanet\",ax=ax[0])\nplt3 = sns.countplot(x=\"Side\", data=df,hue=\"HomePlanet\",ax=ax[1])\n\nplt1.set_title(\"Deck\")\nplt3.set_title(\"Side\")\n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- **At Deck A,B,C,T; people exist only from Europa**\n- **At Deck G, there are only people from Earth**\n- **People from Europa does not exist at Deck F**\n- **People from Earth does not exist at Deck D**","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=\"CryoSleep\", data=df,hue=\"HomePlanet\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxenplot(x=\"HomePlanet\", y=\"TotalSpent\", data=df)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *People from Europa spent more*","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2)\n\nfig.set_figheight(5)\nfig.set_figwidth(15)\n\nplt1 = sns.countplot(x=\"Deck\", data=df,hue=\"CryoSleep\",ax=ax[0])\nplt3 = sns.countplot(x=\"Side\", data=df,hue=\"CryoSleep\",ax=ax[1])\n\nplt1.set_title(\"Deck\")\nplt3.set_title(\"Side\")\n\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *No new information*","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,15),dpi=200)\nsns.catplot(data=df,x='Deck',hue='CryoSleep',col='Destination',kind='count');","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *Some small information*","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,15),dpi=200)\nsns.catplot(data=df,x='HasSpent',hue='CryoSleep',col='Destination',kind='count')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *People not in cryosleep and destination PSO does not spent money*","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,15),dpi=200)\nsns.catplot(data=df,x='Deck',hue='CryoSleep',col='HomePlanet',kind='count');","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- *Some small information*","metadata":{}},{"cell_type":"markdown","source":"#  CONCLUSION\n This information is enogh for filling a lot of missing data. I hope this analysis will be helpful.\n","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}}]}