{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3354,"databundleVersionId":44362,"sourceType":"competition"},{"sourceId":5298005,"sourceType":"datasetVersion","datasetId":3080391}],"dockerImageVersionId":30301,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-12T16:18:44.047521Z","iopub.execute_input":"2024-05-12T16:18:44.047962Z","iopub.status.idle":"2024-05-12T16:18:44.058202Z","shell.execute_reply.started":"2024-05-12T16:18:44.047929Z","shell.execute_reply":"2024-05-12T16:18:44.056549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from netCDF4 import Dataset\nfrom os import listdir\n# Lbraries to unzip compressed data folders\nimport tarfile\nimport zipfile\n# some libraries for lat-long cartesian product\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:44.474999Z","iopub.execute_input":"2024-05-12T16:18:44.476295Z","iopub.status.idle":"2024-05-12T16:18:44.483727Z","shell.execute_reply.started":"2024-05-12T16:18:44.47624Z","shell.execute_reply":"2024-05-12T16:18:44.482472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_csv = pd.read_csv('/kaggle/input/ams-2014-solar-energy-prediction-contest/train.csv')\ndf_train_csv.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:45.040194Z","iopub.execute_input":"2024-05-12T16:18:45.041716Z","iopub.status.idle":"2024-05-12T16:18:45.160672Z","shell.execute_reply.started":"2024-05-12T16:18:45.041671Z","shell.execute_reply":"2024-05-12T16:18:45.15923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfile_paths = [\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/gefs_test.zip\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/gefs_test.tar.gz\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/gefs_train.zip\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/gefs_elevations.nc\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/station_info.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/sampleSubmission.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/solar_training_199401_200712.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/train.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/test.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/gefs_train.tar.gz\"\n]\n\nfor file_path in file_paths:\n    file_name = os.path.basename(file_path)\n    file_size = os.path.getsize(file_path)\n    file_extension = os.path.splitext(file_path)[1]\n    file_stat = os.stat(file_path)\n    \n    print(file_name)\n    print(file_size)\n    print(file_extension)\n    print(file_stat)\n    print()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:45.363949Z","iopub.execute_input":"2024-05-12T16:18:45.364886Z","iopub.status.idle":"2024-05-12T16:18:45.384634Z","shell.execute_reply.started":"2024-05-12T16:18:45.364842Z","shell.execute_reply":"2024-05-12T16:18:45.383614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\nfile_paths = [\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/station_info.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/sampleSubmission.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/solar_training_199401_200712.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/train.csv\",\n    \"/kaggle/input/ams-2014-solar-energy-prediction-contest/test.csv\"\n]\n\nfor file_path in file_paths:\n    file_name = file_path.split('/')[-1]\n    print(f\"File Name: {file_name}\\n\")\n    \n\n    df = pd.read_csv(file_path)\n\n    print(\"DataFrame Info:\")\n    print(df.info())\n    print(\"\\n\")\n\n    print(\"DataFrame Head:\")\n    print(df.head())\n    print(\"\\n\" + \"=\"*50 + \"\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:45.828012Z","iopub.execute_input":"2024-05-12T16:18:45.828744Z","iopub.status.idle":"2024-05-12T16:18:46.242548Z","shell.execute_reply.started":"2024-05-12T16:18:45.828699Z","shell.execute_reply":"2024-05-12T16:18:46.241068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import listdir","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:46.259483Z","iopub.execute_input":"2024-05-12T16:18:46.259951Z","iopub.status.idle":"2024-05-12T16:18:46.265913Z","shell.execute_reply.started":"2024-05-12T16:18:46.259912Z","shell.execute_reply":"2024-05-12T16:18:46.264507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unzip_gefs(directory:str, dest_path:str='tarzip')->str:\n    file_train = tarfile.open(directory) \n    file_train.extractall(f'/kaggle/working/{dest_path}')\n    file_train.close()\n    return \"Unzipped files!\"","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:46.840065Z","iopub.execute_input":"2024-05-12T16:18:46.841498Z","iopub.status.idle":"2024-05-12T16:18:46.848853Z","shell.execute_reply.started":"2024-05-12T16:18:46.841441Z","shell.execute_reply":"2024-05-12T16:18:46.847444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unzip_gefs(directory='/kaggle/input/ams-2014-solar-energy-prediction-contest/gefs_train.tar.gz')","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:18:47.347912Z","iopub.execute_input":"2024-05-12T16:18:47.348511Z","iopub.status.idle":"2024-05-12T16:19:00.162972Z","shell.execute_reply.started":"2024-05-12T16:18:47.348461Z","shell.execute_reply":"2024-05-12T16:19:00.161131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"This function is responsible for processing the input netCDF data.\nIt iterates through each file (f) in the specified directory (data_dir).\nFor each file, it loads the netCDF dataset using the Dataset function from the netCDF4 library.\nIt extracts the last variable (nc_gefs) from the netCDF dataset.\nDepending on the agg_dims parameter, it aggregates the data along the specified dimensions (hours, ens_models, or a combination of both).\nIt then crops the aggregated data based on the specified cropping parameters (crop_gefs_x_start, crop_gefs_x_end, crop_gefs_y_start, crop_gefs_y_end).\nAfter cropping, it reshapes the data into a pandas DataFrame (df_gefs_).\nIt renames the columns of the DataFrame based on the file name (f) and column indices.\nFinally, it concatenates the DataFrame with the global DataFrame (df_gefs) if it's not the first iteration.","metadata":{}},{"cell_type":"code","source":"def pre_processing(data_dir='/kaggle/working/tarzip/train/'):\n    df_gefs = pd.DataFrame()\n\n    for n, f in enumerate(listdir(f'{data_dir}')):\n        nc_ = Dataset(f'{data_dir}{f}')\n        nc_gefs = list(nc_.variables.values())[-1]\n        nc_agg = np.mean(nc_gefs, axis=(1, 2))\n\n        df_gefs_ = pd.DataFrame(nc_agg.reshape(-1, nc_agg.shape[-1]))\n\n        prefix = f.split(\"latlon\")[0]\n        cols = list(df_gefs_.columns)\n        newcols = [prefix + str(c) for c in cols]\n        rename_cols = {cols[i]: newcols[i] for i in range(len(cols))}\n        df_gefs_ = df_gefs_.rename(rename_cols, axis=1)\n\n        if n == 0:\n            latlon = list(itertools.product(nc_['lat'][:][:].data, nc_['lon'][:][:].data))\n            df_nc_time = pd.DataFrame(nc_['intValidTime'][:][:].data)\n            date_latlon = list(itertools.product(df_nc_time[0].apply(lambda x: int(str(x)[:8])), latlon))\n            df_gefs = pd.DataFrame(date_latlon, columns=['date', 'coordinates'])\n            df_gefs = pd.concat([df_gefs, df_gefs_], axis=1)\n        else:\n            df_gefs = pd.concat([df_gefs, df_gefs_], axis=1)\n\n    return df_gefs\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:19:00.165483Z","iopub.execute_input":"2024-05-12T16:19:00.166417Z","iopub.status.idle":"2024-05-12T16:19:00.184765Z","shell.execute_reply.started":"2024-05-12T16:19:00.166349Z","shell.execute_reply":"2024-05-12T16:19:00.183466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# needed for min distance calculations\n## taken extensive help of a R code using same thing\n\nfrom haversine import haversine\ndef custom_haversine(coord1, coordDf):\n    \"\"\"\n    Calculate the great circle distance between two points \n    on the earth (specified in decimal degrees)\n    source: https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points\n    \"\"\"\n    l={}\n    for coord2, n_coord2 in list(zip(coordDf.coord, coordDf.normalized_coord)):\n        l[coord2] = haversine(coord1,n_coord2)\n    return l  ","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:19:00.186166Z","iopub.execute_input":"2024-05-12T16:19:00.186557Z","iopub.status.idle":"2024-05-12T16:19:00.238477Z","shell.execute_reply.started":"2024-05-12T16:19:00.186525Z","shell.execute_reply":"2024-05-12T16:19:00.236792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_min_distance_node(df_gefs):\n    df_gefs_loc = pd.DataFrame(df_gefs['coordinates'].unique(), columns=['coord'])\n    df_gefs_loc['normalized_coord'] = df_gefs_loc['coord'].apply(lambda x: (x[0], x[1] - 360))\n\n    df_mes = pd.read_csv(\"/kaggle/input/ams-2014-solar-energy-prediction-contest/station_info.csv\")\n    df_mes[\"coord\"] = list(zip(df_mes.nlat, df_mes.elon))\n    df_mes['new_distances'] = df_mes['coord'].apply(lambda x: custom_haversine(x, df_gefs_loc))\n    df_mes['min_dist_node'] = df_mes['new_distances'].apply(lambda x: list(x.keys())[list(x.values()).index(min(x.values()))])\n    return df_mes\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:19:00.241266Z","iopub.execute_input":"2024-05-12T16:19:00.241705Z","iopub.status.idle":"2024-05-12T16:19:00.251916Z","shell.execute_reply.started":"2024-05-12T16:19:00.241669Z","shell.execute_reply":"2024-05-12T16:19:00.250725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This function extracts data from a DataFrame (df_gefs) corresponding to a specific ensemble model (ens_model).used by later function\n","metadata":{}},{"cell_type":"code","source":"def get_one_ens(df_gefs, ens_model=0):\n\n    #ens = ['date', 'coordinates']\n    ens = [col for col in df_gefs.columns if col.split(\"_\")[-1]==f\"{ens_model}\" ]\n    ens.insert(0,'date')\n    ens.insert(1,'coordinates')\n    df_gefs_ens = df_gefs[ens]\n    return df_gefs_ens","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:19:00.25344Z","iopub.execute_input":"2024-05-12T16:19:00.254143Z","iopub.status.idle":"2024-05-12T16:19:00.267086Z","shell.execute_reply.started":"2024-05-12T16:19:00.254106Z","shell.execute_reply":"2024-05-12T16:19:00.265892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This function combines MES station information with GEFS ensemble data.\nIt merges the training data with MES station information based on station IDs.\nIt creates a join key using the date and the GEFS point closest to each MES station.\nIt merges the combined DataFrame with the GEFS ensemble data based on the join key.\n","metadata":{}},{"cell_type":"code","source":"def combine_ens_gefs(df_mes, df_train_csv, df_gefs_ens):\n    mesonets = list(df_mes['stid'])\n    df_train = pd.melt(df_train_csv, id_vars='Date', value_vars=mesonets, var_name='stid', value_name='Daily_Production')\n    df_train = pd.merge(df_train, df_mes[['stid', 'new_distances', 'min_dist_node']], on='stid', how='left')\n    df_train['join'] = df_train['Date'].astype(str) + df_train['min_dist_node'].astype(str)\n    df_gefs_ens['join'] = df_gefs_ens['date'].astype(str) + df_gefs_ens['coordinates'].astype(str)\n    df_train_merged = pd.merge(df_train, df_gefs_ens, how='left', on='join')\n    return df_train_merged\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:19:00.268825Z","iopub.execute_input":"2024-05-12T16:19:00.270118Z","iopub.status.idle":"2024-05-12T16:19:00.280116Z","shell.execute_reply.started":"2024-05-12T16:19:00.27007Z","shell.execute_reply":"2024-05-12T16:19:00.279264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_gefs = pre_processing(data_dir='/kaggle/working/tarzip/train/')\nprint(df_gefs.head())\ndf_mes = get_min_distance_node(df_gefs)\ndf_gefs_ens = get_one_ens(df_gefs)\ndf_train_csv = pd.read_csv('/kaggle/input/ams-2014-solar-energy-prediction-contest/train.csv')\ndf_train_merged = combine_ens_gefs(df_mes, df_train_csv, df_gefs_ens)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T16:19:00.281522Z","iopub.execute_input":"2024-05-12T16:19:00.282879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_merged.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nplt.figure(figsize=(18, 12))\nfor i, col in enumerate(['tcolc_eatm_0', 'ulwrf_tatm_0', 'dlwrf_sfc_0', 'tmp_sfc_0', \n                         'tcdc_eatm_0', 'dswrf_sfc_0', 'tmax_2m_0', 'tmin_2m_0', \n                         'pwat_eatm_0', 'uswrf_sfc_0', 'spfh_2m_0', 'ulwrf_sfc_0', \n                         'tmp_2m_0', 'apcp_sfc_0', 'pres_msl_0']):\n    plt.subplot(4, 4, i+1)\n    sns.histplot(df_train_merged[col], kde=True)\n    plt.title(f'Distribution of {col}')\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14, 10))\nsns.heatmap(df_train_merged[['tcolc_eatm_0', 'ulwrf_tatm_0', 'dlwrf_sfc_0', 'tmp_sfc_0', \n                             'tcdc_eatm_0', 'dswrf_sfc_0', 'tmax_2m_0', 'tmin_2m_0', \n                             'pwat_eatm_0', 'uswrf_sfc_0', 'spfh_2m_0', 'ulwrf_sfc_0', \n                             'tmp_2m_0', 'apcp_sfc_0', 'pres_msl_0', 'Daily_Production']].corr(), \n            annot=True, cmap='coolwarm')\nplt.title('Correlation Heatmap')\nplt.show()\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 12))\nfor i, col in enumerate(['tcolc_eatm_0', 'ulwrf_tatm_0', 'dlwrf_sfc_0', 'tmp_sfc_0', \n                         'tcdc_eatm_0', 'dswrf_sfc_0', 'tmax_2m_0', 'tmin_2m_0', \n                         'pwat_eatm_0', 'uswrf_sfc_0', 'spfh_2m_0', 'ulwrf_sfc_0', \n                         'tmp_2m_0', 'apcp_sfc_0', 'pres_msl_0']):\n    plt.subplot(4, 4, i+1)\n    sns.scatterplot(x=col, y='Daily_Production', data=df_train_merged)\n    plt.title(f'Scatter Plot of {col} vs. Daily_Production')\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nsns.scatterplot(x=df_train_merged['min_dist_node'].str[0],  \n                y=df_train_merged['min_dist_node'].str[1], \n                data=df_train_merged,\n                hue='Daily_Production')\nplt.title('Scatter Plot of Coordinates vs. Daily Production')\nplt.xlabel('Longitude')\nplt.ylabel('Latitude')\nplt.legend(title='Daily Production')\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nfrom sklearn.preprocessing import MinMaxScaler\nimport numpy as np\n\ndef principal_comp_analysis(data) :\n    pca_res = PCA(n_components=n_comp)\n    pcomp = pca_res.fit_transform(data_n)\n\n    pca_df = pd.DataFrame(data=pcomp,\n                          columns=[f'principal_component_{i}' for i in range(1, n_comp + 1)])\n\n    return pca_df\n\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nfrom sklearn.linear_model import Ridge, LinearRegression, Lasso, ElasticNet, SGDRegressor, PassiveAggressiveRegressor\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.svm import SVR\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout\nfrom keras.callbacks import EarlyStopping","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_merged = df_train_merged.dropna()\ntraining_set = df_train_merged[['tcolc_eatm_0', 'ulwrf_tatm_0', 'dlwrf_sfc_0', 'tmp_sfc_0', \\\n       'tcdc_eatm_0', 'dswrf_sfc_0', 'tmax_2m_0', 'tmin_2m_0', 'pwat_eatm_0', \\\n       'uswrf_sfc_0', 'spfh_2m_0', 'ulwrf_sfc_0', 'tmp_2m_0', 'apcp_sfc_0', 'pres_msl_0']]\n\ny = df_train_merged['Daily_Production']\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntraining_pca = principal_comp_analysis(training_set)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_pca = principal_comp_analysis(training_set)\n\nX_train_pca, X_temp_test_pca, y_train_pca, y_temp_test_pca = train_test_split(training_pca, y, test_size=0.20, random_state=0)\nX_test_pca, X_test_pca_1, y_test_pca, y_test_pca_1 = train_test_split(X_temp_test_pca, y_temp_test_pca, test_size=0.50, random_state=0)\n\nprint(\"Training Set:\")\nprint(\"X_train_pca shape:\", X_train_pca.shape)\nprint(\"y_train_pca shape:\", y_train_pca.shape)\nprint(\"\\nValidation Set:\")\nprint(\"X_val_pca shape:\", X_test_pca.shape)\nprint(\"y_val_pca shape:\", y_test_pca.shape)\nprint(\"\\nFinal Test Set:\")\nprint(\"X_test_pca_1 shape:\", X_test_pca_1.shape)\nprint(\"y_test_pca_1 shape:\", y_test_pca_1.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel_lr = LinearRegression()\nmodel_lr.fit(X_train_pca, y_train_pca)\npreds_lr = model_lr.predict(X_test_pca)\nmae = metrics.mean_absolute_error(y_test_pca,preds_lr)\nprint(f\"MAE: {mae}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel_rf = RandomForestRegressor()\nmodel_rf.fit(X_train_pca, y_train_pca)\npreds_rf = model_rf.predict(X_test_pca)\nmae = metrics.mean_absolute_error(y_test_pca,preds_rf)\nprint(f\"MAE: {mae}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel_ridge = Ridge()\nmodel_ridge.fit(X_train_pca, y_train_pca)\npreds_ridge = model_ridge.predict(X_test_pca)\nmae = metrics.mean_absolute_error(y_test_pca,preds_ridge)\nprint(f\"MAE: {mae}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel_dtr = DecisionTreeRegressor(max_depth=5)\nmodel_dtr.fit(X_train_pca, y_train_pca)\npreds_dtr = model_dtr.predict(X_test_pca)\nmae = metrics.mean_absolute_error(y_test_pca,preds_dtr)\nprint(f\"MAE: {mae}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ANN Model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfrom tensorflow.keras.layers import InputLayer\n\nmodel_ann = Sequential()\nmodel_ann.add(InputLayer(input_shape=(X_train_pca.shape[1],)))\nmodel_ann.add(Dense(50, activation='relu'))\nmodel_ann.add(Dense(25, activation='relu'))\nmodel_ann.add(Dense(1, activation='linear'))\nmodel_ann.summary() \nmodel_ann.compile(optimizer='adam', loss='mae', metrics=['mse'])\n\nes = EarlyStopping(monitor='loss',\n                   mode='min',\n                   patience=50,\n                   restore_best_weights = True)\n\nhistory = model_ann.fit(X_train_pca, y_train_pca,\n                    callbacks=[es],\n                    epochs=100,\n                    batch_size=50,\n                    verbose=1)\n\ntest_loss, test_acc = model_ann.evaluate(X_test_pca, y_test_pca)\nprint(\"Test loss:\", test_loss)\nprint(\"Test accuracy:\", test_acc)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-12T13:21:00.825875Z","iopub.status.idle":"2024-05-12T13:21:00.826397Z","shell.execute_reply.started":"2024-05-12T13:21:00.826176Z","shell.execute_reply":"2024-05-12T13:21:00.826198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}