{"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)\nimport random\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-09T08:51:55.599436Z","iopub.execute_input":"2022-08-09T08:51:55.600124Z","iopub.status.idle":"2022-08-09T08:51:55.608818Z","shell.execute_reply.started":"2022-08-09T08:51:55.600086Z","shell.execute_reply":"2022-08-09T08:51:55.607451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic = pd.read_csv('/kaggle/input/titanic/train.csv')\ntest = pd.read_csv('/kaggle/input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:55.675639Z","iopub.execute_input":"2022-08-09T08:51:55.676263Z","iopub.status.idle":"2022-08-09T08:51:55.691295Z","shell.execute_reply.started":"2022-08-09T08:51:55.676230Z","shell.execute_reply":"2022-08-09T08:51:55.689996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:55.740096Z","iopub.execute_input":"2022-08-09T08:51:55.740496Z","iopub.status.idle":"2022-08-09T08:51:55.747860Z","shell.execute_reply.started":"2022-08-09T08:51:55.740463Z","shell.execute_reply":"2022-08-09T08:51:55.746726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:55.813289Z","iopub.execute_input":"2022-08-09T08:51:55.814339Z","iopub.status.idle":"2022-08-09T08:51:55.840150Z","shell.execute_reply.started":"2022-08-09T08:51:55.814296Z","shell.execute_reply":"2022-08-09T08:51:55.839212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:55.878308Z","iopub.execute_input":"2022-08-09T08:51:55.878886Z","iopub.status.idle":"2022-08-09T08:51:55.894283Z","shell.execute_reply.started":"2022-08-09T08:51:55.878847Z","shell.execute_reply":"2022-08-09T08:51:55.892842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic[\"Sex\"]=titanic[\"Sex\"].replace([\"male\",\"female\"],[0,1])","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:55.933148Z","iopub.execute_input":"2022-08-09T08:51:55.934429Z","iopub.status.idle":"2022-08-09T08:51:55.943411Z","shell.execute_reply.started":"2022-08-09T08:51:55.934386Z","shell.execute_reply":"2022-08-09T08:51:55.942347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic[\"Embarked\"]=titanic[\"Embarked\"].replace([\"S\",\"C\",\"Q\"],[1,2,3])","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:55.991222Z","iopub.execute_input":"2022-08-09T08:51:55.992198Z","iopub.status.idle":"2022-08-09T08:51:56.001060Z","shell.execute_reply.started":"2022-08-09T08:51:55.992149Z","shell.execute_reply":"2022-08-09T08:51:56.000037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic[\"Embarked\"] = titanic[\"Embarked\"].fillna(random.randint(1,3))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.050221Z","iopub.execute_input":"2022-08-09T08:51:56.050689Z","iopub.status.idle":"2022-08-09T08:51:56.056443Z","shell.execute_reply.started":"2022-08-09T08:51:56.050653Z","shell.execute_reply":"2022-08-09T08:51:56.055072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic[\"Fare\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.104150Z","iopub.execute_input":"2022-08-09T08:51:56.105357Z","iopub.status.idle":"2022-08-09T08:51:56.117910Z","shell.execute_reply.started":"2022-08-09T08:51:56.105314Z","shell.execute_reply":"2022-08-09T08:51:56.116684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic.loc[ titanic[\"Age\"] <= 5, \"Age\"] = 0\ntitanic.loc[(titanic[\"Age\"] > 5) & (titanic[\"Age\"] <= 15), \"Age\"] = 1\ntitanic.loc[(titanic[\"Age\"] > 15) & (titanic[\"Age\"] <= 25), \"Age\"] = 2\ntitanic.loc[(titanic[\"Age\"] > 25) & (titanic[\"Age\"] <= 35), \"Age\"] = 3\ntitanic.loc[(titanic[\"Age\"] > 35) & (titanic[\"Age\"] <= 45), \"Age\"] = 4\ntitanic.loc[(titanic[\"Age\"] > 45) & (titanic[\"Age\"] <= 55), \"Age\"] = 5\ntitanic.loc[(titanic[\"Age\"] > 55) & (titanic[\"Age\"] <= 65), \"Age\"] = 6\ntitanic.loc[ titanic[\"Age\"] > 65, \"Age\"] = 7","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.157526Z","iopub.execute_input":"2022-08-09T08:51:56.160579Z","iopub.status.idle":"2022-08-09T08:51:56.179038Z","shell.execute_reply.started":"2022-08-09T08:51:56.160508Z","shell.execute_reply":"2022-08-09T08:51:56.177968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic[\"Age\"] = titanic[\"Age\"].fillna(random.randint(0,7))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.207696Z","iopub.execute_input":"2022-08-09T08:51:56.208625Z","iopub.status.idle":"2022-08-09T08:51:56.214947Z","shell.execute_reply.started":"2022-08-09T08:51:56.208574Z","shell.execute_reply":"2022-08-09T08:51:56.213944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"titanic[\"Age\"] = titanic[\"Age\"].astype(int)\ntitanic[\"Age\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.262447Z","iopub.execute_input":"2022-08-09T08:51:56.263917Z","iopub.status.idle":"2022-08-09T08:51:56.273979Z","shell.execute_reply.started":"2022-08-09T08:51:56.263863Z","shell.execute_reply":"2022-08-09T08:51:56.272933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.313399Z","iopub.execute_input":"2022-08-09T08:51:56.314554Z","iopub.status.idle":"2022-08-09T08:51:56.320059Z","shell.execute_reply.started":"2022-08-09T08:51:56.314481Z","shell.execute_reply":"2022-08-09T08:51:56.319054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = [\"Sex\", \"Pclass\", \"SibSp\", \"Parch\", \"Embarked\", \"Age\"]\nX=titanic[features]\ny=titanic[\"Survived\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.366229Z","iopub.execute_input":"2022-08-09T08:51:56.366656Z","iopub.status.idle":"2022-08-09T08:51:56.374365Z","shell.execute_reply.started":"2022-08-09T08:51:56.366618Z","shell.execute_reply":"2022-08-09T08:51:56.373365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(X,y,train_size=0.531, test_size=0.469, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.445357Z","iopub.execute_input":"2022-08-09T08:51:56.446618Z","iopub.status.idle":"2022-08-09T08:51:56.453577Z","shell.execute_reply.started":"2022-08-09T08:51:56.446572Z","shell.execute_reply":"2022-08-09T08:51:56.452403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.498027Z","iopub.execute_input":"2022-08-09T08:51:56.498906Z","iopub.status.idle":"2022-08-09T08:51:56.506816Z","shell.execute_reply.started":"2022-08-09T08:51:56.498868Z","shell.execute_reply":"2022-08-09T08:51:56.505613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LR=LogisticRegression(penalty=\"l2\",C=1.0,solver=\"liblinear\")\nLR.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.554068Z","iopub.execute_input":"2022-08-09T08:51:56.554688Z","iopub.status.idle":"2022-08-09T08:51:56.565065Z","shell.execute_reply.started":"2022-08-09T08:51:56.554654Z","shell.execute_reply":"2022-08-09T08:51:56.563908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_value=LR.predict(x_test)\npredicted_value","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.639696Z","iopub.execute_input":"2022-08-09T08:51:56.640094Z","iopub.status.idle":"2022-08-09T08:51:56.651423Z","shell.execute_reply.started":"2022-08-09T08:51:56.640062Z","shell.execute_reply":"2022-08-09T08:51:56.650279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comparation=pd.DataFrame({\"Actual Value\":y_test,\"Predicted Value\":predicted_value})\ncomparation.sample(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.721052Z","iopub.execute_input":"2022-08-09T08:51:56.721760Z","iopub.status.idle":"2022-08-09T08:51:56.733760Z","shell.execute_reply.started":"2022-08-09T08:51:56.721709Z","shell.execute_reply":"2022-08-09T08:51:56.732440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"av=comparation[\"Actual Value\"]\npv=comparation[\"Predicted Value\"]\n\nacc=accuracy_score(av,pv)\npre=precision_score(av,pv)\nrec=recall_score(av,pv)\n\nprint(\"Accuracy score: \",acc)\nprint(\"Precision score: \",pre)\nprint(\"Recall score: \",rec)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.758381Z","iopub.execute_input":"2022-08-09T08:51:56.758788Z","iopub.status.idle":"2022-08-09T08:51:56.771073Z","shell.execute_reply.started":"2022-08-09T08:51:56.758751Z","shell.execute_reply":"2022-08-09T08:51:56.769865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#set ids as PassengerId and predict survival \nids = test['PassengerId']\n\n#set the output as a dataframe and convert to csv file named submission.csv\noutput = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predicted_value })\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.779487Z","iopub.execute_input":"2022-08-09T08:51:56.780178Z","iopub.status.idle":"2022-08-09T08:51:56.789828Z","shell.execute_reply.started":"2022-08-09T08:51:56.780139Z","shell.execute_reply":"2022-08-09T08:51:56.788616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.815475Z","iopub.execute_input":"2022-08-09T08:51:56.816672Z","iopub.status.idle":"2022-08-09T08:51:56.821846Z","shell.execute_reply.started":"2022-08-09T08:51:56.816627Z","shell.execute_reply":"2022-08-09T08:51:56.820610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forest_model = RandomForestClassifier(n_estimators=2, random_state=0)\nforest_model.fit(x_train, y_train)\ntitanic_RF_pred = forest_model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.865469Z","iopub.execute_input":"2022-08-09T08:51:56.865886Z","iopub.status.idle":"2022-08-09T08:51:56.881732Z","shell.execute_reply.started":"2022-08-09T08:51:56.865851Z","shell.execute_reply":"2022-08-09T08:51:56.880373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"comparation2=pd.DataFrame({\"Actual Value\":y_test,\"Predicted Value\":titanic_RF_pred})\ncomparation2.sample(10)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.919142Z","iopub.execute_input":"2022-08-09T08:51:56.920306Z","iopub.status.idle":"2022-08-09T08:51:56.932561Z","shell.execute_reply.started":"2022-08-09T08:51:56.920258Z","shell.execute_reply":"2022-08-09T08:51:56.931272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"av=comparation2[\"Actual Value\"]\npv=comparation2[\"Predicted Value\"]\n\nacc=accuracy_score(av,pv)\npre=precision_score(av,pv)\nrec=recall_score(av,pv)\n\nprint(\"Accuracy score: \",acc)\nprint(\"Precision score: \",pre)\nprint(\"Recall score: \",rec)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:56.972909Z","iopub.execute_input":"2022-08-09T08:51:56.973872Z","iopub.status.idle":"2022-08-09T08:51:56.985622Z","shell.execute_reply.started":"2022-08-09T08:51:56.973831Z","shell.execute_reply":"2022-08-09T08:51:56.984638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission\n\ndef create_submission(pred):\n    if os.path.exists(\"/kaggle/working/submission.csv\"):\n        os.remove(\"/kaggle/working/submission.csv\")\n    \n    passenger_id = pd.read_csv(\"/kaggle/input/titanic/test.csv\")[\"PassengerId\"]\n    survived = pd.Series(pred).rename(\"Survived\")\n\n    output = pd.concat([passenger_id, survived], axis=1)\n    output.to_csv(\"submission.csv\", index=False)\n    print(\"Submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:57.025621Z","iopub.execute_input":"2022-08-09T08:51:57.026933Z","iopub.status.idle":"2022-08-09T08:51:57.034601Z","shell.execute_reply.started":"2022-08-09T08:51:57.026877Z","shell.execute_reply":"2022-08-09T08:51:57.033441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_submission(titanic_RF_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T08:51:57.058452Z","iopub.execute_input":"2022-08-09T08:51:57.059448Z","iopub.status.idle":"2022-08-09T08:51:57.070983Z","shell.execute_reply.started":"2022-08-09T08:51:57.059405Z","shell.execute_reply":"2022-08-09T08:51:57.070010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}