{"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\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-07-27T18:15:22.594357Z","iopub.execute_input":"2022-07-27T18:15:22.594911Z","iopub.status.idle":"2022-07-27T18:15:22.605833Z","shell.execute_reply.started":"2022-07-27T18:15:22.594870Z","shell.execute_reply":"2022-07-27T18:15:22.604683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.experimental import enable_iterative_imputer\nfrom sklearn.impute import IterativeImputer\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nfrom pandas_profiling import ProfileReport","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:18:30.467725Z","iopub.execute_input":"2022-07-27T18:18:30.468232Z","iopub.status.idle":"2022-07-27T18:18:30.477088Z","shell.execute_reply.started":"2022-07-27T18:18:30.468192Z","shell.execute_reply":"2022-07-27T18:18:30.475698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train  = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/titanic/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:16:24.204809Z","iopub.execute_input":"2022-07-27T18:16:24.205330Z","iopub.status.idle":"2022-07-27T18:16:24.243059Z","shell.execute_reply.started":"2022-07-27T18:16:24.205291Z","shell.execute_reply":"2022-07-27T18:16:24.241957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:16:31.482361Z","iopub.execute_input":"2022-07-27T18:16:31.482912Z","iopub.status.idle":"2022-07-27T18:16:31.526384Z","shell.execute_reply.started":"2022-07-27T18:16:31.482860Z","shell.execute_reply":"2022-07-27T18:16:31.525298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:16:56.926584Z","iopub.execute_input":"2022-07-27T18:16:56.927114Z","iopub.status.idle":"2022-07-27T18:16:56.946612Z","shell.execute_reply.started":"2022-07-27T18:16:56.927072Z","shell.execute_reply":"2022-07-27T18:16:56.945632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"profile = ProfileReport(train, title=\"Pandas Profiling Report\")\nprofile","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:19:02.744169Z","iopub.execute_input":"2022-07-27T18:19:02.744814Z","iopub.status.idle":"2022-07-27T18:19:22.789272Z","shell.execute_reply.started":"2022-07-27T18:19:02.744754Z","shell.execute_reply":"2022-07-27T18:19:22.787575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop([\"PassengerId\",\"Name\",\"Ticket\",\"Cabin\",\"Embarked\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:02:06.014068Z","iopub.execute_input":"2022-07-27T19:02:06.015286Z","iopub.status.idle":"2022-07-27T19:02:06.033512Z","shell.execute_reply.started":"2022-07-27T19:02:06.015176Z","shell.execute_reply":"2022-07-27T19:02:06.032101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:02:10.367484Z","iopub.execute_input":"2022-07-27T19:02:10.368220Z","iopub.status.idle":"2022-07-27T19:02:10.395493Z","shell.execute_reply.started":"2022-07-27T19:02:10.368179Z","shell.execute_reply":"2022-07-27T19:02:10.393266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = OneHotEncoder(handle_unknown = \"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:02:25.080400Z","iopub.execute_input":"2022-07-27T19:02:25.081215Z","iopub.status.idle":"2022-07-27T19:02:25.087247Z","shell.execute_reply.started":"2022-07-27T19:02:25.081161Z","shell.execute_reply":"2022-07-27T19:02:25.085690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder_df = pd.DataFrame(encoder.fit_transform(train[[\"Sex\"]]).toarray())\nnew_train = train.join(encoder_df)\nnew_train.drop(\"Sex\",axis =1 , inplace=True)\nnew_train.columns = [\"Survived\",\"Pclass\",\"Age\",\"SibSp\",\"Parch\",\"Fare\",\"Male\",\"Female\"]\nnew_train","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:02:33.947928Z","iopub.execute_input":"2022-07-27T19:02:33.948651Z","iopub.status.idle":"2022-07-27T19:02:33.983492Z","shell.execute_reply.started":"2022-07-27T19:02:33.948611Z","shell.execute_reply":"2022-07-27T19:02:33.982017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = new_train[\"Survived\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:02:49.440678Z","iopub.execute_input":"2022-07-27T19:02:49.441838Z","iopub.status.idle":"2022-07-27T19:02:49.447236Z","shell.execute_reply.started":"2022-07-27T19:02:49.441797Z","shell.execute_reply":"2022-07-27T19:02:49.445894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train=new_train.drop([\"Survived\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:03:00.549072Z","iopub.execute_input":"2022-07-27T19:03:00.549463Z","iopub.status.idle":"2022-07-27T19:03:00.556653Z","shell.execute_reply.started":"2022-07-27T19:03:00.549431Z","shell.execute_reply":"2022-07-27T19:03:00.555410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_passengerid = test['PassengerId']\ntest.drop([\"PassengerId\",\"Name\",\"Cabin\",\"Embarked\",\"Ticket\"],axis=1,inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:03:12.349210Z","iopub.execute_input":"2022-07-27T19:03:12.350077Z","iopub.status.idle":"2022-07-27T19:03:12.358146Z","shell.execute_reply.started":"2022-07-27T19:03:12.350024Z","shell.execute_reply":"2022-07-27T19:03:12.356918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder_df = pd.DataFrame(encoder.fit_transform(test[[\"Sex\"]]).toarray())\nnew_test = test.join(encoder_df)\nnew_test.drop(\"Sex\",axis =1 , inplace=True)\nnew_test","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:03:20.264638Z","iopub.execute_input":"2022-07-27T19:03:20.265059Z","iopub.status.idle":"2022-07-27T19:03:20.297651Z","shell.execute_reply.started":"2022-07-27T19:03:20.265024Z","shell.execute_reply":"2022-07-27T19:03:20.296499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_test.columns = [\"Pclass\",\"Age\",\"SibSp\",\"Parch\",\"Fare\",\"Male\",\"Female\"]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:03:40.428467Z","iopub.execute_input":"2022-07-27T19:03:40.429284Z","iopub.status.idle":"2022-07-27T19:03:40.437075Z","shell.execute_reply.started":"2022-07-27T19:03:40.429246Z","shell.execute_reply":"2022-07-27T19:03:40.436051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_test","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:03:43.763742Z","iopub.execute_input":"2022-07-27T19:03:43.764630Z","iopub.status.idle":"2022-07-27T19:03:43.787100Z","shell.execute_reply.started":"2022-07-27T19:03:43.764588Z","shell.execute_reply":"2022-07-27T19:03:43.785844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imputer = IterativeImputer(estimator = RandomForestRegressor(),imputation_order = \"ascending\")\nimputer.fit(new_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:03:55.766357Z","iopub.execute_input":"2022-07-27T19:03:55.767067Z","iopub.status.idle":"2022-07-27T19:04:11.412258Z","shell.execute_reply.started":"2022-07-27T19:03:55.767027Z","shell.execute_reply":"2022-07-27T19:04:11.410810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformed_train = imputer.transform(new_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:04:14.750825Z","iopub.execute_input":"2022-07-27T19:04:14.751254Z","iopub.status.idle":"2022-07-27T19:04:14.890563Z","shell.execute_reply.started":"2022-07-27T19:04:14.751217Z","shell.execute_reply":"2022-07-27T19:04:14.889262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformed_train","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:04:17.456009Z","iopub.execute_input":"2022-07-27T19:04:17.456807Z","iopub.status.idle":"2022-07-27T19:04:17.464693Z","shell.execute_reply.started":"2022-07-27T19:04:17.456765Z","shell.execute_reply":"2022-07-27T19:04:17.463182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimputer.fit(new_test)\ntransformed_test = imputer.transform(new_test)\ntransformed_test","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:04:46.683395Z","iopub.execute_input":"2022-07-27T19:04:46.684516Z","iopub.status.idle":"2022-07-27T19:04:59.823989Z","shell.execute_reply.started":"2022-07-27T19:04:46.684470Z","shell.execute_reply":"2022-07-27T19:04:59.823113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# machine learning model","metadata":{}},{"cell_type":"code","source":"logistic_model = LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:05:43.519167Z","iopub.execute_input":"2022-07-27T19:05:43.520085Z","iopub.status.idle":"2022-07-27T19:05:43.525464Z","shell.execute_reply.started":"2022-07-27T19:05:43.520037Z","shell.execute_reply":"2022-07-27T19:05:43.524385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logistic_model.fit(transformed_train,target)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:05:55.584294Z","iopub.execute_input":"2022-07-27T19:05:55.585160Z","iopub.status.idle":"2022-07-27T19:05:55.632329Z","shell.execute_reply.started":"2022-07-27T19:05:55.585118Z","shell.execute_reply":"2022-07-27T19:05:55.630725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = logistic_model.predict(transformed_test)\npred","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:08:03.585911Z","iopub.execute_input":"2022-07-27T19:08:03.586691Z","iopub.status.idle":"2022-07-27T19:08:03.596250Z","shell.execute_reply.started":"2022-07-27T19:08:03.586653Z","shell.execute_reply":"2022-07-27T19:08:03.595282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'PassengerId' : test_passengerid,'Survived' : pred})\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:06:22.571835Z","iopub.execute_input":"2022-07-27T19:06:22.572739Z","iopub.status.idle":"2022-07-27T19:06:22.578515Z","shell.execute_reply.started":"2022-07-27T19:06:22.572685Z","shell.execute_reply":"2022-07-27T19:06:22.577370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = pd.read_csv(\"/kaggle/input/titanic/gender_submission.csv\")\ny_test","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:06:42.024833Z","iopub.execute_input":"2022-07-27T19:06:42.025614Z","iopub.status.idle":"2022-07-27T19:06:42.053549Z","shell.execute_reply.started":"2022-07-27T19:06:42.025575Z","shell.execute_reply":"2022-07-27T19:06:42.052564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = accuracy_score(y_test[\"Survived\"],submission[\"Survived\"])*100","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:06:54.517895Z","iopub.execute_input":"2022-07-27T19:06:54.518612Z","iopub.status.idle":"2022-07-27T19:06:54.525736Z","shell.execute_reply.started":"2022-07-27T19:06:54.518562Z","shell.execute_reply":"2022-07-27T19:06:54.524612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:07:01.899957Z","iopub.execute_input":"2022-07-27T19:07:01.901126Z","iopub.status.idle":"2022-07-27T19:07:01.908315Z","shell.execute_reply.started":"2022-07-27T19:07:01.901043Z","shell.execute_reply":"2022-07-27T19:07:01.907292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_test[\"Survived\"],submission[\"Survived\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:08:55.141330Z","iopub.execute_input":"2022-07-27T19:08:55.142065Z","iopub.status.idle":"2022-07-27T19:08:55.152622Z","shell.execute_reply.started":"2022-07-27T19:08:55.142025Z","shell.execute_reply":"2022-07-27T19:08:55.151291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.compare(submission).count()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:07:13.898096Z","iopub.execute_input":"2022-07-27T19:07:13.898524Z","iopub.status.idle":"2022-07-27T19:07:13.921430Z","shell.execute_reply.started":"2022-07-27T19:07:13.898491Z","shell.execute_reply":"2022-07-27T19:07:13.920583Z"},"trusted":true},"execution_count":null,"outputs":[]}]}