{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":56537,"databundleVersionId":8877088,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.callbacks import LearningRateScheduler, ReduceLROnPlateau\nfrom tensorflow.keras import backend as K\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport os\nimport polars as pl\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import r2_score\n# from kan import *\nimport torch\nfrom matplotlib import pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:26:07.49218Z","iopub.execute_input":"2024-07-02T15:26:07.493015Z","iopub.status.idle":"2024-07-02T15:26:26.767874Z","shell.execute_reply.started":"2024-07-02T15:26:07.492974Z","shell.execute_reply":"2024-07-02T15:26:26.76686Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\ntrain_df = pd.read_csv('/kaggle/input/leap-atmospheric-physics-ai-climsim/train.csv',chunksize=40000)\ntest_df = pd.read_csv('/kaggle/input/leap-atmospheric-physics-ai-climsim/test.csv')\nW = pd.read_csv(\"/kaggle/input/leap-atmospheric-physics-ai-climsim/sample_submission.csv\").iloc[0,:]\n# train_df = train_df.sample(n=train_df.shape[0]//10, seed=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:26:26.769773Z","iopub.execute_input":"2024-07-02T15:26:26.770401Z","iopub.status.idle":"2024-07-02T15:29:21.39716Z","shell.execute_reply.started":"2024-07-02T15:26:26.770369Z","shell.execute_reply":"2024-07-02T15:29:21.395922Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = next(train_df)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:29:21.398657Z","iopub.execute_input":"2024-07-02T15:29:21.399088Z","iopub.status.idle":"2024-07-02T15:29:36.792946Z","shell.execute_reply.started":"2024-07-02T15:29:21.39905Z","shell.execute_reply":"2024-07-02T15:29:36.791799Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:29:36.794398Z","iopub.execute_input":"2024-07-02T15:29:36.794838Z","iopub.status.idle":"2024-07-02T15:29:36.855858Z","shell.execute_reply.started":"2024-07-02T15:29:36.794799Z","shell.execute_reply":"2024-07-02T15:29:36.854814Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"FEAT_COLS = train_df.columns[1:557]\nTARGET_COLS = train_df.columns[557:]","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:29:36.858594Z","iopub.execute_input":"2024-07-02T15:29:36.858941Z","iopub.status.idle":"2024-07-02T15:29:36.864156Z","shell.execute_reply.started":"2024-07-02T15:29:36.858905Z","shell.execute_reply":"2024-07-02T15:29:36.862785Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler,scale\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.linear_model import SGDRegressor\nfrom sklearn.metrics import r2_score\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.ensemble import GradientBoostingRegressor","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:29:36.865415Z","iopub.execute_input":"2024-07-02T15:29:36.865719Z","iopub.status.idle":"2024-07-02T15:29:37.145827Z","shell.execute_reply.started":"2024-07-02T15:29:36.865695Z","shell.execute_reply":"2024-07-02T15:29:37.144644Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = train_df[FEAT_COLS].values\ny = train_df[TARGET_COLS].values\nX_test = test_df[FEAT_COLS].values\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:29:37.147222Z","iopub.execute_input":"2024-07-02T15:29:37.147646Z","iopub.status.idle":"2024-07-02T15:29:39.098121Z","shell.execute_reply.started":"2024-07-02T15:29:37.147611Z","shell.execute_reply":"2024-07-02T15:29:39.097039Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"min_std = 1e-24\n\ny_mean = y_train.mean(axis=0)\ny_std = np.maximum(y_train.std(axis=0), min_std)\ny_train = (y_train - y_mean) / y_std\n\nX_mean = X_train.mean(axis=0)\nX_std = np.maximum(X_train.std(axis=0), min_std)\nX_train = (X_train - X_mean) / X_std","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:29:39.100019Z","iopub.execute_input":"2024-07-02T15:29:39.100372Z","iopub.status.idle":"2024-07-02T15:29:39.504096Z","shell.execute_reply.started":"2024-07-02T15:29:39.100344Z","shell.execute_reply":"2024-07-02T15:29:39.502986Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"kf = KFold(n_splits=5, shuffle=True, random_state=42)\n\nfor fold, (train_idx, val_idx) in enumerate(kf.split(X_train, y_train)):\n    models = {}\n    models[fold]={}\n    r2_scores = 0\n    for i in range(len(TARGET_COLS)):\n        model = GradientBoostingRegressor()\n        print(TARGET_COLS[i],\" ==> start model training\")\n        model.fit(X_train[train_idx],y_train[train_idx,i])\n        print(TARGET_COLS[i],\" ==> finished model\")\n        models[fold][TARGET_COLS[i]] = model\n        val_pred = model.predict(X_train[val_idx])\n        r2 = r2_score(y_pred=val_pred,y_true=y_train[val_idx,i])\n        print(TARGET_COLS[i],\" ==> r2= \",r2)\n        r2_scores = r2_scores + r2\n    r2_scores = r2_scores/len(TARGET_COLS)\n    print(\"average r2 score is \",r2_scores)","metadata":{"execution":{"iopub.status.busy":"2024-07-02T15:40:13.468853Z","iopub.execute_input":"2024-07-02T15:40:13.46932Z"},"trusted":true},"outputs":[],"execution_count":null}]}