{"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":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n%matplotlib inline\nplt.rcParams[\"figure.figsize\"] = (14, 8)\nsns.set_theme(context=\"notebook\", style=\"whitegrid\")","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:32:16.335868Z","iopub.execute_input":"2022-07-09T18:32:16.336324Z","iopub.status.idle":"2022-07-09T18:32:16.91388Z","shell.execute_reply.started":"2022-07-09T18:32:16.336217Z","shell.execute_reply":"2022-07-09T18:32:16.912877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"# file paths\nDATA_DIR = Path(\"../input/tabular-playground-series-aug-2021\")\n\n# data\nTRAIN_DATA = DATA_DIR / \"train.csv\"\n\n# columns in the data\nINDEX_COL = \"id\"\n\nTARGET_COL = \"loss\"\n\n# random state\nRANDOM_STATE = 42","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:32:16.915963Z","iopub.execute_input":"2022-07-09T18:32:16.916356Z","iopub.status.idle":"2022-07-09T18:32:16.921077Z","shell.execute_reply.started":"2022-07-09T18:32:16.916314Z","shell.execute_reply":"2022-07-09T18:32:16.920236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(TRAIN_DATA, index_col=INDEX_COL)\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:32:16.922643Z","iopub.execute_input":"2022-07-09T18:32:16.922939Z","iopub.status.idle":"2022-07-09T18:32:21.709476Z","shell.execute_reply.started":"2022-07-09T18:32:16.922898Z","shell.execute_reply":"2022-07-09T18:32:21.708459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pandas profiling report","metadata":{}},{"cell_type":"code","source":"from pandas_profiling import ProfileReport\n\nprofile = ProfileReport(df, minimal=True)\nprofile","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:32:21.710845Z","iopub.execute_input":"2022-07-09T18:32:21.71115Z","iopub.status.idle":"2022-07-09T18:33:27.206666Z","shell.execute_reply.started":"2022-07-09T18:32:21.711111Z","shell.execute_reply":"2022-07-09T18:33:27.205476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Separate features from target","metadata":{}},{"cell_type":"code","source":"y = df[TARGET_COL]\nX = df.drop(TARGET_COL, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:33:27.207962Z","iopub.execute_input":"2022-07-09T18:33:27.208239Z","iopub.status.idle":"2022-07-09T18:33:27.344603Z","shell.execute_reply.started":"2022-07-09T18:33:27.208211Z","shell.execute_reply":"2022-07-09T18:33:27.343398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of variables","metadata":{}},{"cell_type":"markdown","source":"### Numeric features by target","metadata":{}},{"cell_type":"code","source":"rows = np.ceil(X.shape[1] / 2).astype(int)\nfig, axes = plt.subplots(rows, 2, figsize=(14, 8 // 2 * rows))\nplt.tight_layout()\n\nfor i, col in enumerate(X.columns):\n    ax = axes[i // 2, i % 2]\n    ax.scatter(data=df, x=col, y=TARGET_COL, alpha=0.1)\n    ax.set_title(f\"Scatterplot of {col} by {TARGET_COL}\", y=0.92)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:33:27.346125Z","iopub.execute_input":"2022-07-09T18:33:27.346459Z","iopub.status.idle":"2022-07-09T18:34:31.605821Z","shell.execute_reply.started":"2022-07-09T18:33:27.346387Z","shell.execute_reply":"2022-07-09T18:34:31.603746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Mutual information","metadata":{}},{"cell_type":"code","source":"# sampling\ny_sample = y.sample(frac=0.2, random_state=RANDOM_STATE)\nX_sample = X[X.index.isin(y_sample.index)]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:34:31.608209Z","iopub.execute_input":"2022-07-09T18:34:31.608969Z","iopub.status.idle":"2022-07-09T18:34:31.700907Z","shell.execute_reply.started":"2022-07-09T18:34:31.608909Z","shell.execute_reply":"2022-07-09T18:34:31.699596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_selection import mutual_info_regression\n\nmi_scores = mutual_info_regression(X_sample, y_sample, random_state=RANDOM_STATE)\nmi_scores = pd.Series(mi_scores, name=\"MI Scores\", index=X.columns)\nmi_scores = mi_scores.sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:34:31.704175Z","iopub.execute_input":"2022-07-09T18:34:31.704716Z","iopub.status.idle":"2022-07-09T18:39:27.399832Z","shell.execute_reply.started":"2022-07-09T18:34:31.704656Z","shell.execute_reply":"2022-07-09T18:39:27.39884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(14, len(X.columns)//3))\n\nsns.barplot(x=mi_scores.values, y=mi_scores.index, ax=ax, color=\"tab:blue\")\nax.set_title(\"Mutual information scores\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T18:39:27.401551Z","iopub.execute_input":"2022-07-09T18:39:27.402142Z","iopub.status.idle":"2022-07-09T18:39:28.765246Z","shell.execute_reply.started":"2022-07-09T18:39:27.402094Z","shell.execute_reply":"2022-07-09T18:39:28.764061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}