{"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":"import numpy as np\nimport pandas as pd \n\nTRAIN_PATH = \"../input/titanic/train.csv\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-24T12:52:29.404218Z","iopub.execute_input":"2022-07-24T12:52:29.404602Z","iopub.status.idle":"2022-07-24T12:52:29.409294Z","shell.execute_reply.started":"2022-07-24T12:52:29.404571Z","shell.execute_reply":"2022-07-24T12:52:29.408452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(TRAIN_PATH)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.413960Z","iopub.execute_input":"2022-07-24T12:52:29.414534Z","iopub.status.idle":"2022-07-24T12:52:29.438793Z","shell.execute_reply.started":"2022-07-24T12:52:29.414477Z","shell.execute_reply":"2022-07-24T12:52:29.437624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.441036Z","iopub.execute_input":"2022-07-24T12:52:29.441451Z","iopub.status.idle":"2022-07-24T12:52:29.456096Z","shell.execute_reply.started":"2022-07-24T12:52:29.441409Z","shell.execute_reply":"2022-07-24T12:52:29.454789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = \"Sex\"\nTARGET = \"Survived\"\n\nGROUP_FUNCTION = \"mean\"\nENCODED = \"_encoded\"\n\ntrain[col + ENCODED] = train.groupby(col)[TARGET].transform(GROUP_FUNCTION)\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.479699Z","iopub.execute_input":"2022-07-24T12:52:29.480071Z","iopub.status.idle":"2022-07-24T12:52:29.503299Z","shell.execute_reply.started":"2022-07-24T12:52:29.480041Z","shell.execute_reply":"2022-07-24T12:52:29.502206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[[col,col + ENCODED]].drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.505109Z","iopub.execute_input":"2022-07-24T12:52:29.505543Z","iopub.status.idle":"2022-07-24T12:52:29.519780Z","shell.execute_reply.started":"2022-07-24T12:52:29.505511Z","shell.execute_reply":"2022-07-24T12:52:29.518677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[[col,col + ENCODED]].drop_duplicates().set_index(col)","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.579609Z","iopub.execute_input":"2022-07-24T12:52:29.579979Z","iopub.status.idle":"2022-07-24T12:52:29.594353Z","shell.execute_reply.started":"2022-07-24T12:52:29.579951Z","shell.execute_reply":"2022-07-24T12:52:29.593190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[[col,col + ENCODED]].drop_duplicates().set_index(col).to_dict()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.596972Z","iopub.execute_input":"2022-07-24T12:52:29.597686Z","iopub.status.idle":"2022-07-24T12:52:29.609720Z","shell.execute_reply.started":"2022-07-24T12:52:29.597641Z","shell.execute_reply":"2022-07-24T12:52:29.608416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"di = train[[col,col + ENCODED]].drop_duplicates().set_index(col).to_dict()[col + ENCODED]\ndi","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.610978Z","iopub.execute_input":"2022-07-24T12:52:29.611652Z","iopub.status.idle":"2022-07-24T12:52:29.622608Z","shell.execute_reply.started":"2022-07-24T12:52:29.611618Z","shell.execute_reply":"2022-07-24T12:52:29.621449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train= train.replace({col:di})\n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.634145Z","iopub.execute_input":"2022-07-24T12:52:29.635758Z","iopub.status.idle":"2022-07-24T12:52:29.655330Z","shell.execute_reply.started":"2022-07-24T12:52:29.635710Z","shell.execute_reply":"2022-07-24T12:52:29.654546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop([col + ENCODED],axis=1)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-24T12:52:29.674584Z","iopub.execute_input":"2022-07-24T12:52:29.675158Z","iopub.status.idle":"2022-07-24T12:52:29.693769Z","shell.execute_reply.started":"2022-07-24T12:52:29.675125Z","shell.execute_reply":"2022-07-24T12:52:29.692893Z"},"trusted":true},"execution_count":null,"outputs":[]}]}