{"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":"# Spaceship Titanic\n \nPredict which passengers are transported to an alternate dimension\n\nTo help rescue crews and retrieve the lost passengers, you are challenged to predict which passengers were transported by the anomaly using records recovered from the spaceship’s damaged computer system.\n\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 in 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.\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.\n\ntest.csv - Personal records for the remaining one-third (~4300) of the passengers, to be used as test data. Your task is to predict the value of Transported for the passengers in this set.\n\nsample_submission.csv - A submission file in the correct format.\nPassengerId - Id for each passenger in the test set.\nTransported - The target. For each passenger, predict either True or False.","metadata":{}},{"cell_type":"markdown","source":"# Change Directory","metadata":{}},{"cell_type":"code","source":"from os import chdir\nchdir(\"../input/spaceship-titanic\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.442285Z","iopub.execute_input":"2022-07-21T06:50:57.442631Z","iopub.status.idle":"2022-07-21T06:50:57.463065Z","shell.execute_reply.started":"2022-07-21T06:50:57.442603Z","shell.execute_reply":"2022-07-21T06:50:57.461251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read and preview data","metadata":{}},{"cell_type":"code","source":"from warnings import filterwarnings\nfilterwarnings(\"ignore\")\n\nimport pandas as pd\ntrain = pd.read_csv(\"train.csv\")\n\ntest = pd.read_csv(\"test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.464682Z","iopub.status.idle":"2022-07-21T06:50:57.465621Z","shell.execute_reply.started":"2022-07-21T06:50:57.465295Z","shell.execute_reply":"2022-07-21T06:50:57.465326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.467367Z","iopub.status.idle":"2022-07-21T06:50:57.468418Z","shell.execute_reply.started":"2022-07-21T06:50:57.468071Z","shell.execute_reply":"2022-07-21T06:50:57.468114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.470776Z","iopub.status.idle":"2022-07-21T06:50:57.471677Z","shell.execute_reply.started":"2022-07-21T06:50:57.471367Z","shell.execute_reply":"2022-07-21T06:50:57.471397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.481337Z","iopub.execute_input":"2022-07-21T06:50:57.482051Z","iopub.status.idle":"2022-07-21T06:50:57.519733Z","shell.execute_reply.started":"2022-07-21T06:50:57.481976Z","shell.execute_reply":"2022-07-21T06:50:57.518473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.522442Z","iopub.execute_input":"2022-07-21T06:50:57.523269Z","iopub.status.idle":"2022-07-21T06:50:57.553489Z","shell.execute_reply.started":"2022-07-21T06:50:57.523215Z","shell.execute_reply":"2022-07-21T06:50:57.551381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Drop discrete column","metadata":{}},{"cell_type":"code","source":"c1=train.drop(labels=[\"PassengerId\",\"Name\"],axis=1)\nc2 = test.drop(labels=[\"PassengerId\",\"Name\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.555452Z","iopub.execute_input":"2022-07-21T06:50:57.556325Z","iopub.status.idle":"2022-07-21T06:50:57.566600Z","shell.execute_reply.started":"2022-07-21T06:50:57.556278Z","shell.execute_reply":"2022-07-21T06:50:57.565473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.571163Z","iopub.execute_input":"2022-07-21T06:50:57.571487Z","iopub.status.idle":"2022-07-21T06:50:57.588367Z","shell.execute_reply.started":"2022-07-21T06:50:57.571458Z","shell.execute_reply":"2022-07-21T06:50:57.587347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.589581Z","iopub.execute_input":"2022-07-21T06:50:57.590078Z","iopub.status.idle":"2022-07-21T06:50:57.601566Z","shell.execute_reply.started":"2022-07-21T06:50:57.590047Z","shell.execute_reply":"2022-07-21T06:50:57.600439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"markdown","source":"Adding new column as Expense that is sum of RoomService,FoodCourt,ShoppingMall,Spa\n\nAdding new column as Deck to be retrived from Cabin number\n\nAdding new column as side to be retrived from Cabin number","metadata":{}},{"cell_type":"code","source":"c1['Expense'] = c1['RoomService']+c1['FoodCourt']+c1['ShoppingMall']+c1['Spa']\nc2['Expense'] = c2['RoomService']+c2['FoodCourt']+c2['ShoppingMall']+c2['Spa']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.603479Z","iopub.execute_input":"2022-07-21T06:50:57.605610Z","iopub.status.idle":"2022-07-21T06:50:57.617430Z","shell.execute_reply.started":"2022-07-21T06:50:57.605562Z","shell.execute_reply":"2022-07-21T06:50:57.615399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.drop(labels=['RoomService','FoodCourt','ShoppingMall','Spa'],axis=1,inplace=True)\nc2.drop(labels=['RoomService','FoodCourt','ShoppingMall','Spa'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.629595Z","iopub.execute_input":"2022-07-21T06:50:57.630564Z","iopub.status.idle":"2022-07-21T06:50:57.641235Z","shell.execute_reply.started":"2022-07-21T06:50:57.630463Z","shell.execute_reply":"2022-07-21T06:50:57.640215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.643726Z","iopub.execute_input":"2022-07-21T06:50:57.644428Z","iopub.status.idle":"2022-07-21T06:50:57.670104Z","shell.execute_reply.started":"2022-07-21T06:50:57.644385Z","shell.execute_reply":"2022-07-21T06:50:57.669037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.671579Z","iopub.execute_input":"2022-07-21T06:50:57.672675Z","iopub.status.idle":"2022-07-21T06:50:57.691823Z","shell.execute_reply.started":"2022-07-21T06:50:57.672598Z","shell.execute_reply":"2022-07-21T06:50:57.690549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.693477Z","iopub.execute_input":"2022-07-21T06:50:57.694179Z","iopub.status.idle":"2022-07-21T06:50:57.708865Z","shell.execute_reply.started":"2022-07-21T06:50:57.694135Z","shell.execute_reply":"2022-07-21T06:50:57.708016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in c1.columns:\n    if c1[i].dtypes =='object':\n        x = c1[i].mode()[0]\n        c1[i] = c1[i].fillna(x)\n    else:\n        x = c1[i].mean()\n        c1[i] = c1[i].fillna(x)\nfor i in c2.columns:\n    if c2[i].dtypes =='object':\n        x = c2[i].mode()[0]\n        c2[i] = c2[i].fillna(x)\n    else:\n        x = c2[i].mean()\n        c2[i] = c2[i].fillna(x)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.711545Z","iopub.execute_input":"2022-07-21T06:50:57.712449Z","iopub.status.idle":"2022-07-21T06:50:57.755898Z","shell.execute_reply.started":"2022-07-21T06:50:57.712414Z","shell.execute_reply":"2022-07-21T06:50:57.754848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.757586Z","iopub.execute_input":"2022-07-21T06:50:57.758278Z","iopub.status.idle":"2022-07-21T06:50:57.774073Z","shell.execute_reply.started":"2022-07-21T06:50:57.758233Z","shell.execute_reply":"2022-07-21T06:50:57.772979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.775802Z","iopub.execute_input":"2022-07-21T06:50:57.776279Z","iopub.status.idle":"2022-07-21T06:50:57.783747Z","shell.execute_reply.started":"2022-07-21T06:50:57.776237Z","shell.execute_reply":"2022-07-21T06:50:57.782689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.785572Z","iopub.execute_input":"2022-07-21T06:50:57.786236Z","iopub.status.idle":"2022-07-21T06:50:57.803332Z","shell.execute_reply.started":"2022-07-21T06:50:57.786194Z","shell.execute_reply":"2022-07-21T06:50:57.802125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.805931Z","iopub.execute_input":"2022-07-21T06:50:57.807057Z","iopub.status.idle":"2022-07-21T06:50:57.813904Z","shell.execute_reply.started":"2022-07-21T06:50:57.807012Z","shell.execute_reply":"2022-07-21T06:50:57.812821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cabin = []\ncabin1 =[]\nfor i in c1.Cabin:\n    n = str(i).split('/')[0]\n    cabin.append(n)\nfor i in c2.Cabin:\n    n = str(i).split('/')[0]\n    cabin1.append(n)\nc1['Deck'] = cabin\nc2['Deck'] = cabin1","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.815211Z","iopub.execute_input":"2022-07-21T06:50:57.816267Z","iopub.status.idle":"2022-07-21T06:50:57.842015Z","shell.execute_reply.started":"2022-07-21T06:50:57.816210Z","shell.execute_reply":"2022-07-21T06:50:57.840878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.843532Z","iopub.execute_input":"2022-07-21T06:50:57.844159Z","iopub.status.idle":"2022-07-21T06:50:57.860887Z","shell.execute_reply.started":"2022-07-21T06:50:57.844115Z","shell.execute_reply":"2022-07-21T06:50:57.859778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.862396Z","iopub.execute_input":"2022-07-21T06:50:57.863498Z","iopub.status.idle":"2022-07-21T06:50:57.881473Z","shell.execute_reply.started":"2022-07-21T06:50:57.863449Z","shell.execute_reply":"2022-07-21T06:50:57.880430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"side = []\nside1 =[]\nfor i in c1.Cabin:\n    n = str(i).split('/')[2]\n    side.append(n)\nfor i in c2.Cabin:\n    n = str(i).split('/')[2]\n    side1.append(n)\nc1['Side'] = side\nc2['Side'] = side1","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.884914Z","iopub.execute_input":"2022-07-21T06:50:57.885443Z","iopub.status.idle":"2022-07-21T06:50:57.904766Z","shell.execute_reply.started":"2022-07-21T06:50:57.885397Z","shell.execute_reply":"2022-07-21T06:50:57.903835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1 = c1.drop(labels=['Cabin'],axis=1)\nc2 = c2.drop(labels=['Cabin'],axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.911282Z","iopub.execute_input":"2022-07-21T06:50:57.911613Z","iopub.status.idle":"2022-07-21T06:50:57.922123Z","shell.execute_reply.started":"2022-07-21T06:50:57.911583Z","shell.execute_reply":"2022-07-21T06:50:57.920894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.923951Z","iopub.execute_input":"2022-07-21T06:50:57.924628Z","iopub.status.idle":"2022-07-21T06:50:57.946560Z","shell.execute_reply.started":"2022-07-21T06:50:57.924586Z","shell.execute_reply":"2022-07-21T06:50:57.945421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.948199Z","iopub.execute_input":"2022-07-21T06:50:57.948853Z","iopub.status.idle":"2022-07-21T06:50:57.967171Z","shell.execute_reply.started":"2022-07-21T06:50:57.948809Z","shell.execute_reply":"2022-07-21T06:50:57.965980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.skew()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.969718Z","iopub.execute_input":"2022-07-21T06:50:57.970460Z","iopub.status.idle":"2022-07-21T06:50:57.985466Z","shell.execute_reply.started":"2022-07-21T06:50:57.970415Z","shell.execute_reply":"2022-07-21T06:50:57.984282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nc1.CryoSleep = le.fit_transform(c1.CryoSleep)\nc1.VIP = le.fit_transform(c1.VIP)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.987049Z","iopub.execute_input":"2022-07-21T06:50:57.987475Z","iopub.status.idle":"2022-07-21T06:50:57.995679Z","shell.execute_reply.started":"2022-07-21T06:50:57.987434Z","shell.execute_reply":"2022-07-21T06:50:57.994805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s1 =['VIP','VRDeck','Expense']","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:57.997018Z","iopub.execute_input":"2022-07-21T06:50:57.998275Z","iopub.status.idle":"2022-07-21T06:50:58.009404Z","shell.execute_reply.started":"2022-07-21T06:50:57.998228Z","shell.execute_reply":"2022-07-21T06:50:58.008403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom numpy import log\nfor j in s1:\n    w = []\n    for i in c1[j]:\n        if(i != 0):\n            w.append(np.log(i)) \n        else: \n            w.append(i)\n            \nc1[j] = w\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.011148Z","iopub.execute_input":"2022-07-21T06:50:58.011860Z","iopub.status.idle":"2022-07-21T06:50:58.045655Z","shell.execute_reply.started":"2022-07-21T06:50:58.011818Z","shell.execute_reply":"2022-07-21T06:50:58.044714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.skew()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.046865Z","iopub.execute_input":"2022-07-21T06:50:58.047215Z","iopub.status.idle":"2022-07-21T06:50:58.062040Z","shell.execute_reply.started":"2022-07-21T06:50:58.047184Z","shell.execute_reply":"2022-07-21T06:50:58.060837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.063091Z","iopub.execute_input":"2022-07-21T06:50:58.063816Z","iopub.status.idle":"2022-07-21T06:50:58.090305Z","shell.execute_reply.started":"2022-07-21T06:50:58.063786Z","shell.execute_reply":"2022-07-21T06:50:58.089075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c2.skew()","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.092175Z","iopub.execute_input":"2022-07-21T06:50:58.092912Z","iopub.status.idle":"2022-07-21T06:50:58.104967Z","shell.execute_reply.started":"2022-07-21T06:50:58.092856Z","shell.execute_reply":"2022-07-21T06:50:58.104257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define X and Y\n","metadata":{}},{"cell_type":"code","source":"Y = c1[['Transported']]\nX = c1.drop(labels=[\"Transported\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.106363Z","iopub.execute_input":"2022-07-21T06:50:58.106895Z","iopub.status.idle":"2022-07-21T06:50:58.112867Z","shell.execute_reply.started":"2022-07-21T06:50:58.106865Z","shell.execute_reply":"2022-07-21T06:50:58.112181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.114063Z","iopub.execute_input":"2022-07-21T06:50:58.114537Z","iopub.status.idle":"2022-07-21T06:50:58.125894Z","shell.execute_reply.started":"2022-07-21T06:50:58.114508Z","shell.execute_reply":"2022-07-21T06:50:58.124865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.128145Z","iopub.execute_input":"2022-07-21T06:50:58.128558Z","iopub.status.idle":"2022-07-21T06:50:58.137655Z","shell.execute_reply.started":"2022-07-21T06:50:58.128516Z","shell.execute_reply":"2022-07-21T06:50:58.136548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat = []\ncon = []\nfor i in X.columns:\n    if X[i].dtypes =='object':\n        cat.append(i)\n    else:\n        con.append(i)\nprint(cat)\nprint(con)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.138825Z","iopub.execute_input":"2022-07-21T06:50:58.139429Z","iopub.status.idle":"2022-07-21T06:50:58.149193Z","shell.execute_reply.started":"2022-07-21T06:50:58.139396Z","shell.execute_reply":"2022-07-21T06:50:58.148388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standardisation on continuous columns","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nss = StandardScaler()\nr = pd.DataFrame(ss.fit_transform(X[con]),columns = con)\nr","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.150537Z","iopub.execute_input":"2022-07-21T06:50:58.150868Z","iopub.status.idle":"2022-07-21T06:50:58.177620Z","shell.execute_reply.started":"2022-07-21T06:50:58.150837Z","shell.execute_reply":"2022-07-21T06:50:58.176499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# On hot encoding on categorical cols","metadata":{}},{"cell_type":"code","source":"r1 = pd.get_dummies(X[cat])\nr1","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.179215Z","iopub.execute_input":"2022-07-21T06:50:58.180217Z","iopub.status.idle":"2022-07-21T06:50:58.211813Z","shell.execute_reply.started":"2022-07-21T06:50:58.180172Z","shell.execute_reply":"2022-07-21T06:50:58.210784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1 = r.join(r1)\nX1","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-21T06:50:58.213131Z","iopub.execute_input":"2022-07-21T06:50:58.213453Z","iopub.status.idle":"2022-07-21T06:50:58.240321Z","shell.execute_reply.started":"2022-07-21T06:50:58.213423Z","shell.execute_reply":"2022-07-21T06:50:58.239043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Remove Outliers\n","metadata":{}},{"cell_type":"code","source":"outliers = []\nfor i in con:\n    outliers.extend(list(r[r[i]>3].index))\n    outliers.extend(list(r[r[i]<-3].index))\nfrom numpy import unique\nol = list(unique(outliers))","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.242046Z","iopub.execute_input":"2022-07-21T06:50:58.242711Z","iopub.status.idle":"2022-07-21T06:50:58.254245Z","shell.execute_reply.started":"2022-07-21T06:50:58.242669Z","shell.execute_reply":"2022-07-21T06:50:58.253136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ol)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.257309Z","iopub.execute_input":"2022-07-21T06:50:58.257806Z","iopub.status.idle":"2022-07-21T06:50:58.265160Z","shell.execute_reply.started":"2022-07-21T06:50:58.257775Z","shell.execute_reply":"2022-07-21T06:50:58.264067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.266787Z","iopub.execute_input":"2022-07-21T06:50:58.267586Z","iopub.status.idle":"2022-07-21T06:50:58.277829Z","shell.execute_reply.started":"2022-07-21T06:50:58.267540Z","shell.execute_reply":"2022-07-21T06:50:58.277051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xnew = X1.drop(index = ol,axis=0)\nY1 = Y.drop(index = ol,axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.279409Z","iopub.execute_input":"2022-07-21T06:50:58.280086Z","iopub.status.idle":"2022-07-21T06:50:58.289612Z","shell.execute_reply.started":"2022-07-21T06:50:58.280044Z","shell.execute_reply":"2022-07-21T06:50:58.288872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xnew.shape\nY1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.291103Z","iopub.execute_input":"2022-07-21T06:50:58.291404Z","iopub.status.idle":"2022-07-21T06:50:58.305922Z","shell.execute_reply.started":"2022-07-21T06:50:58.291375Z","shell.execute_reply":"2022-07-21T06:50:58.304588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xnew.index = range(0,8322)\nY1.index=range(0,8322)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.308957Z","iopub.execute_input":"2022-07-21T06:50:58.309673Z","iopub.status.idle":"2022-07-21T06:50:58.321714Z","shell.execute_reply.started":"2022-07-21T06:50:58.309640Z","shell.execute_reply":"2022-07-21T06:50:58.320706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xnew","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.324087Z","iopub.execute_input":"2022-07-21T06:50:58.324704Z","iopub.status.idle":"2022-07-21T06:50:58.359014Z","shell.execute_reply.started":"2022-07-21T06:50:58.324656Z","shell.execute_reply":"2022-07-21T06:50:58.357753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nYnew = pd.DataFrame(le.fit_transform(Y1))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.360885Z","iopub.execute_input":"2022-07-21T06:50:58.361636Z","iopub.status.idle":"2022-07-21T06:50:58.369117Z","shell.execute_reply.started":"2022-07-21T06:50:58.361587Z","shell.execute_reply":"2022-07-21T06:50:58.367859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Ynew","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.370816Z","iopub.execute_input":"2022-07-21T06:50:58.371607Z","iopub.status.idle":"2022-07-21T06:50:58.384683Z","shell.execute_reply.started":"2022-07-21T06:50:58.371564Z","shell.execute_reply":"2022-07-21T06:50:58.383925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train test split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nxtrain,xtest,ytrain,ytest = train_test_split(Xnew,Ynew,test_size=0.2,random_state=21)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.385747Z","iopub.execute_input":"2022-07-21T06:50:58.386563Z","iopub.status.idle":"2022-07-21T06:50:58.399838Z","shell.execute_reply.started":"2022-07-21T06:50:58.386532Z","shell.execute_reply":"2022-07-21T06:50:58.399045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.406390Z","iopub.execute_input":"2022-07-21T06:50:58.406878Z","iopub.status.idle":"2022-07-21T06:50:58.412602Z","shell.execute_reply.started":"2022-07-21T06:50:58.406847Z","shell.execute_reply":"2022-07-21T06:50:58.411641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlr = LogisticRegression()\nmodel = lr.fit(xtrain,ytrain)\n\npred_tr = model.predict(xtrain)\npred_ts = model.predict(xtest)\n\nfrom sklearn.metrics import accuracy_score\n\nbias = accuracy_score(ytrain,pred_tr)\nvariance = accuracy_score(ytest,pred_ts)\nprint(\"bias:\",round(bias,2))\nprint(\"variance:\",round(variance,2))","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.414519Z","iopub.execute_input":"2022-07-21T06:50:58.415613Z","iopub.status.idle":"2022-07-21T06:50:58.509706Z","shell.execute_reply.started":"2022-07-21T06:50:58.415565Z","shell.execute_reply":"2022-07-21T06:50:58.508492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thus model has best fit","metadata":{}},{"cell_type":"markdown","source":"# Predictions on test set","metadata":{}},{"cell_type":"code","source":"cat = []\ncon = []\nfor i in c2.columns:\n    if c2[i].dtypes =='object':\n        cat.append(i)\n    else:\n        con.append(i)\nprint(cat)\nprint(con)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.511679Z","iopub.execute_input":"2022-07-21T06:50:58.512887Z","iopub.status.idle":"2022-07-21T06:50:58.522861Z","shell.execute_reply.started":"2022-07-21T06:50:58.512834Z","shell.execute_reply":"2022-07-21T06:50:58.521648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = pd.DataFrame(ss.transform(c2[con]),columns = con)\nt1 = pd.get_dummies(c2[cat])\nt2 = t.join(t1)\nt2","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.524964Z","iopub.execute_input":"2022-07-21T06:50:58.525823Z","iopub.status.idle":"2022-07-21T06:50:58.596076Z","shell.execute_reply.started":"2022-07-21T06:50:58.525772Z","shell.execute_reply":"2022-07-21T06:50:58.594733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(t2)\nprediction","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.598138Z","iopub.execute_input":"2022-07-21T06:50:58.599027Z","iopub.status.idle":"2022-07-21T06:50:58.612119Z","shell.execute_reply.started":"2022-07-21T06:50:58.598963Z","shell.execute_reply":"2022-07-21T06:50:58.610879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = le.inverse_transform(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:50:58.614521Z","iopub.execute_input":"2022-07-21T06:50:58.615559Z","iopub.status.idle":"2022-07-21T06:50:58.622314Z","shell.execute_reply.started":"2022-07-21T06:50:58.615504Z","shell.execute_reply":"2022-07-21T06:50:58.621038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = test[['PassengerId']]\nsubmission ['Transported'] = predict\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-21T06:51:08.059258Z","iopub.execute_input":"2022-07-21T06:51:08.059672Z","iopub.status.idle":"2022-07-21T06:51:08.074489Z","shell.execute_reply.started":"2022-07-21T06:51:08.059639Z","shell.execute_reply":"2022-07-21T06:51:08.073319Z"},"trusted":true},"execution_count":null,"outputs":[]}]}