{"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":"# Analytical Information Systems\n# Titanic Data Machine learning\n# Professor Gefei Zhang\n# -Rasika Danawade\n\n# 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)\nimport seaborn as sn\nimport matplotlib.pyplot as plt\n%matplotlib inline\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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-07-08T21:15:34.751486Z","iopub.execute_input":"2022-07-08T21:15:34.751895Z","iopub.status.idle":"2022-07-08T21:15:34.764906Z","shell.execute_reply.started":"2022-07-08T21:15:34.751864Z","shell.execute_reply":"2022-07-08T21:15:34.763772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic= pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntitanic.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:34.885870Z","iopub.execute_input":"2022-07-08T21:15:34.888695Z","iopub.status.idle":"2022-07-08T21:15:34.917188Z","shell.execute_reply.started":"2022-07-08T21:15:34.888640Z","shell.execute_reply":"2022-07-08T21:15:34.915999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dropping unused columns from dataset\ntitanic.drop(['Name','SibSp','Parch','Ticket','Cabin'],axis='columns', inplace=True)\ntitanic.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:35.126611Z","iopub.execute_input":"2022-07-08T21:15:35.127777Z","iopub.status.idle":"2022-07-08T21:15:35.144631Z","shell.execute_reply.started":"2022-07-08T21:15:35.127724Z","shell.execute_reply":"2022-07-08T21:15:35.143670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic['Sex']=titanic['Sex'].map({'male':0,'female':1})\nfor col in titanic.columns:\n    print(col, titanic[col].isna().sum())\n#titanic.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:35.241633Z","iopub.execute_input":"2022-07-08T21:15:35.242224Z","iopub.status.idle":"2022-07-08T21:15:35.252268Z","shell.execute_reply.started":"2022-07-08T21:15:35.242136Z","shell.execute_reply":"2022-07-08T21:15:35.251428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_survived= titanic.loc[titanic['Survived'] == 1]\nno_died = titanic.loc[titanic['Survived'] == 0]\nno_passenger = len(titanic.index)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:35.420514Z","iopub.execute_input":"2022-07-08T21:15:35.420890Z","iopub.status.idle":"2022-07-08T21:15:35.428821Z","shell.execute_reply.started":"2022-07-08T21:15:35.420860Z","shell.execute_reply":"2022-07-08T21:15:35.427471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(np.where(np.log(titanic.Fare) == np.min(np.log(titanic.Fare))))","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:35.606259Z","iopub.execute_input":"2022-07-08T21:15:35.607228Z","iopub.status.idle":"2022-07-08T21:15:35.611295Z","shell.execute_reply.started":"2022-07-08T21:15:35.607178Z","shell.execute_reply":"2022-07-08T21:15:35.610334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the correlations between all variables\nsn.heatmap(titanic.corr(), annot=True, center=0, cmap = \"Greens\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:35.770518Z","iopub.execute_input":"2022-07-08T21:15:35.771156Z","iopub.status.idle":"2022-07-08T21:15:36.178677Z","shell.execute_reply.started":"2022-07-08T21:15:35.771118Z","shell.execute_reply":"2022-07-08T21:15:36.177451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As per above heatmap correlation exists between Pclass and Fare, hence combining Pclass and fare as per classes in Pclass w.r.t Fare and calculating their mean","metadata":{}},{"cell_type":"code","source":"np.seterr(divide = 'ignore')\nrows = np.where(np.log(titanic.Fare) == np.min(np.log(titanic.Fare)))\n\n#taking mean for all 3 classes wrt to fare\nclass_loc = titanic.iloc[rows[0], 2].values\n# print(class_loc)\nfor cg, row in zip(class_loc, rows[0]):\n    Pclass = titanic.loc[(titanic['Pclass'] == cg)]\n    mean_val = np.mean(Pclass['Fare'])\n    #mean of class(Pclass1,2,3) fare of row w.r.t fare \n    titanic.iloc[row, 5] = mean_val\n    print(titanic.iloc[row, 5])","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:36.180361Z","iopub.execute_input":"2022-07-08T21:15:36.180700Z","iopub.status.idle":"2022-07-08T21:15:36.212469Z","shell.execute_reply.started":"2022-07-08T21:15:36.180671Z","shell.execute_reply":"2022-07-08T21:15:36.211341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gaussian_thm(class_type, fare):\n    data_temp = titanic.loc[(titanic['Survived'] == class_type)]\n\n    mean = np.mean(np.log(data_temp.Fare))\n    sd = np.std(np.log(data_temp.Fare))\n    return (np.exp((((np.log(fare)-mean)/sd)**2)*-0.5)) / (sd * np.sqrt(2*np.pi))","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:36.213667Z","iopub.execute_input":"2022-07-08T21:15:36.214017Z","iopub.status.idle":"2022-07-08T21:15:36.221471Z","shell.execute_reply.started":"2022-07-08T21:15:36.213987Z","shell.execute_reply":"2022-07-08T21:15:36.220357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def p_sex(class_type, sex_type):\n    c_result = titanic.loc[(titanic['Survived'] == class_type) & (titanic['Sex'] == sex_type)]\n    #print(c_result)\n    if class_type == 0:\n        return len(c_result.index)/len(no_died.index)\n    else:\n        return len(c_result.index)/len(no_survived.index)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:36.338669Z","iopub.execute_input":"2022-07-08T21:15:36.339138Z","iopub.status.idle":"2022-07-08T21:15:36.345739Z","shell.execute_reply.started":"2022-07-08T21:15:36.339101Z","shell.execute_reply":"2022-07-08T21:15:36.344450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def p_pclass(class_type, pclass_type):\n    c_result = titanic.loc[(titanic['Survived'] == class_type) & (titanic['Pclass'] == pclass_type)]\n    #print(c_result)\n    if class_type == 0:\n        return len(c_result.index)/len(no_died.index)\n    else:\n        return len(c_result.index)/len(no_survived.index)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:36.492670Z","iopub.execute_input":"2022-07-08T21:15:36.493056Z","iopub.status.idle":"2022-07-08T21:15:36.499944Z","shell.execute_reply.started":"2022-07-08T21:15:36.493026Z","shell.execute_reply":"2022-07-08T21:15:36.498606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def p_survived(class_type):\n    data_temp = titanic.loc[titanic['Survived'] == class_type]\n    return len(data_temp.index)/no_passenger","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:36.652273Z","iopub.execute_input":"2022-07-08T21:15:36.652708Z","iopub.status.idle":"2022-07-08T21:15:36.658134Z","shell.execute_reply.started":"2022-07-08T21:15:36.652673Z","shell.execute_reply":"2022-07-08T21:15:36.657309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def naive_bayes(fare, sex_type, class_type,pclass_type):\n    return np.log(p_sex(class_type, sex_type)) + np.log(gaussian_thm(class_type, fare)) + np.log(p_survived(class_type)) + np.log(p_pclass(class_type, pclass_type))\n#     return np.log(p_sex(class_type, sex_type)) ","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:36.865456Z","iopub.execute_input":"2022-07-08T21:15:36.865834Z","iopub.status.idle":"2022-07-08T21:15:36.873195Z","shell.execute_reply.started":"2022-07-08T21:15:36.865803Z","shell.execute_reply":"2022-07-08T21:15:36.871558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#no_classes is the Survived class types i.e 0 or 1 (count is 2) \ndef classifier(fare, sex_type,pclass_type, no_classes = 2):\n    return np.argmax(np.array([[naive_bayes(fare, sex_type, class_type,pclass_type)] for class_type in range(no_classes)]))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:37.111667Z","iopub.execute_input":"2022-07-08T21:15:37.112154Z","iopub.status.idle":"2022-07-08T21:15:37.120502Z","shell.execute_reply.started":"2022-07-08T21:15:37.112110Z","shell.execute_reply":"2022-07-08T21:15:37.119334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = 0\nfor i, row in titanic.iterrows():\n    fare = round(row['Fare'])\n    sex_type = row['Sex']\n    pclass_type=row['Pclass']\n    try:\n        sur_prediction = classifier(fare, sex_type,pclass_type)\n    except Exception as e:\n        pass\n    if row['Survived'] == sur_prediction:\n        acc += 1\n#         print(f\"{row['PassengerId']}:{row['Survived']}\")\n    else:\n        pass\n        \nprint(acc/len(titanic.index))","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:37.276088Z","iopub.execute_input":"2022-07-08T21:15:37.276567Z","iopub.status.idle":"2022-07-08T21:15:42.780973Z","shell.execute_reply.started":"2022-07-08T21:15:37.276528Z","shell.execute_reply":"2022-07-08T21:15:42.779905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/titanic/test.csv')\ntest_data['Sex'] = test_data['Sex'].map({'female': 1, 'male': 0})\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:42.782817Z","iopub.execute_input":"2022-07-08T21:15:42.784721Z","iopub.status.idle":"2022-07-08T21:15:42.812328Z","shell.execute_reply.started":"2022-07-08T21:15:42.784672Z","shell.execute_reply":"2022-07-08T21:15:42.811449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row_data = np.where(test_data.Fare == 0)\nfor r in row_data:\n    print(test_data.iloc[r, 8])\nclass_loc = test_data.iloc[row_data[0], 1].values\nprint(class_loc)\nfor cg, row in zip(class_loc, row_data[0]):\n    Pclass = test_data.loc[(test_data['Pclass'] == cg)]\n    mean_val = np.mean(Pclass['Fare'])\n    test_data.iloc[row, 8] = mean_val","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:42.813510Z","iopub.execute_input":"2022-07-08T21:15:42.814294Z","iopub.status.idle":"2022-07-08T21:15:42.826355Z","shell.execute_reply.started":"2022-07-08T21:15:42.814261Z","shell.execute_reply":"2022-07-08T21:15:42.825334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for na values in columns\nfor col in test_data.columns:\n    print(col, test_data[col].isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:42.828302Z","iopub.execute_input":"2022-07-08T21:15:42.828941Z","iopub.status.idle":"2022-07-08T21:15:42.844796Z","shell.execute_reply.started":"2022-07-08T21:15:42.828902Z","shell.execute_reply":"2022-07-08T21:15:42.844050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# for na index\nrow_data = test_data['Fare'].index[test_data['Fare'].apply(np.isnan)]\nprint(row_data)\nclass_loc = test_data.iloc[row_data, 1].values\nprint(class_loc)\nfor cg, row in zip(class_loc, row_data):\n    Pclass = test_data.loc[(test_data['Pclass'] == cg)]\n    mean_val = np.mean(Pclass['Fare'])\n    test_data.iloc[row, 8] = mean_val","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:42.846245Z","iopub.execute_input":"2022-07-08T21:15:42.846608Z","iopub.status.idle":"2022-07-08T21:15:42.857189Z","shell.execute_reply.started":"2022-07-08T21:15:42.846577Z","shell.execute_reply":"2022-07-08T21:15:42.856036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking the fixed value\ntest_data.iloc[row_data[0], 8]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:42.858657Z","iopub.execute_input":"2022-07-08T21:15:42.859270Z","iopub.status.idle":"2022-07-08T21:15:42.866934Z","shell.execute_reply.started":"2022-07-08T21:15:42.859239Z","shell.execute_reply":"2022-07-08T21:15:42.865860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predict = []\npassenger_id = []\nfor i, row in test_data.iterrows():\n    fare = round(row['Fare'])\n    sex = row['Sex']\n    pclass_type=row['Pclass']\n    final_predict.append(classifier(fare, sex, pclass_type))\n    passenger_id.append(row['PassengerId'])\n# final_predict[0:20]\nfinal_predict_data = {\"PassengerId\": passenger_id, \"Survived\": final_predict}\noutput_data = pd.DataFrame(final_predict_data)\noutput_data.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:42.868046Z","iopub.execute_input":"2022-07-08T21:15:42.868654Z","iopub.status.idle":"2022-07-08T21:15:45.248487Z","shell.execute_reply.started":"2022-07-08T21:15:42.868619Z","shell.execute_reply":"2022-07-08T21:15:45.247520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_data.to_csv(\"output_data.csv\", index= False)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T21:15:45.249652Z","iopub.execute_input":"2022-07-08T21:15:45.249991Z","iopub.status.idle":"2022-07-08T21:15:45.256342Z","shell.execute_reply.started":"2022-07-08T21:15:45.249961Z","shell.execute_reply":"2022-07-08T21:15:45.255536Z"},"trusted":true},"execution_count":null,"outputs":[]}]}