{"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\nimport matplotlib.pyplot as plt # görselleştirme için kullanılıyor\nplt.style.use(\"seaborn-whitegrid\") # seaborn-whitegrid ile görselleştir.\n\nimport seaborn as sns\n\nfrom collections import Counter\n\nimport warnings\nwarnings.filterwarnings(\"ignore\") # noktanın ardından tab'a basılarak kullanılabilcek seçenekler otomatik çıkacakdır(İnternet yavaş ise çalışmayabilir).  |  (ignore) : Python'dan kaynaklı uyarıları görmezden gel(tavsiye edilmez).\n\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-08-01T15:34:43.638521Z","iopub.execute_input":"2022-08-01T15:34:43.638894Z","iopub.status.idle":"2022-08-01T15:34:43.649214Z","shell.execute_reply.started":"2022-08-01T15:34:43.638864Z","shell.execute_reply":"2022-08-01T15:34:43.647873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Introduction\nThe sinking of the Titanic is one of the most infamous shipwrecks in history. In 1912, during her voyage the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew.\n\n<font color = 'blue' >\n    Content:\n   \n1. [Load and Check Data](#1)     \n1. [Variable Description](#2) \n    * [Univariate Variable Analysis](#3)   \n        * [Categorical Variable ](#4)\n        * [Numerical Variable ](#5)     \n1. [Basic Data Analysis](#6)\n1. [Outlier Detection](#7)\n1. [Missing Value](#8)\n    * [Find Value](#9)\n    * [Fill Value](#10)\n1.[Visualization](#11)    \n    * [Correlation Between SibSp -- Parch -- Age -- Fare -- Survived](12)\n    * [SibSp -- Survived](#13)\n    * [Parch -- Survived](#14)\n    * [Pclass -- Survived](#15)\n    * [Age -- Survived](#16)\n    * [Pclass -- Survived -- Age](#17)\n    * [Embarked -- Sex -- Pclass -- Survived](#18)\n    * [Embarked -- Sex -- Fare -- Survived](#19)\n    * [Fill Missing : Age Feauture](#20)","metadata":{}},{"cell_type":"markdown","source":"\n    \n   \n    ","metadata":{}},{"cell_type":"markdown","source":"<a id = \"1\"></a><br>\n# Load and Check Data \n","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntest_PassangerId  = test_df[\"PassengerId\"] #PassengerId ifadesinin ilk değerinin kaybolmaması için bir değişkene atandı.\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:44.493079Z","iopub.execute_input":"2022-08-01T15:34:44.493736Z","iopub.status.idle":"2022-08-01T15:34:44.530356Z","shell.execute_reply.started":"2022-08-01T15:34:44.493699Z","shell.execute_reply":"2022-08-01T15:34:44.529457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns #features","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:44.627040Z","iopub.execute_input":"2022-08-01T15:34:44.627442Z","iopub.status.idle":"2022-08-01T15:34:44.637526Z","shell.execute_reply.started":"2022-08-01T15:34:44.627407Z","shell.execute_reply":"2022-08-01T15:34:44.636408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head() #ilk 5 verinin değerleri gösterildi.","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:44.927403Z","iopub.execute_input":"2022-08-01T15:34:44.928054Z","iopub.status.idle":"2022-08-01T15:34:44.952247Z","shell.execute_reply.started":"2022-08-01T15:34:44.927988Z","shell.execute_reply":"2022-08-01T15:34:44.951007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe() # nümerik(sayısal) feature'lar ile ilgili istatistiksel bilgiler yazdırıldı.","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:45.076488Z","iopub.execute_input":"2022-08-01T15:34:45.076900Z","iopub.status.idle":"2022-08-01T15:34:45.121855Z","shell.execute_reply.started":"2022-08-01T15:34:45.076866Z","shell.execute_reply":"2022-08-01T15:34:45.120792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"2\"></a><br>\n# Variable Description\n1. PassengerId : Unique is number to each passenger\n1. Survived : passenger survived (1) or die (0)\n1. Pclass : passenger class\n1. Name : name of passenger\n1. Sex : gender of passenger\n1. Age : age of passenger\n1. SibSp : number of siblings/spouses\n1. Parch : number of parents/ children\n1. Ticket : ticket number\n1. Fare : amount of money spent on ticket\n1. Cabin : cabin category\n1. Embarked : port where passenger embarked ( C = Cherbourg,         Q = Queenstown,        S = Southampton)","metadata":{}},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:45.501850Z","iopub.execute_input":"2022-08-01T15:34:45.502250Z","iopub.status.idle":"2022-08-01T15:34:45.521519Z","shell.execute_reply.started":"2022-08-01T15:34:45.502215Z","shell.execute_reply":"2022-08-01T15:34:45.520313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- float64(2) : Fare and Age\n- int64(5) : Pclass , Sibsp ,  Parch , PassengerId and Survived\n- object(5) : Name , Cabin , Sex , Embarked and Ticket","metadata":{}},{"cell_type":"markdown","source":"<a id = \"3\"></a><br>\n# Univariate Variable Analysis\n * Categorical Variable : Survived(dead/live) , Sex(F/M) , Pclass , Embarked , Cabin , Name , Ticket , SibSp , Parch\n * Numerical Variable : Age , PassengerId , Fare(bilet fiyatı)","metadata":{}},{"cell_type":"markdown","source":"<a id = \"4\"></a><br>\n## Categorical Variable","metadata":{}},{"cell_type":"code","source":"def bar_plot(variable):\n    \"\"\"\n    input : variable ex: \"Sex\"\n    output : bar plot & value count\n    \"\"\"\n    # get feature\n    var = train_df[variable]\n    # count number of categorical variable(valu/sample)\n    varValue = var.value_counts()#cinsiyetten kaç adet olduğunu göstermeye yarıyor\n    \n    #visualize\n    plt.figure(figsize = (9,3))\n    plt.bar(varValue.index , varValue) # herhangi bir feature içindeki seçenek sayısının gösterir. Ör: A listesinde 4 M ve 4 F olsun.\n    plt.xticks(varValue.index , varValue.index.values) #xtixks : x'de bulunan tik sayısı\n    plt.ylabel(\"Frequency\") # ylabel : y ekseninde bulunan kategorilere ait sample sayısı\n    plt.title(variable)\n    plt.show()\n    print(\"{}: \\n {}\".format(variable, varValue))\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:46.294213Z","iopub.execute_input":"2022-08-01T15:34:46.294999Z","iopub.status.idle":"2022-08-01T15:34:46.305136Z","shell.execute_reply.started":"2022-08-01T15:34:46.294945Z","shell.execute_reply":"2022-08-01T15:34:46.304057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category1 = [\"Survived\" , \"Sex\" , \"Pclass\" , \"Embarked\" , \"SibSp\" , \"Parch\"]\nfor c in category1:\n    bar_plot(c)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:46.446419Z","iopub.execute_input":"2022-08-01T15:34:46.447354Z","iopub.status.idle":"2022-08-01T15:34:47.509293Z","shell.execute_reply.started":"2022-08-01T15:34:46.447303Z","shell.execute_reply":"2022-08-01T15:34:47.507743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"category2 = [\"Cabin\" , \"Name\" , \"Ticket\"] # Aslında categorical olduğunu bilinen ama görselleştirme yapıldığı zaman çok karmaşa yaratacağından emin olunan categorical variable'ları gösteriyor.\nfor c in category2:\n    print(\"{} \\n\".format(train_df[c].value_counts()))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:47.511254Z","iopub.execute_input":"2022-08-01T15:34:47.511639Z","iopub.status.idle":"2022-08-01T15:34:47.525854Z","shell.execute_reply.started":"2022-08-01T15:34:47.511605Z","shell.execute_reply":"2022-08-01T15:34:47.525002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" <a id = \"5\"></a><br>\n ## Numerical Variable","metadata":{}},{"cell_type":"code","source":"def plot_hist(variable):\n    plt.figure(figsize = (9,3))\n    plt.hist(train_df[variable]) #  plt.hist ile train.df'nin tüm variable'larını sırasıyla çağırıldı.\n    plt.xlabel(variable)\n    plt.ylabel(\"Frequency\") # ilgili feature'a ait kaç adet  sample bulunmakta.\n    plt.title(\"{} distrubution with hist\".format(variable))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:47.761805Z","iopub.execute_input":"2022-08-01T15:34:47.762194Z","iopub.status.idle":"2022-08-01T15:34:47.769240Z","shell.execute_reply.started":"2022-08-01T15:34:47.762162Z","shell.execute_reply":"2022-08-01T15:34:47.767781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numericVar = [\"Fare\" , \"Age\" , \"PassengerId\"]\nfor n in numericVar:\n    plot_hist(n)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:47.891667Z","iopub.execute_input":"2022-08-01T15:34:47.892199Z","iopub.status.idle":"2022-08-01T15:34:48.502817Z","shell.execute_reply.started":"2022-08-01T15:34:47.892146Z","shell.execute_reply":"2022-08-01T15:34:48.501658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"6\"></a><br>\n# Basic Data Analysis\n* Pclass - Survived\n* Sex - Survived\n* SibSp -Survived\n* Parch - Survived\n\n\nBu değerlerin birbirleri ile bağlantısının olduğunun kontrolü sağlandı.","metadata":{}},{"cell_type":"code","source":"#Pclass - Survived\ntrain_df[[\"Pclass\",\"Survived\"]] # DataFrame içerisinde bulunan \"Pclass\" ve \"Survived\" feature'larını görüntülemek için","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:48.504817Z","iopub.execute_input":"2022-08-01T15:34:48.505166Z","iopub.status.idle":"2022-08-01T15:34:48.518687Z","shell.execute_reply.started":"2022-08-01T15:34:48.505132Z","shell.execute_reply":"2022-08-01T15:34:48.517190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Pclass - Survived\ntrain_df[[\"Pclass\" , \"Survived\"]].groupby([\"Pclass\"], as_index = False) # Pclass'ın Survived'a olan etkisini öğrenmek için (Pclass'a göre groupby yapıldı) ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:48.521114Z","iopub.execute_input":"2022-08-01T15:34:48.521811Z","iopub.status.idle":"2022-08-01T15:34:48.534413Z","shell.execute_reply.started":"2022-08-01T15:34:48.521760Z","shell.execute_reply":"2022-08-01T15:34:48.533103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Yukarıda bulunan kodda ek olarak verilerin neye göre sıralayacağının da belirtilmesi gerekli. \ntrain_df[[\"Pclass\" , \"Survived\"]].groupby([\"Pclass\"], as_index = False).mean().sort_values(by = \"Survived\", ascending = False) # grupla ve ortalamasını göster\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:48.767839Z","iopub.execute_input":"2022-08-01T15:34:48.769006Z","iopub.status.idle":"2022-08-01T15:34:48.789438Z","shell.execute_reply.started":"2022-08-01T15:34:48.768951Z","shell.execute_reply":"2022-08-01T15:34:48.788116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font color = 'blue' >\nsort_values(by = \"Survived\", ascending = False) : \n</font> \n\nDeğerleri Survived'a göre artan(ascending) olarak sırala.","metadata":{}},{"cell_type":"code","source":"#Sex - Survived\ntrain_df[[\"Sex\" , \"Survived\"]].groupby([\"Sex\"], as_index = False).mean().sort_values(by = \"Survived\", ascending = False) #Sex'in Survived'a olan etkisini öğrenmek için (Sex'e göre groupby yapıldı)  ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:49.138196Z","iopub.execute_input":"2022-08-01T15:34:49.138605Z","iopub.status.idle":"2022-08-01T15:34:49.157164Z","shell.execute_reply.started":"2022-08-01T15:34:49.138571Z","shell.execute_reply":"2022-08-01T15:34:49.156167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# SibSp - Survived\ntrain_df[[\"SibSp\" , \"Survived\"]].groupby([\"SibSp\"], as_index = False).mean().sort_values(by = \"Survived\", ascending = False) #SibSp'in Survived'a olan etkisini öğrenmek için (SibSp'e göre groupby yapıldı)  ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:49.286264Z","iopub.execute_input":"2022-08-01T15:34:49.286709Z","iopub.status.idle":"2022-08-01T15:34:49.303914Z","shell.execute_reply.started":"2022-08-01T15:34:49.286671Z","shell.execute_reply":"2022-08-01T15:34:49.302682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Parch - Survived\ntrain_df[[\"Parch\" , \"Survived\"]].groupby([\"Parch\"], as_index = False).mean().sort_values(by = \"Survived\", ascending = False) # Parch'ın Survived'a olan etkisini öğrenmek için (Parch'a göre groupby yapıldı)  ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:49.532882Z","iopub.execute_input":"2022-08-01T15:34:49.534227Z","iopub.status.idle":"2022-08-01T15:34:49.551305Z","shell.execute_reply.started":"2022-08-01T15:34:49.534171Z","shell.execute_reply":"2022-08-01T15:34:49.549941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Eğer çıkan sonuçlar arasında herhangi bir correlation(ilişki) yoksa bu değerler sınıflandırma modeli için farklı bilgiler sağlayabilir. Yani bu değerler ( ör : Parch ve SubSp) birleştirilirse sınıflandırmada kullanılan modeli eğitmek için yeni bir feature oluşturulabilir.","metadata":{}},{"cell_type":"code","source":"# Parch(parent / child) - SibSp(Sister / spouse)\ntrain_df[[\"Parch\" , \"SibSp\"]].groupby([\"Parch\"], as_index = False).mean().sort_values(by = \"SibSp\", ascending = False) # Parch'ın SibSp'a olan etkisini öğrenmek için (Parch'a göre groupby yapıldı)  ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:49.919443Z","iopub.execute_input":"2022-08-01T15:34:49.920566Z","iopub.status.idle":"2022-08-01T15:34:49.937889Z","shell.execute_reply.started":"2022-08-01T15:34:49.920525Z","shell.execute_reply":"2022-08-01T15:34:49.936781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"7\"></a><br>\n# Outlier Detection\nKısaca istatiksel anlamda elimizde bulunan veriyi bozan sample'lardır. \nÖr: Bir firmada çalışanların maaş değerleri : 1,2,3,4,5,6,7,8,9,100 olsun. Müdür çalışanlarına zam yapacağı zaman çalışanların maaş ort 14.5 oluyor ve bunun yeterli olduğunu varsayıyor. Oysaki 100 değeri çıkartıldığında yeni ort değeri 4.5 olmaktadır. Bu nedenle 100 burada outlier'dır.","metadata":{}},{"cell_type":"markdown","source":"\n1st quartile = Q1 -> 3 (1,2,3,4,5 -> 3 )\n                 \n                 Second quartile (median) = Q3 -> 5.5\n\n3rd quartile = Q3 -> 8 (6,7,8,9,100 -> 8)\n\n\n**\n\nIQR = Q3 -Q1 = 5\n\nOutlier detection step -> 5 x 1.5 = 7.5 \n\nQ1 - 7.5  ||  Q3 + 7.5\n\n   -4.5     |     15.5\n   \n-4.5,1,2,3,4,5,6,7,8,9,15.5,100   -4.5 ve 15.5 aralığında kalan değerler OUTLIER'dır.\n\n!!!Eğer outlier çıkartılmaz ise istatiksel anlamda doğru bilgiler elde edilemez yani Machine Learning modeli doğru eğitilmiş olmaz.(1,2adet olan tolere edilebilir büyük çalışmalar için)\n   \n ","metadata":{}},{"cell_type":"code","source":"def detect_outliers(df, features):\n    outlier_indices = []\n    \n    for c in features:\n        #1st quartile\n        Q1 =  np.percentile(df[c],25)\n        #3rd quartile\n        Q3 = np.percentile(df[c],75)\n        #IQR\n        IQR = Q3 - Q1\n        #outlier step\n        outlier_step = IQR * 1.5\n        #detect outlier and their indices\n        outlier_list_col = df[(df[c] < Q1 - outlier_step) | (df[c] > Q3 + outlier_step)].index\n        # store indices \n        outlier_indices.extend(outlier_list_col)\n   \n    outlier_indices = Counter(outlier_indices)\n    multiple_outliers = list(i for i, v in outlier_indices.items() if v > 2)\n    return multiple_outliers\n            ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:50.499567Z","iopub.execute_input":"2022-08-01T15:34:50.500774Z","iopub.status.idle":"2022-08-01T15:34:50.509287Z","shell.execute_reply.started":"2022-08-01T15:34:50.500724Z","shell.execute_reply":"2022-08-01T15:34:50.508533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hangi veriden kaç adet olduğunu hesaplar : Counter() \na = [\"a\", \"a\" , \"a\", \"a\" , \"a\", \"a\" ,\"[b]\"]\nCounter(a)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:50.627954Z","iopub.execute_input":"2022-08-01T15:34:50.628675Z","iopub.status.idle":"2022-08-01T15:34:50.636190Z","shell.execute_reply.started":"2022-08-01T15:34:50.628623Z","shell.execute_reply":"2022-08-01T15:34:50.635440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[detect_outliers(train_df,[\"Age\",\"SibSp\",\"Parch\",\"Fare\"])]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:50.919376Z","iopub.execute_input":"2022-08-01T15:34:50.919842Z","iopub.status.idle":"2022-08-01T15:34:50.947781Z","shell.execute_reply.started":"2022-08-01T15:34:50.919804Z","shell.execute_reply":"2022-08-01T15:34:50.946622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.loc[detect_outliers(train_df,[\"Age\", \"SibSp\",\"Parch\",\"Fare\"])]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:51.069032Z","iopub.execute_input":"2022-08-01T15:34:51.069427Z","iopub.status.idle":"2022-08-01T15:34:51.096349Z","shell.execute_reply.started":"2022-08-01T15:34:51.069395Z","shell.execute_reply":"2022-08-01T15:34:51.095401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#drop outliers\ntrain_df = train_df.drop(detect_outliers(train_df,[\"Age\", \"SibSp\",\"Parch\",\"Fare\"]), axis=0).reset_index(drop = True )","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:51.311055Z","iopub.execute_input":"2022-08-01T15:34:51.313820Z","iopub.status.idle":"2022-08-01T15:34:51.327549Z","shell.execute_reply.started":"2022-08-01T15:34:51.313775Z","shell.execute_reply":"2022-08-01T15:34:51.326263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"8\"></a><br>\n# Missing Value\n* Find Missing Value\n* Fill Missing Value","metadata":{}},{"cell_type":"markdown","source":"Missing value^ları atabilmek ya da onları doldurabilmek için hem test hem de train dataframe'i içersindeki değerlere bakılmalı. Çünkü missing value'lar yalnızca train'in içerisinde doldurularak eğitilirse oluşturulan ML modelinde test datafreme'inin içerisinde bulunan boş değerleri görünce hata verecektir. Bu nedenle test ve train dataframe'leri birleştirilecektir. Bu sayede \"missing value\" problemi ortadan kalkacaktır.","metadata":{}},{"cell_type":"code","source":"train_df_len = len(train_df)\ntrain_df = pd.concat([train_df, test_df],axis= 0 ).reset_index(drop = True)\n# Bu kısım 1'den fazla kez çalıştırılmamalı, çalıştırılır ise kodların tümü restart edilmeli.","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:51.966548Z","iopub.execute_input":"2022-08-01T15:34:51.966988Z","iopub.status.idle":"2022-08-01T15:34:51.979107Z","shell.execute_reply.started":"2022-08-01T15:34:51.966951Z","shell.execute_reply":"2022-08-01T15:34:51.977832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:52.193874Z","iopub.execute_input":"2022-08-01T15:34:52.194436Z","iopub.status.idle":"2022-08-01T15:34:52.212698Z","shell.execute_reply.started":"2022-08-01T15:34:52.194403Z","shell.execute_reply":"2022-08-01T15:34:52.211952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"9\"></a><br>\n# Find Missing Value","metadata":{}},{"cell_type":"code","source":"train_df.columns[train_df.isnull().any()] # missing value ifadelerinin hangi sütunda olduğu ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:52.917588Z","iopub.execute_input":"2022-08-01T15:34:52.917964Z","iopub.status.idle":"2022-08-01T15:34:52.928461Z","shell.execute_reply.started":"2022-08-01T15:34:52.917932Z","shell.execute_reply":"2022-08-01T15:34:52.927334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum() # kaç adaet missing value var","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:52.968633Z","iopub.execute_input":"2022-08-01T15:34:52.969358Z","iopub.status.idle":"2022-08-01T15:34:52.980099Z","shell.execute_reply.started":"2022-08-01T15:34:52.969305Z","shell.execute_reply":"2022-08-01T15:34:52.979165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"10\"></a><br>\n# Fill Missing Value\n\"Veriyi kaybetmektense doldurmak daha mantıklı\"\n* Embarked has 2 missing value\n* Fare has 1","metadata":{}},{"cell_type":"code","source":"train_df[train_df[\"Embarked\"].isnull()] #60. ve 821. yolcuların gemiye nerden bindiği bilinmiyor","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:53.448435Z","iopub.execute_input":"2022-08-01T15:34:53.449158Z","iopub.status.idle":"2022-08-01T15:34:53.468331Z","shell.execute_reply.started":"2022-08-01T15:34:53.449092Z","shell.execute_reply":"2022-08-01T15:34:53.467179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.boxplot(column = \"Fare\" , by = \"Embarked\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:53.591179Z","iopub.execute_input":"2022-08-01T15:34:53.592408Z","iopub.status.idle":"2022-08-01T15:34:53.800743Z","shell.execute_reply.started":"2022-08-01T15:34:53.592351Z","shell.execute_reply":"2022-08-01T15:34:53.799345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Yolcuların C durağından binme olasıkları çok yüksek.","metadata":{}},{"cell_type":"code","source":"train_df[\"Embarked\"] = train_df[\"Embarked\"].fillna(\"C\")  # Boş olan değerlere C yazıldı.\ntrain_df[train_df[\"Embarked\"].isnull()]#\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:54.101613Z","iopub.execute_input":"2022-08-01T15:34:54.102000Z","iopub.status.idle":"2022-08-01T15:34:54.116947Z","shell.execute_reply.started":"2022-08-01T15:34:54.101966Z","shell.execute_reply":"2022-08-01T15:34:54.115687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Fare\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:54.259525Z","iopub.execute_input":"2022-08-01T15:34:54.260239Z","iopub.status.idle":"2022-08-01T15:34:54.277917Z","shell.execute_reply.started":"2022-08-01T15:34:54.260187Z","shell.execute_reply":"2022-08-01T15:34:54.276626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Pclass\"] == 3]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:54.522683Z","iopub.execute_input":"2022-08-01T15:34:54.523080Z","iopub.status.idle":"2022-08-01T15:34:54.553090Z","shell.execute_reply.started":"2022-08-01T15:34:54.523047Z","shell.execute_reply":"2022-08-01T15:34:54.552001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Pclass\"] == 3][\"Fare\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:54.676575Z","iopub.execute_input":"2022-08-01T15:34:54.676968Z","iopub.status.idle":"2022-08-01T15:34:54.687260Z","shell.execute_reply.started":"2022-08-01T15:34:54.676935Z","shell.execute_reply":"2022-08-01T15:34:54.686128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(train_df[train_df[\"Pclass\"] == 3][\"Fare\"]) # 3. sınfa ait insanların ort ödediği değer 13.675550101832993 ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:54.965169Z","iopub.execute_input":"2022-08-01T15:34:54.966207Z","iopub.status.idle":"2022-08-01T15:34:54.976313Z","shell.execute_reply.started":"2022-08-01T15:34:54.966163Z","shell.execute_reply":"2022-08-01T15:34:54.975558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Fare\"] = train_df[\"Fare\"].fillna(np.mean(train_df[train_df[\"Pclass\"] == 3][\"Fare\"]))\ntrain_df[train_df[\"Embarked\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:55.178231Z","iopub.execute_input":"2022-08-01T15:34:55.179031Z","iopub.status.idle":"2022-08-01T15:34:55.196069Z","shell.execute_reply.started":"2022-08-01T15:34:55.178984Z","shell.execute_reply":"2022-08-01T15:34:55.194868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"11\"></a><br>\n# Visualization","metadata":{}},{"cell_type":"markdown","source":"<a id = \"12\"></a><br>\n## Correlation Between SibSp -- Parch -- Age -- Fare -- Survived","metadata":{}},{"cell_type":"code","source":"list1 = [\"SibSp\" , \"Parch\" , \"Age\" , \"Fare\" , \"Survived\" ]\nsns.heatmap(train_df[list1].corr(), annot = True , fmt = \".2f\") # annot = True : Korelasyon matrisi üzerindeki değerlerin yazılmasını sağlıyor. \n# fmt = \".2f\" : virgülden sonraki 2 basamağın görülmesi\n# annot = True : korelasyon değerleri kutucuklar üzerinde gösteriliyor  \\ \n# annot = False : korelasyon değerleri kutucuklar üzerinde gösterilmiyor","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:56.135589Z","iopub.execute_input":"2022-08-01T15:34:56.135968Z","iopub.status.idle":"2022-08-01T15:34:56.430603Z","shell.execute_reply.started":"2022-08-01T15:34:56.135936Z","shell.execute_reply":"2022-08-01T15:34:56.429808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fair özelliği survived özelliği ile bir korelasyona sahip (doğru orantılı olarak). -> 0.26","metadata":{}},{"cell_type":"markdown","source":"<a id = \"13\"></a><br>\n## SibSp -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.factorplot(x = \"SibSp\" , y = \"Survived\" , data = train_df , kind = \"bar\" , size = 9)\ng.set_ylabels(\"Survived Probability\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:56.805528Z","iopub.execute_input":"2022-08-01T15:34:56.806094Z","iopub.status.idle":"2022-08-01T15:34:57.281009Z","shell.execute_reply.started":"2022-08-01T15:34:56.806061Z","shell.execute_reply":"2022-08-01T15:34:57.280248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Yaşama oranı 2'den fazla aile üyesine sahip olanların haytta kalma oranı daha düşük.\n\nAileddeki birey sayısı (SibSp) 0 , 1 , 2 ise hayatta kalma şansı daha fazla.\n\nWe can consider a new feature describing these categories    \n\n ","metadata":{}},{"cell_type":"markdown","source":"<a id = \"14\"></a><br>\n## Parch -- Survived ","metadata":{}},{"cell_type":"code","source":"g = sns.factorplot(x = \"Parch\" , y = \"Survived\" , kind = \"bar\" , data = train_df , size = 5 )\ng.set_ylabels(\"Survived Probability\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:57.431379Z","iopub.execute_input":"2022-08-01T15:34:57.432008Z","iopub.status.idle":"2022-08-01T15:34:57.850856Z","shell.execute_reply.started":"2022-08-01T15:34:57.431970Z","shell.execute_reply":"2022-08-01T15:34:57.849691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* SibSp and PArch can be used for new feature extraction with th = 3\n* Small families have more chance to survive.\n* There is a standart deviation(standart sapma) in survival of passenger with Parch = 3 ","metadata":{}},{"cell_type":"markdown","source":"<a id = \"15\"></a><br>\n## Pclass -- Survived ","metadata":{}},{"cell_type":"code","source":"g = sns.factorplot ( x = \"Pclass\" , y = \"Survived\", data = train_df , kind = \"bar\", size = 6 )\ng.set_ylabels(\"Survived Probability\")                    \nplt.show()                    ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:58.117538Z","iopub.execute_input":"2022-08-01T15:34:58.118072Z","iopub.status.idle":"2022-08-01T15:34:58.588538Z","shell.execute_reply.started":"2022-08-01T15:34:58.118024Z","shell.execute_reply":"2022-08-01T15:34:58.587261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id = \"16\"></a><br>\n## Age -- Survived ","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df , col = \"Survived\")\ng.map(sns.distplot , \"Age\" , bins = 25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:58.591018Z","iopub.execute_input":"2022-08-01T15:34:58.592230Z","iopub.status.idle":"2022-08-01T15:34:59.026051Z","shell.execute_reply.started":"2022-08-01T15:34:58.592182Z","shell.execute_reply":"2022-08-01T15:34:59.024751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Soldaki grafik = haytta kalmayanlar\n\nSağdaki grafik = hayatta kalanlar\n* Age <= 10 Has a survival rate,\n* Oldest passenger (>80) survived,\n* Large number of 20 years old did not survived,\n* Most passengers are in 15-35 age range,\n* Use age feature in training\n* Use age distribution for missing value of age. ","metadata":{}},{"cell_type":"markdown","source":"<a id = \"17\"></a><br>\n## Pclass -- Survived -- Age","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid( train_df , col = \"Survived\" , row = \"Pclass\" , size = 3)\ng.map(plt.hist , \"Age\" , bins = 25)\ng.add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:34:59.278071Z","iopub.execute_input":"2022-08-01T15:34:59.278449Z","iopub.status.idle":"2022-08-01T15:35:00.680671Z","shell.execute_reply.started":"2022-08-01T15:34:59.278417Z","shell.execute_reply":"2022-08-01T15:35:00.679518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-Pclass değeri 1'e yaklaştıkça haytta kalma oranı daha fazla\n","metadata":{"_kg_hide-output":true}},{"cell_type":"markdown","source":"<a id = \"18\"></a><br>\n## Embarked -- Sex -- Pclass -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df , row = \"Embarked\" , size = 3 )\ng.map(sns.pointplot, \"Pclass\" , \"Survived\" , \"Sex\")\ng.add_legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:35:00.682326Z","iopub.execute_input":"2022-08-01T15:35:00.682633Z","iopub.status.idle":"2022-08-01T15:35:01.897995Z","shell.execute_reply.started":"2022-08-01T15:35:00.682605Z","shell.execute_reply":"2022-08-01T15:35:01.894121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sex is related to survived , Females are survived more than Males.\n* C : Erkeklerin hatta kalma oranı daha fazla\n* S : Kadınların hatta kalma oranı daha fazla\n* Q : Kadınların hatta kalma oranı daha fazla","metadata":{}},{"cell_type":"markdown","source":"<a id = \"19\"></a><br>\n## Embarked -- Sex -- Fare -- Survived","metadata":{}},{"cell_type":"code","source":"g = sns.FacetGrid(train_df , row= \"Embarked\" , col = \"Survived\" , size = 3)\ng.map(sns.barplot, \"Sex\" , \"Fare\")\ng.add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:35:01.899800Z","iopub.execute_input":"2022-08-01T15:35:01.900083Z","iopub.status.idle":"2022-08-01T15:35:03.263466Z","shell.execute_reply.started":"2022-08-01T15:35:01.900056Z","shell.execute_reply":"2022-08-01T15:35:03.262435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Daha çok para ödeyenlerin hayatta kalma olasılıkları daha fazla.\nHayatta kalan kadınlar hayatta kalan erkeklere göre daha fazla para ödemiş ( C limanında )","metadata":{}},{"cell_type":"markdown","source":"<a id = \"20\"></a><br>\n# Fill Missing : Age Feauture","metadata":{}},{"cell_type":"code","source":"train_df[train_df[\"Age\"].isnull()] # Age feature'ında bulunan null değerlerini bul ve bunu dataframe'e aktar.","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:35:03.264787Z","iopub.execute_input":"2022-08-01T15:35:03.265075Z","iopub.status.idle":"2022-08-01T15:35:03.292722Z","shell.execute_reply.started":"2022-08-01T15:35:03.265047Z","shell.execute_reply":"2022-08-01T15:35:03.291567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.factorplot(x = \"Sex\" , y =\"Age\" , data =  train_df , kind = \"box\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:40:08.099252Z","iopub.execute_input":"2022-08-01T15:40:08.100222Z","iopub.status.idle":"2022-08-01T15:40:08.295521Z","shell.execute_reply.started":"2022-08-01T15:40:08.100172Z","shell.execute_reply":"2022-08-01T15:40:08.294333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Kutuların ortasında bulunan çizgi medyanı ifade etmekte.\n* Sex is not informative for age prediction , age distrubution seems to same.\n","metadata":{}},{"cell_type":"code","source":"sns.factorplot(x = \"Sex\" , y =\"Age\" , hue = \"Pclass\" , data =  train_df , kind = \"box\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:46:57.015201Z","iopub.execute_input":"2022-08-01T15:46:57.015619Z","iopub.status.idle":"2022-08-01T15:46:57.357381Z","shell.execute_reply.started":"2022-08-01T15:46:57.015583Z","shell.execute_reply":"2022-08-01T15:46:57.356089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1st class passengers are older than 2nd , and 2nd is older than 3rd class. ","metadata":{}},{"cell_type":"code","source":"sns.factorplot(x = \"Parch\" , y =\"Age\" , data =  train_df , kind = \"box\")\nsns.factorplot(x = \"SibSp\" , y =\"Age\" , data =  train_df , kind = \"box\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:51:40.764941Z","iopub.execute_input":"2022-08-01T15:51:40.765360Z","iopub.status.idle":"2022-08-01T15:51:41.327336Z","shell.execute_reply.started":"2022-08-01T15:51:40.765323Z","shell.execute_reply":"2022-08-01T15:51:41.326322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_df[[\"Age\" , \"Sex\" , \"SibSp\" , \"Parch\" , \"Pclass\"]].corr() , annot = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:55:44.240774Z","iopub.execute_input":"2022-08-01T15:55:44.241954Z","iopub.status.idle":"2022-08-01T15:55:44.505059Z","shell.execute_reply.started":"2022-08-01T15:55:44.241895Z","shell.execute_reply":"2022-08-01T15:55:44.503951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sex özelliği string bir değer döndürdüğü için heatmap'de görülmez. Bu nedenle bu ifadelerin sayısal değerlere dönüştürülmesi gerekir.","metadata":{}},{"cell_type":"code","source":"train_df[\"Sex\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:57:39.653165Z","iopub.execute_input":"2022-08-01T15:57:39.653577Z","iopub.status.idle":"2022-08-01T15:57:39.662374Z","shell.execute_reply.started":"2022-08-01T15:57:39.653540Z","shell.execute_reply":"2022-08-01T15:57:39.661287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Sex\"] = [1 if i == \"male\" else 0 for i in train_df[\"Sex\"]] ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T15:59:43.351773Z","iopub.execute_input":"2022-08-01T15:59:43.352473Z","iopub.status.idle":"2022-08-01T15:59:43.360033Z","shell.execute_reply.started":"2022-08-01T15:59:43.352423Z","shell.execute_reply":"2022-08-01T15:59:43.358771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"Sex\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:00:11.774616Z","iopub.execute_input":"2022-08-01T16:00:11.775630Z","iopub.status.idle":"2022-08-01T16:00:11.783682Z","shell.execute_reply.started":"2022-08-01T16:00:11.775576Z","shell.execute_reply":"2022-08-01T16:00:11.782568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_df[[\"Age\" , \"Sex\" , \"SibSp\" , \"Parch\" , \"Pclass\"]].corr() , annot = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:00:13.625594Z","iopub.execute_input":"2022-08-01T16:00:13.625981Z","iopub.status.idle":"2022-08-01T16:00:13.917261Z","shell.execute_reply.started":"2022-08-01T16:00:13.625950Z","shell.execute_reply":"2022-08-01T16:00:13.916489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Age is not correlated with sex but it is correlated with Parch ,  SibSp and Pclass. ","metadata":{}},{"cell_type":"code","source":"# NaN value'ların indexlerini bulalım.\nindex_nan_age = list(train_df[\"Age\"][train_df[\"Age\"].isnull()].index)\nindex_nan_age","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:10:09.234282Z","iopub.execute_input":"2022-08-01T16:10:09.234666Z","iopub.status.idle":"2022-08-01T16:10:09.248469Z","shell.execute_reply.started":"2022-08-01T16:10:09.234635Z","shell.execute_reply":"2022-08-01T16:10:09.247316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_nan_age = list(train_df[\"Age\"][train_df[\"Age\"].isnull()].index)\nfor i in index_nan_age : \n    age_pred = train_df[\"Age\"][((train_df[\"SibSp\"] == train_df.iloc[i][\"SibSp\"]) & (train_df[\"Parch\"] == train_df.iloc[i][\"Parch\"]) & (train_df[\"Pclass\"] == train_df.iloc[i][\"Pclass\"]))].median()\n    age_med = train_df[\"Age\"].median()\n    if not np.isnan(age_pred) :\n        train_df[\"Age\"].iloc[i] = age_pred\n    else:\n        train_df[\"Age\"].iloc[i] = age_med \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:23:51.446439Z","iopub.execute_input":"2022-08-01T16:23:51.446850Z","iopub.status.idle":"2022-08-01T16:23:51.980641Z","shell.execute_reply.started":"2022-08-01T16:23:51.446815Z","shell.execute_reply":"2022-08-01T16:23:51.979322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"age_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:20:23.457424Z","iopub.execute_input":"2022-08-01T16:20:23.457875Z","iopub.status.idle":"2022-08-01T16:20:23.465006Z","shell.execute_reply.started":"2022-08-01T16:20:23.457836Z","shell.execute_reply":"2022-08-01T16:20:23.463784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"Age\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T16:26:27.029378Z","iopub.execute_input":"2022-08-01T16:26:27.029811Z","iopub.status.idle":"2022-08-01T16:26:27.045445Z","shell.execute_reply.started":"2022-08-01T16:26:27.029773Z","shell.execute_reply":"2022-08-01T16:26:27.044580Z"},"trusted":true},"execution_count":null,"outputs":[]}]}