{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:44.820433Z","iopub.execute_input":"2026-07-27T02:21:44.821548Z","iopub.status.idle":"2026-07-27T02:21:44.835240Z","shell.execute_reply.started":"2026-07-27T02:21:44.821501Z","shell.execute_reply":"2026-07-27T02:21:44.834163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/competitions/titanic/train.csv')\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:44.836996Z","iopub.execute_input":"2026-07-27T02:21:44.837414Z","iopub.status.idle":"2026-07-27T02:21:44.914176Z","shell.execute_reply.started":"2026-07-27T02:21:44.837287Z","shell.execute_reply":"2026-07-27T02:21:44.913201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:44.915266Z","iopub.execute_input":"2026-07-27T02:21:44.916352Z","iopub.status.idle":"2026-07-27T02:21:44.948482Z","shell.execute_reply.started":"2026-07-27T02:21:44.916319Z","shell.execute_reply":"2026-07-27T02:21:44.947597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.pivot_table(\"Survived\",index=\"Sex\",columns=\"Pclass\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:44.950622Z","iopub.execute_input":"2026-07-27T02:21:44.950933Z","iopub.status.idle":"2026-07-27T02:21:45.003296Z","shell.execute_reply.started":"2026-07-27T02:21:44.950898Z","shell.execute_reply":"2026-07-27T02:21:45.002425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:45.004357Z","iopub.execute_input":"2026-07-27T02:21:45.004711Z","iopub.status.idle":"2026-07-27T02:21:45.013720Z","shell.execute_reply.started":"2026-07-27T02:21:45.004682Z","shell.execute_reply":"2026-07-27T02:21:45.012787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nsns.countplot(x=\"Sex\",hue=\"Survived\",data=train)\nplt.legend(loc=\"upper right\",title=\"Survived - Sex\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:45.014874Z","iopub.execute_input":"2026-07-27T02:21:45.015142Z","iopub.status.idle":"2026-07-27T02:21:46.400805Z","shell.execute_reply.started":"2026-07-27T02:21:45.015118Z","shell.execute_reply":"2026-07-27T02:21:46.399845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.stripplot(x=\"Sex\",y=\"Age\",data=train,hue=\"Survived\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:46.402045Z","iopub.execute_input":"2026-07-27T02:21:46.402797Z","iopub.status.idle":"2026-07-27T02:21:46.610377Z","shell.execute_reply.started":"2026-07-27T02:21:46.402768Z","shell.execute_reply":"2026-07-27T02:21:46.609460Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=\"SibSp\",data=train,hue=\"Survived\")\nplt.legend(loc=\"upper right\",title=\"Survived - SibSp\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:46.611463Z","iopub.execute_input":"2026-07-27T02:21:46.611755Z","iopub.status.idle":"2026-07-27T02:21:46.813929Z","shell.execute_reply.started":"2026-07-27T02:21:46.611729Z","shell.execute_reply":"2026-07-27T02:21:46.813076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.countplot(x=\"Parch\",data=train,hue=\"Survived\")\nplt.legend(loc=\"upper right\",title=\"Survived - parch\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:46.815086Z","iopub.execute_input":"2026-07-27T02:21:46.815448Z","iopub.status.idle":"2026-07-27T02:21:47.001414Z","shell.execute_reply.started":"2026-07-27T02:21:46.815411Z","shell.execute_reply":"2026-07-27T02:21:47.000483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Alone\"]=np.where((train[\"SibSp\"]+train[\"Parch\"])>0,0,1)\ntrain.drop([\"SibSp\",\"Parch\"],axis=1,inplace=True)\nsns.countplot(x=\"Embarked\",data=train,hue=\"Survived\")\nplt.legend(loc=\"upper right\",title=\"Survived - Embarked\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.004408Z","iopub.execute_input":"2026-07-27T02:21:47.004721Z","iopub.status.idle":"2026-07-27T02:21:47.184825Z","shell.execute_reply.started":"2026-07-27T02:21:47.004693Z","shell.execute_reply":"2026-07-27T02:21:47.183948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Ticket\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.185935Z","iopub.execute_input":"2026-07-27T02:21:47.186278Z","iopub.status.idle":"2026-07-27T02:21:47.193070Z","shell.execute_reply.started":"2026-07-27T02:21:47.186243Z","shell.execute_reply":"2026-07-27T02:21:47.192355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.drop([\"Ticket\"],axis=1,inplace=True)\ntrain.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.194118Z","iopub.execute_input":"2026-07-27T02:21:47.194545Z","iopub.status.idle":"2026-07-27T02:21:47.211284Z","shell.execute_reply.started":"2026-07-27T02:21:47.194510Z","shell.execute_reply":"2026-07-27T02:21:47.210419Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.drop([\"Name\",\"PassengerId\",\"Cabin\"],axis=1,inplace=True)\ntrain.corr(numeric_only=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.212352Z","iopub.execute_input":"2026-07-27T02:21:47.212680Z","iopub.status.idle":"2026-07-27T02:21:47.236174Z","shell.execute_reply.started":"2026-07-27T02:21:47.212655Z","shell.execute_reply":"2026-07-27T02:21:47.235236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Age\"]=train[\"Age\"].fillna(train[\"Age\"].median())\ntraining=pd.get_dummies(train,columns=[\"Pclass\",\"Sex\"],drop_first=True)\ntraining","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.237339Z","iopub.execute_input":"2026-07-27T02:21:47.237675Z","iopub.status.idle":"2026-07-27T02:21:47.258116Z","shell.execute_reply.started":"2026-07-27T02:21:47.237649Z","shell.execute_reply":"2026-07-27T02:21:47.257338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\ntrain_standard=StandardScaler()\ntrain_copied=training.copy()\ntrain_standard.fit(train_copied[[\"Age\",\"Fare\"]])\ntrain_std=pd.DataFrame(train_standard.transform(train_copied[[\"Age\",\"Fare\"]]))\ntrain_std","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.259187Z","iopub.execute_input":"2026-07-27T02:21:47.259554Z","iopub.status.idle":"2026-07-27T02:21:47.369293Z","shell.execute_reply.started":"2026-07-27T02:21:47.259517Z","shell.execute_reply":"2026-07-27T02:21:47.368347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training[[\"Age\",\"Fare\"]]=train_std\ntraining","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.370639Z","iopub.execute_input":"2026-07-27T02:21:47.371127Z","iopub.status.idle":"2026-07-27T02:21:47.384311Z","shell.execute_reply.started":"2026-07-27T02:21:47.371100Z","shell.execute_reply":"2026-07-27T02:21:47.383495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\ncols=[\"Age\",\"Fare\",\"Alone\",\"Pclass_2\",\"Pclass_3\",\"Sex_male\"]\nx=training[cols]\ny=training[\"Survived\"]\nmodel=LogisticRegression()\nmodel.fit(x,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.385444Z","iopub.execute_input":"2026-07-27T02:21:47.385798Z","iopub.status.idle":"2026-07-27T02:21:47.700362Z","shell.execute_reply.started":"2026-07-27T02:21:47.385759Z","shell.execute_reply":"2026-07-27T02:21:47.699719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ntrain_predicted=model.predict(x)\naccuracy_score(train_predicted,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.701201Z","iopub.execute_input":"2026-07-27T02:21:47.701696Z","iopub.status.idle":"2026-07-27T02:21:47.713688Z","shell.execute_reply.started":"2026-07-27T02:21:47.701663Z","shell.execute_reply":"2026-07-27T02:21:47.711949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodel=RandomForestClassifier(bootstrap=True,n_estimators=10,criterion=\"gini\",max_depth=None,random_state=1)\nmodel.fit(x,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:47.715177Z","iopub.execute_input":"2026-07-27T02:21:47.715711Z","iopub.status.idle":"2026-07-27T02:21:48.168775Z","shell.execute_reply.started":"2026-07-27T02:21:47.715677Z","shell.execute_reply":"2026-07-27T02:21:48.167890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_predicted2=model.predict(x)\naccuracy_score(train_predicted2,y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.169924Z","iopub.execute_input":"2026-07-27T02:21:48.170705Z","iopub.status.idle":"2026-07-27T02:21:48.183262Z","shell.execute_reply.started":"2026-07-27T02:21:48.170677Z","shell.execute_reply":"2026-07-27T02:21:48.182407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/competitions/titanic/test.csv\")\ntest","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.184484Z","iopub.execute_input":"2026-07-27T02:21:48.184873Z","iopub.status.idle":"2026-07-27T02:21:48.212326Z","shell.execute_reply.started":"2026-07-27T02:21:48.184834Z","shell.execute_reply":"2026-07-27T02:21:48.211456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.drop([\"PassengerId\",\"Name\",\"Cabin\",\"Ticket\"],axis=1,inplace=True)\ntrainmedian=train[\"Age\"].median()\ntrainmedian","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.213591Z","iopub.execute_input":"2026-07-27T02:21:48.213974Z","iopub.status.idle":"2026-07-27T02:21:48.221790Z","shell.execute_reply.started":"2026-07-27T02:21:48.213947Z","shell.execute_reply":"2026-07-27T02:21:48.221025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test[\"Age\"]=test[\"Age\"].fillna(trainmedian)\ntest[\"Fare\"]=test[\"Fare\"].fillna(train[\"Fare\"].median())\ntest[\"Alone\"]=np.where((test[\"SibSp\"]+test[\"Parch\"])>0,0,1)\ntest.drop([\"SibSp\",\"Parch\"],axis=1,inplace=True)\ntesting=pd.get_dummies(test,columns=[\"Pclass\",\"Sex\"],drop_first=True)\ntesting","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.222932Z","iopub.execute_input":"2026-07-27T02:21:48.223217Z","iopub.status.idle":"2026-07-27T02:21:48.252248Z","shell.execute_reply.started":"2026-07-27T02:21:48.223192Z","shell.execute_reply":"2026-07-27T02:21:48.251457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_standard=StandardScaler()\ntest_copied=testing.copy()\ntrain_standard.fit(test_copied[[\"Age\",\"Fare\"]])\ntest_std=pd.DataFrame(train_standard.transform(test_copied[[\"Age\",\"Fare\"]]))\ntest_copied[[\"Age\",\"Fare\"]]=test_std\ntest_copied","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.253351Z","iopub.execute_input":"2026-07-27T02:21:48.253683Z","iopub.status.idle":"2026-07-27T02:21:48.279948Z","shell.execute_reply.started":"2026-07-27T02:21:48.253655Z","shell.execute_reply":"2026-07-27T02:21:48.278856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test=test_copied[cols]\ntest_predicted=model.predict(x_test)\ntest_predicted","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.281112Z","iopub.execute_input":"2026-07-27T02:21:48.281886Z","iopub.status.idle":"2026-07-27T02:21:48.293663Z","shell.execute_reply.started":"2026-07-27T02:21:48.281856Z","shell.execute_reply":"2026-07-27T02:21:48.292854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub=pd.read_csv(\"/kaggle/input/competitions/titanic/gender_submission.csv\")\nsub[\"Survived\"]=list(map(int,test_predicted))\nsub.to_csv(\"submission.csv\",index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-27T02:21:48.294808Z","iopub.execute_input":"2026-07-27T02:21:48.295154Z","iopub.status.idle":"2026-07-27T02:21:48.317005Z","shell.execute_reply.started":"2026-07-27T02:21:48.295127Z","shell.execute_reply":"2026-07-27T02:21:48.316202Z"}},"outputs":[],"execution_count":null}]}