{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-28T14:30:44.971500Z","iopub.execute_input":"2024-10-28T14:30:44.972011Z","iopub.status.idle":"2024-10-28T14:30:46.737977Z","shell.execute_reply.started":"2024-10-28T14:30:44.971964Z","shell.execute_reply":"2024-10-28T14:30:46.736366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. example data generation","metadata":{}},{"cell_type":"code","source":"# generate data\nnp.random.seed(0)\nX = np.linspace(-10, 10, 100)\ny_true = 3 * X + 5 + np.random.randn(100) * 5  # 真の関係式にノイズを加える\n\n# clip target you can change this part\ny_clipped = np.clip(y_true, -5, 5)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-28T14:31:16.521686Z","iopub.execute_input":"2024-10-28T14:31:16.523082Z","iopub.status.idle":"2024-10-28T14:31:16.531614Z","shell.execute_reply.started":"2024-10-28T14:31:16.523008Z","shell.execute_reply":"2024-10-28T14:31:16.529906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. training by linear regression without clipped data","metadata":{}},{"cell_type":"code","source":"# training with non-clipped data\nmodel_true = LinearRegression()\nmodel_true.fit(X.reshape(-1, 1), y_true)\ny_pred_true = model_true.predict(X.reshape(-1, 1))\n\n# calculate metric with non-clipped data\nr2_true = r2_score(y_true, y_pred_true)\nmae_true = mean_absolute_error(y_true, y_pred_true)\nmse_true = mean_squared_error(y_true, y_pred_true)\n\nprint(f\"{r2_true=}\")\nprint(f\"{mae_true=}\")\nprint(f\"{mse_true=}\")\n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-28T14:31:17.509028Z","iopub.execute_input":"2024-10-28T14:31:17.509540Z","iopub.status.idle":"2024-10-28T14:31:17.522691Z","shell.execute_reply.started":"2024-10-28T14:31:17.509495Z","shell.execute_reply":"2024-10-28T14:31:17.521331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. training by linear regression with clipped data","metadata":{}},{"cell_type":"code","source":"# training with clipped data\nmodel_clipped = LinearRegression()\nmodel_clipped.fit(X.reshape(-1, 1), y_clipped)\ny_pred_clipped = model_clipped.predict(X.reshape(-1, 1))\n\n\n# calculate metric with clipped data\nr2_clipped = r2_score(y_clipped, y_pred_clipped)\nmae_clipped = mean_absolute_error(y_clipped, y_pred_clipped)\nmse_clipped = mean_squared_error(y_clipped, y_pred_clipped)\n\nprint(f\"{r2_clipped=}\")\nprint(f\"{mae_clipped=}\")\nprint(f\"{mse_clipped=}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-10-28T14:31:18.854395Z","iopub.execute_input":"2024-10-28T14:31:18.854862Z","iopub.status.idle":"2024-10-28T14:31:18.867575Z","shell.execute_reply.started":"2024-10-28T14:31:18.854813Z","shell.execute_reply":"2024-10-28T14:31:18.866265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.figure(figsize=(12, 5))\n\n# data with non-clipped data\nplt.subplot(1, 2, 1)\nplt.scatter(X, y_true, label='target without clipping', color='blue')\nplt.plot(X, y_pred_true, label='preds', color='red')\nplt.title('data without clipping')\nplt.xlabel('X')\nplt.ylabel('y')\nplt.legend()\n\n# data with clipped data\nplt.subplot(1, 2, 2)\nplt.scatter(X, y_clipped, label='target with clipping', color='green')\nplt.plot(X, y_pred_clipped, label='preds', color='red')\nplt.title('data with clipping')\nplt.xlabel('X')\nplt.ylabel('y')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-28T14:31:19.597316Z","iopub.execute_input":"2024-10-28T14:31:19.597866Z","iopub.status.idle":"2024-10-28T14:31:20.422153Z","shell.execute_reply.started":"2024-10-28T14:31:19.597818Z","shell.execute_reply":"2024-10-28T14:31:20.420904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}