{"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":"from sklearn.preprocessing import PowerTransformer, QuantileTransformer\n\nimport pandas as pd\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nsns.set()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-07T09:35:33.411383Z","iopub.execute_input":"2022-08-07T09:35:33.411816Z","iopub.status.idle":"2022-08-07T09:35:33.608492Z","shell.execute_reply.started":"2022-08-07T09:35:33.411781Z","shell.execute_reply":"2022-08-07T09:35:33.607095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/titanic/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:03:53.018513Z","iopub.execute_input":"2022-08-07T10:03:53.019063Z","iopub.status.idle":"2022-08-07T10:03:53.032596Z","shell.execute_reply.started":"2022-08-07T10:03:53.019018Z","shell.execute_reply":"2022-08-07T10:03:53.031260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['Age', 'Fare']","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:03:53.324931Z","iopub.execute_input":"2022-08-07T10:03:53.325863Z","iopub.status.idle":"2022-08-07T10:03:53.331530Z","shell.execute_reply.started":"2022-08-07T10:03:53.325821Z","shell.execute_reply":"2022-08-07T10:03:53.329833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.dropna(subset=cols)\ndf = df[df['Fare']>0]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:03:53.425248Z","iopub.execute_input":"2022-08-07T10:03:53.426468Z","iopub.status.idle":"2022-08-07T10:03:53.436512Z","shell.execute_reply.started":"2022-08-07T10:03:53.426422Z","shell.execute_reply":"2022-08-07T10:03:53.435030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Original Data","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(20, 10))\naxes = axes.ravel()\n\nfor col, ax in zip(cols, axes):\n    sns.histplot(df[col], ax=ax)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:04:42.300236Z","iopub.execute_input":"2022-08-07T10:04:42.300747Z","iopub.status.idle":"2022-08-07T10:04:42.985013Z","shell.execute_reply.started":"2022-08-07T10:04:42.300708Z","shell.execute_reply":"2022-08-07T10:04:42.983356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PowerTransformer\n\n[sklearn.preprocessing.PowerTransformer — scikit-learn 1.1.2 documentation](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PowerTransformer.html#sklearn.preprocessing.PowerTransformer)","metadata":{}},{"cell_type":"markdown","source":"Yeo-Johnson","metadata":{}},{"cell_type":"code","source":"yj_cols = ['Age_yj', 'Fare_yj']\npt = PowerTransformer(method='yeo-johnson')\npt.fit(df[cols].values)\ndf[yj_cols] = pt.transform(df[cols].values)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:04:03.868094Z","iopub.execute_input":"2022-08-07T10:04:03.868695Z","iopub.status.idle":"2022-08-07T10:04:03.892887Z","shell.execute_reply.started":"2022-08-07T10:04:03.868648Z","shell.execute_reply":"2022-08-07T10:04:03.891349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(20, 10))\naxes = axes.ravel()\n\nfor col, ax in zip(yj_cols, axes):\n    sns.histplot(df[col], ax=ax)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:04:04.390376Z","iopub.execute_input":"2022-08-07T10:04:04.390855Z","iopub.status.idle":"2022-08-07T10:04:04.923037Z","shell.execute_reply.started":"2022-08-07T10:04:04.390818Z","shell.execute_reply":"2022-08-07T10:04:04.921683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Box-Cox","metadata":{}},{"cell_type":"code","source":"bc_cols = ['Age_bc', 'Fare_bc']\npt = PowerTransformer(method='box-cox')\npt.fit(df[cols].values)\ndf[bc_cols] = pt.transform(df[cols].values)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:04:19.688311Z","iopub.execute_input":"2022-08-07T10:04:19.688775Z","iopub.status.idle":"2022-08-07T10:04:19.707333Z","shell.execute_reply.started":"2022-08-07T10:04:19.688740Z","shell.execute_reply":"2022-08-07T10:04:19.705837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(20, 10))\naxes = axes.ravel()\n\nfor col, ax in zip(bc_cols, axes):\n    sns.histplot(df[col], ax=ax)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:04:20.040934Z","iopub.execute_input":"2022-08-07T10:04:20.042274Z","iopub.status.idle":"2022-08-07T10:04:20.567936Z","shell.execute_reply.started":"2022-08-07T10:04:20.042223Z","shell.execute_reply":"2022-08-07T10:04:20.566337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# QuantileTransformer\n\n[sklearn.preprocessing.QuantileTransformer — scikit-learn 1.1.2 documentation](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.QuantileTransformer.html#sklearn.preprocessing.QuantileTransformer)","metadata":{}},{"cell_type":"code","source":"qt_cols = ['Age_qt', 'Fare_qt']\nqt =  QuantileTransformer(n_quantiles=100, output_distribution='normal', random_state=42)\nqt.fit(df[cols].values)\ndf[qt_cols] = qt.transform(df[cols].values)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:07:55.258854Z","iopub.execute_input":"2022-08-07T10:07:55.260408Z","iopub.status.idle":"2022-08-07T10:07:55.275552Z","shell.execute_reply.started":"2022-08-07T10:07:55.260342Z","shell.execute_reply":"2022-08-07T10:07:55.274499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(20, 10))\naxes = axes.ravel()\n\nfor col, ax in zip(qt_cols, axes):\n    sns.histplot(df[col], ax=ax)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:07:55.581478Z","iopub.execute_input":"2022-08-07T10:07:55.582739Z","iopub.status.idle":"2022-08-07T10:07:56.174147Z","shell.execute_reply.started":"2022-08-07T10:07:55.582628Z","shell.execute_reply":"2022-08-07T10:07:56.172981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}