{"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":8015876,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.compose import ColumnTransformer\n\nfrom xgboost import XGBRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:25.457666Z","iopub.execute_input":"2024-05-08T08:35:25.458114Z","iopub.status.idle":"2024-05-08T08:35:25.465705Z","shell.execute_reply.started":"2024-05-08T08:35:25.458082Z","shell.execute_reply":"2024-05-08T08:35:25.464663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_rows = 1000\nN_columns_X = 100\nN_columns_y = 100","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:25.467398Z","iopub.execute_input":"2024-05-08T08:35:25.468512Z","iopub.status.idle":"2024-05-08T08:35:25.479988Z","shell.execute_reply.started":"2024-05-08T08:35:25.468478Z","shell.execute_reply":"2024-05-08T08:35:25.47864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/leap-atmospheric-physics-ai-climsim/train.csv\", nrows = N_rows)\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:25.481428Z","iopub.execute_input":"2024-05-08T08:35:25.481756Z","iopub.status.idle":"2024-05-08T08:35:25.862721Z","shell.execute_reply.started":"2024-05-08T08:35:25.481729Z","shell.execute_reply":"2024-05-08T08:35:25.861507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(\"/kaggle/input/leap-atmospheric-physics-ai-climsim/test.csv\", nrows = N_rows)\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:25.866059Z","iopub.execute_input":"2024-05-08T08:35:25.866615Z","iopub.status.idle":"2024-05-08T08:35:26.122478Z","shell.execute_reply.started":"2024-05-08T08:35:25.866573Z","shell.execute_reply":"2024-05-08T08:35:26.121166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.iloc[:,1:N_columns_X]\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.124071Z","iopub.execute_input":"2024-05-08T08:35:26.124432Z","iopub.status.idle":"2024-05-08T08:35:26.133905Z","shell.execute_reply.started":"2024-05-08T08:35:26.124394Z","shell.execute_reply":"2024-05-08T08:35:26.132716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = df_train.iloc[:, 557:557+N_columns_y]\ny_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.135489Z","iopub.execute_input":"2024-05-08T08:35:26.135815Z","iopub.status.idle":"2024-05-08T08:35:26.146112Z","shell.execute_reply.started":"2024-05-08T08:35:26.135788Z","shell.execute_reply":"2024-05-08T08:35:26.145173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.147698Z","iopub.execute_input":"2024-05-08T08:35:26.148152Z","iopub.status.idle":"2024-05-08T08:35:26.185175Z","shell.execute_reply.started":"2024-05-08T08:35:26.148121Z","shell.execute_reply":"2024-05-08T08:35:26.183525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test.iloc[:, 1:N_columns_X]\nX_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.186655Z","iopub.execute_input":"2024-05-08T08:35:26.187051Z","iopub.status.idle":"2024-05-08T08:35:26.195904Z","shell.execute_reply.started":"2024-05-08T08:35:26.18702Z","shell.execute_reply":"2024-05-08T08:35:26.195023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = df_test.iloc[:, 362:362+N_columns_y]\ny_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.200042Z","iopub.execute_input":"2024-05-08T08:35:26.201154Z","iopub.status.idle":"2024-05-08T08:35:26.209855Z","shell.execute_reply.started":"2024-05-08T08:35:26.201116Z","shell.execute_reply":"2024-05-08T08:35:26.208663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.212118Z","iopub.execute_input":"2024-05-08T08:35:26.212494Z","iopub.status.idle":"2024-05-08T08:35:26.251661Z","shell.execute_reply.started":"2024-05-08T08:35:26.212464Z","shell.execute_reply":"2024-05-08T08:35:26.25065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sanity","metadata":{}},{"cell_type":"code","source":"X_train.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.252979Z","iopub.execute_input":"2024-05-08T08:35:26.253509Z","iopub.status.idle":"2024-05-08T08:35:26.273799Z","shell.execute_reply.started":"2024-05-08T08:35:26.253472Z","shell.execute_reply":"2024-05-08T08:35:26.271949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Missing values","metadata":{}},{"cell_type":"code","source":"X_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.2756Z","iopub.execute_input":"2024-05-08T08:35:26.27602Z","iopub.status.idle":"2024-05-08T08:35:26.28757Z","shell.execute_reply.started":"2024-05-08T08:35:26.275978Z","shell.execute_reply":"2024-05-08T08:35:26.286562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().sum()/len(X_train)*100","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.289092Z","iopub.execute_input":"2024-05-08T08:35:26.289873Z","iopub.status.idle":"2024-05-08T08:35:26.301273Z","shell.execute_reply.started":"2024-05-08T08:35:26.28984Z","shell.execute_reply":"2024-05-08T08:35:26.300047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Duplicate values","metadata":{}},{"cell_type":"code","source":"X_train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.302888Z","iopub.execute_input":"2024-05-08T08:35:26.303526Z","iopub.status.idle":"2024-05-08T08:35:26.330422Z","shell.execute_reply.started":"2024-05-08T08:35:26.30349Z","shell.execute_reply":"2024-05-08T08:35:26.32902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Garbage values","metadata":{}},{"cell_type":"code","source":"for i in X_train.select_dtypes(include = \"object\").columns:\n    print(X_train[i].value_counts())\n    print(\"***\"*100)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.332158Z","iopub.execute_input":"2024-05-08T08:35:26.332576Z","iopub.status.idle":"2024-05-08T08:35:26.339776Z","shell.execute_reply.started":"2024-05-08T08:35:26.332541Z","shell.execute_reply":"2024-05-08T08:35:26.338177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.34166Z","iopub.execute_input":"2024-05-08T08:35:26.342055Z","iopub.status.idle":"2024-05-08T08:35:26.581053Z","shell.execute_reply.started":"2024-05-08T08:35:26.342011Z","shell.execute_reply":"2024-05-08T08:35:26.579739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe(include = \"object\")","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.582499Z","iopub.execute_input":"2024-05-08T08:35:26.582861Z","iopub.status.idle":"2024-05-08T08:35:26.596625Z","shell.execute_reply.started":"2024-05-08T08:35:26.582831Z","shell.execute_reply":"2024-05-08T08:35:26.595688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Histogram","metadata":{}},{"cell_type":"code","source":"\"\"\"for i in X_train.columns:\n    sns.histplot(data = X_train, x = i, color = \"red\")\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.597941Z","iopub.execute_input":"2024-05-08T08:35:26.598803Z","iopub.status.idle":"2024-05-08T08:35:26.604627Z","shell.execute_reply.started":"2024-05-08T08:35:26.598769Z","shell.execute_reply":"2024-05-08T08:35:26.603827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Outliers","metadata":{}},{"cell_type":"code","source":"\"\"\"for i in X_train.columns:\n    sns.boxplot(data = X_train, x = i, color = \"green\")\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.606067Z","iopub.execute_input":"2024-05-08T08:35:26.60668Z","iopub.status.idle":"2024-05-08T08:35:26.61657Z","shell.execute_reply.started":"2024-05-08T08:35:26.606649Z","shell.execute_reply":"2024-05-08T08:35:26.615741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Outliers treatment","metadata":{}},{"cell_type":"code","source":"\"\"\"def wisker(col):\n    q1, q3 = np.percentile(col, [25,75])\n    iqr = q3 - q1\n    lw = q1 - (1.5)*iqr\n    uw = q3 + (1.5)*iqr\n    return lw, uw\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.618141Z","iopub.execute_input":"2024-05-08T08:35:26.618653Z","iopub.status.idle":"2024-05-08T08:35:26.628618Z","shell.execute_reply.started":"2024-05-08T08:35:26.618625Z","shell.execute_reply":"2024-05-08T08:35:26.627369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"cols = X_train.columns\ncols\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.629872Z","iopub.execute_input":"2024-05-08T08:35:26.630837Z","iopub.status.idle":"2024-05-08T08:35:26.640639Z","shell.execute_reply.started":"2024-05-08T08:35:26.630804Z","shell.execute_reply":"2024-05-08T08:35:26.639407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"for i in cols:\n    lw, uw = wisker(X_train[i])\n    X_train[i] = np.where(X_train[i] < lw , lw, X_train[i])\n    X_train[i] = np.where(X_train[i] > uw , uw, X_train[i])\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.642495Z","iopub.execute_input":"2024-05-08T08:35:26.643121Z","iopub.status.idle":"2024-05-08T08:35:26.656065Z","shell.execute_reply.started":"2024-05-08T08:35:26.643087Z","shell.execute_reply":"2024-05-08T08:35:26.652685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"for i in cols:\n    sns.boxplot(data = X_train, x = i, color = \"green\")\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.657618Z","iopub.execute_input":"2024-05-08T08:35:26.658001Z","iopub.status.idle":"2024-05-08T08:35:26.669029Z","shell.execute_reply.started":"2024-05-08T08:35:26.657946Z","shell.execute_reply":"2024-05-08T08:35:26.667879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalization","metadata":{}},{"cell_type":"code","source":"col_pipeline = Pipeline([(\"std_scaler\", StandardScaler())])","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.675311Z","iopub.execute_input":"2024-05-08T08:35:26.675717Z","iopub.status.idle":"2024-05-08T08:35:26.680734Z","shell.execute_reply.started":"2024-05-08T08:35:26.675684Z","shell.execute_reply":"2024-05-08T08:35:26.679742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_attribs = list(X_train)\nfull_pipeline = ColumnTransformer([(\"sclr\", col_pipeline, num_attribs)])","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.682946Z","iopub.execute_input":"2024-05-08T08:35:26.683428Z","iopub.status.idle":"2024-05-08T08:35:26.691945Z","shell.execute_reply.started":"2024-05-08T08:35:26.683387Z","shell.execute_reply":"2024-05-08T08:35:26.690872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"DEJAMOS EL PIPELINE PARA DESPUES","metadata":{}},{"cell_type":"code","source":"scaler = StandardScaler()\nscaler.fit(X_train)\nX_train_scaled = scaler.transform(X_train)\nX_train_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.693303Z","iopub.execute_input":"2024-05-08T08:35:26.694258Z","iopub.status.idle":"2024-05-08T08:35:26.71513Z","shell.execute_reply.started":"2024-05-08T08:35:26.694224Z","shell.execute_reply":"2024-05-08T08:35:26.713536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_scaled = scaler.transform(X_test)\nX_test_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.716407Z","iopub.execute_input":"2024-05-08T08:35:26.717184Z","iopub.status.idle":"2024-05-08T08:35:26.729088Z","shell.execute_reply.started":"2024-05-08T08:35:26.717153Z","shell.execute_reply":"2024-05-08T08:35:26.727774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"MODEL","metadata":{}},{"cell_type":"code","source":"xgbr = XGBRegressor()\nxgbr.fit(X_train_scaled, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:26.730332Z","iopub.execute_input":"2024-05-08T08:35:26.731243Z","iopub.status.idle":"2024-05-08T08:35:34.670278Z","shell.execute_reply.started":"2024-05-08T08:35:26.731212Z","shell.execute_reply":"2024-05-08T08:35:34.669048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = xgbr.predict(X_test_scaled)\nprint(f'RMSE = {(mean_squared_error(y_test, preds)**0.5).round(3)}')","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:34.67177Z","iopub.execute_input":"2024-05-08T08:35:34.672821Z","iopub.status.idle":"2024-05-08T08:35:34.709014Z","shell.execute_reply.started":"2024-05-08T08:35:34.672779Z","shell.execute_reply":"2024-05-08T08:35:34.707729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mse = mean_squared_error(y_test, preds)\nrmse = np.sqrt(mse)\nrmse","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:34.710847Z","iopub.execute_input":"2024-05-08T08:35:34.711215Z","iopub.status.idle":"2024-05-08T08:35:34.72303Z","shell.execute_reply.started":"2024-05-08T08:35:34.711184Z","shell.execute_reply":"2024-05-08T08:35:34.721906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Model 2","metadata":{}},{"cell_type":"code","source":"tree_reg = DecisionTreeRegressor()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:34.724748Z","iopub.execute_input":"2024-05-08T08:35:34.725116Z","iopub.status.idle":"2024-05-08T08:35:34.731709Z","shell.execute_reply.started":"2024-05-08T08:35:34.725086Z","shell.execute_reply":"2024-05-08T08:35:34.730293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tree_reg.fit(X_train_scaled, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:34.733297Z","iopub.execute_input":"2024-05-08T08:35:34.733616Z","iopub.status.idle":"2024-05-08T08:35:35.198222Z","shell.execute_reply.started":"2024-05-08T08:35:34.73359Z","shell.execute_reply":"2024-05-08T08:35:35.196788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = tree_reg.predict(X_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:35.200335Z","iopub.execute_input":"2024-05-08T08:35:35.20079Z","iopub.status.idle":"2024-05-08T08:35:35.207933Z","shell.execute_reply.started":"2024-05-08T08:35:35.20075Z","shell.execute_reply":"2024-05-08T08:35:35.206546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tree_mse = mean_squared_error(y_test, preds)\ntree_rmse = np.sqrt(tree_mse)\ntree_rmse","metadata":{"execution":{"iopub.status.busy":"2024-05-08T08:35:35.209553Z","iopub.execute_input":"2024-05-08T08:35:35.210011Z","iopub.status.idle":"2024-05-08T08:35:35.227614Z","shell.execute_reply.started":"2024-05-08T08:35:35.209948Z","shell.execute_reply":"2024-05-08T08:35:35.226013Z"},"trusted":true},"execution_count":null,"outputs":[]}]}