{"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":"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":"2023-03-26T19:20:32.250137Z","iopub.execute_input":"2023-03-26T19:20:32.250555Z","iopub.status.idle":"2023-03-26T19:20:32.260741Z","shell.execute_reply.started":"2023-03-26T19:20:32.250519Z","shell.execute_reply":"2023-03-26T19:20:32.259841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import pandas as pd\n#import os\n#dataf=pd.read_csv('/kaggle/input/ams-2014-solar-energy-prediction-contest/solar_training_199401_200712.csv')\n#dataf.head()\n\n#import sqllite as SQl\n#select * from sql.\n\n\n# -*- coding: utf-8 -*-\n\nimport pandas as pd\nimport numpy as np\nimport os\n\n# Reading the Train File\ndf1 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/train.csv')\n\ndf1.dtypes\n\n#Changing the format of the dates and checking the start and end Dates\ndf1['date'] = pd.to_datetime(df1['date'], format='%Y%m%d%H')\ndf1['date'].min()\ndf1['date'].max()\n\n# Records of each windfarm, and checking NAs\ndf1.info()\n\n#EDA of the train file\n#Univariate check\n\ndf1.describe(percentiles=[0.1,0.25,0.5,0.75,0.9,0.95,0.99]).T\n\n# Creating Master dataset function\ndef data_processing(df_wf1, y_col):\n    # Getting the forecast date for wind forecasts\n    df_wf1['date'] = pd.to_datetime(df_wf1['date'], format='%Y%m%d%H')\n    df_wf1['hors_delta'] = pd.to_timedelta(df_wf1.hors, unit='hours')\n    df_wf1['forecast_date'] = df_wf1['date']+ df_wf1['hors_delta']\n    df_wf1.drop(columns= 'hors_delta', inplace=True)\n    # MErging to get all independent and dependent features in 1 dataset\n    df_wf1a = df_wf1.merge(df1[['date',y_col]], how='left', left_on='forecast_date', right_on='date')\n    df_wf1a.rename(columns={y_col:'power'}, inplace=True)\n    df_wf1a.rename(columns={'date_x':'date'}, inplace=True)\n    df_wf1a.drop(columns='date_y', inplace=True)\n    \n    # Selecting only those records where complete forecasts are available - 4counts and averaging the power forecasts \n    df_wf1b = df_wf1a.groupby('forecast_date').agg({'power': 'mean',\n                                                    'hors':'count'}).reset_index()\n    df_wf1b.dropna(subset=['power'], inplace=True)\n    df_wf1b = df_wf1b[df_wf1b.hors==4]\n    df_wf1b.drop(columns='hors', inplace=True)\n\n    # Creating Average feature based on 2 latest rolling wind forecasts - to incorporate recent behavior only\n    df_wf1a['u_avg'] = df_wf1a.groupby('forecast_date').u.transform(lambda x: x.rolling(2, min_periods=1).mean())\n    df_wf1a['v_avg'] = df_wf1a.groupby('forecast_date').v.transform(lambda x: x.rolling(2, min_periods=1).mean())\n    df_wf1a['ws_avg'] = df_wf1a.groupby('forecast_date').ws.transform(lambda x: x.rolling(2, min_periods=1).mean())\n    df_wf1a['wd_avg'] = df_wf1a.groupby('forecast_date').wd.transform(lambda x: x.rolling(2, min_periods=1).mean())\n\n    df_wf1c = df_wf1a.groupby('forecast_date').last().reset_index()\n    df_wf1c.columns\n\n    # Other features reflecting the timing of the forecast date\n    df_wf1c['year'] = df_wf1c.forecast_date.dt.year\n    df_wf1c['month'] = df_wf1c.forecast_date.dt.month\n    df_wf1c['day'] = df_wf1c.forecast_date.dt.day\n    df_wf1c['hour'] = df_wf1c.forecast_date.dt.hour\n    df_wf1c['dayofweek'] = df_wf1c.forecast_date.dt.dayofweek\n    df_wf1c['weekday'] = np.where(df_wf1c.forecast_date.dt.dayofweek>4,0,1)\n    \n    #coverting degrees to radian for the model to understand\n    df_wf1c['wd_radian'] = df_wf1c['wd']*((22/7)/180)\n\n    df_wf1c.columns\n\n    df_wf1d = df_wf1b.merge(df_wf1c[['forecast_date','u_avg', 'v_avg', 'ws_avg', 'wd_avg', 'year', 'month', 'day',\n                                     'hour', 'dayofweek', 'weekday', 'wd_radian']],\n                            on='forecast_date', how='left')\n\n    return df_wf1d\n\n\n# Calling the function for creating master dataset for each wind farm\ndf_wf1 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf1.csv')\nfarm1 = data_processing(df_wf1,'wp1')\nfarm1['farm_no'] = 'farm1'\n\ndf_wf2 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf2.csv')\nfarm2 = data_processing(df_wf2,'wp2')\nfarm2['farm_no'] = 'farm2'\n\ndf_wf3 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf3.csv')\nfarm3 = data_processing(df_wf3,'wp3')\nfarm3['farm_no'] = 'farm3'\n\ndf_wf4 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf4.csv')\nfarm4 = data_processing(df_wf4,'wp4')\nfarm4['farm_no'] = 'farm4'\n\ndf_wf5 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf5.csv')\nfarm5 = data_processing(df_wf5,'wp5')\nfarm5['farm_no'] = 'farm5'\n\ndf_wf6 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf6.csv')\nfarm6 = data_processing(df_wf6,'wp6')\nfarm6['farm_no'] = 'farm6'\n\ndf_wf7 = pd.read_csv('/kaggle/input/GEF2012-wind-forecasting/windforecasts_wf7.csv')\nfarm7 = data_processing(df_wf7,'wp7')\nfarm7['farm_no'] = 'farm7'\n\n# Concating and using the farm no. as a categorical feature in the model\ndf = pd.concat([farm1, farm2, farm3, farm4, farm5, farm6, farm7])\n\nfrom sklearn.preprocessing import LabelEncoder\nlabel_encoder = LabelEncoder()\ndf['farm_no']= label_encoder.fit_transform(df['farm_no'])  \ndf['farm_no'].unique()\n\n# Independent and dependent features\ny = df[['power']]\nX = df.drop(columns=['power','forecast_date'])\n\n# Split train and test by 70-30%\nfrom sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(X,y,\n                                                    test_size=0.3,\n                                                    random_state=13,\n                                                    stratify=X.farm_no)\n\n# Using XGboost model for the predictions\n\nfrom xgboost import XGBRegressor\n\nxgb_r = XGBRegressor(alpha=0.02, learning_rate=0.1, max_depth=15, \n                     min_child_weight=3, n_estimators=100, \n                     subsample=0.8, tree_method='hist')\n\nxgb_r.fit(X_train, y_train)\n  \npred = xgb_r.predict(X_test)\n\nfrom sklearn.metrics import mean_squared_error, r2_score\nrmse = np.sqrt(mean_squared_error(y_test, pred))\nr2 = r2_score(y_test, pred)\n# 0.8549756085102491\n","metadata":{"execution":{"iopub.status.busy":"2023-03-26T19:20:32.262416Z","iopub.execute_input":"2023-03-26T19:20:32.262901Z"},"trusted":true},"execution_count":null,"outputs":[]}]}