# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.6 with Spark
#     language: python3
#     name: python36
# ---

# # Importing the libraries

# +
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import matplotlib
import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

print('numpy', np.__version__)
print('pandas', pd.__version__)
print('matplotlib', matplotlib.__version__)
print('seaborn', sns.__version__)


# -

# # Importing the dataset

# +
def __iter__(self): return 0

# @hidden_cell
# The following code accesses a file in your IBM Cloud Object Storage. It includes your credentials.
# You might want to remove those credentials before you share the notebook.
client_62d16fde9ee64a49b809102844695402 = ibm_boto3.client(service_name='s3',
    ibm_api_key_id='<KEY>',
    ibm_auth_endpoint="https://iam.cloud.ibm.com/oidc/token",
    config=Config(signature_version='oauth'),
    endpoint_url='https://s3.eu-geo.objectstorage.service.networklayer.com')

body = client_62d16fde9ee64a49b809102844695402.get_object(Bucket='predictingtheenergyoutputofwindtu-donotdelete-pr-4zv0a0xlcw57fc',Key='Dataset.xlsx')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

dataset = pd.read_excel(body)
dataset.head()
# -

# Separating Date and Time
dataset['Date/Time'] = pd.to_datetime(dataset['Date/Time'],format='%d %m %Y %H:%M')
dataset['Hour'] = dataset['Date/Time'].dt.hour
dataset['Minute'] = dataset['Date/Time'].dt.minute
dataset['Day'] = dataset['Date/Time'].dt.day
dataset['Month'] = dataset['Date/Time'].dt.month
dataset['Year'] = dataset['Date/Time'].dt.year
dataset = dataset.reindex(columns = ['Date/Time', 'Minute', 'Hour', 'Day', 'Month', 'Year', 'Wind Speed (m/s)' ,'Theoretical_Power_Curve (KWh)',
                                     'Wind Direction', 'maxtempC', 'windGustKmph', 'DewPointC', 'windChillC', 'humidity', 'pressure',
                                     'LV ActivePower'])
dataset.head()

# ### Drop column Wind Speed (m/s), maxtempC, DewPointC	

# + active=""
# # Drop column 
# dataset = dataset.drop('Wind Speed (m/s)', axis=1)
# dataset = dataset.drop('maxtempC', axis=1)
# dataset = dataset.drop('DewPointC', axis=1)
# dataset.head()
# -

dataset.shape

# # Data Preprocessing

# Splitting the dataset into X and Y
X = dataset.iloc[:, 1:15].values
y = dataset.iloc[:, -1].values
print('Shape of X =', X.shape)
print('Shape of y =', y.shape)

# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = X[0:46083, :], X[46083:50531, :], y[0:46083], y[46083:50531]
print('Train set size is', y_train.size)
print('Test set size is', y_test.size)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

# # Finding suitable regression model

# Fitting Multiple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
linr = LinearRegression()
linr.fit(X_train, y_train)

# Fitting SVR to the dataset
from sklearn.svm import SVR
svr = SVR(kernel = 'rbf')
svr.fit(X_train, y_train)

# Fitting Decision Tree Regression to the dataset
from sklearn.tree import DecisionTreeRegressor
dtr = DecisionTreeRegressor(random_state = 0)
dtr.fit(X_train, y_train)

# Fitting Random Forest Regression to the dataset
from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor(n_estimators = 10, random_state = 0, n_jobs = -1)
rfr.fit(X_train, y_train)

# Fitting xgboost Regression to the dataset
import xgboost as xgb
xgbr = xgb.XGBRegressor(n_jobs= -1)
xgbr.fit(X_train, y_train)

# # Printing the Accuracies

regression = {0:'LinearRegression', 1:'SVR', 2:'DecisionTreeRegressor', 3:'RandomForestRegressor', 4:'xgboost'}
reg = {0:linr, 1:svr, 2:dtr, 3:rfr, 4:xgbr}
y_pred = {0:'y_lin', 1:'y_svr', 2:'y_dtr', 3:'y_rfr', 4:'y_xgb'}

for i in range(0, 5):
    print('\n\n-------------------------------------------------------------------------------')
    print('Type of regression model: {}'.format(regression[i]))
    # Predicting a new result
    y_pred[i] = reg[i].predict(X_test)
    # calculate MAE, MSE, RMSE and r2
    import sklearn.metrics as metrics
    print('Mean Absolute Error (MAE) = ',metrics.mean_absolute_error(y_test, y_pred[i]))
    print('Mean Squared Error (MSE) = ',metrics.mean_squared_error(y_test, y_pred[i]))
    print('Root Mean Squared Error (RMSE) = ',np.sqrt(metrics.mean_squared_error(y_test, y_pred[i])))
    print('R2 Score = ',metrics.r2_score(y_test, y_pred[i]))
    print('-------------------------------------------------------------------------------')

# # Most Accurate

# Applying k-Fold Cross Validation (xgboost)
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = xgbr, X = X_train, y = y_train, cv = 10)
print('Accuracies mean = ',accuracies.mean())
print('Accuracies standard deviation = ' ,accuracies.std())

# Applying k-Fold Cross Validation (RandomForestRegressor)
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = rfr, X = X_train, y = y_train, cv = 10)
print('Accuracies mean = ',accuracies.mean())
print('Accuracies standard deviation = ' ,accuracies.std())

# # Applying Grid Search to find the best parameters

xgbr.get_params()

# First Run
from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators': [80, 90, 100, 110, 120],
              'base_score': [0.3, 0.4, 0.5, 0.6, 0.7],
              'learning_rate': [0.05, 0.090, 0.095, 0.1, 0.105, 0.11, 0.2],
              'booster': ['gbtree', 'gblinear', 'dart']}]
grid_search = GridSearchCV(estimator = xgbr,
                           param_grid = parameters,
                           scoring = 'r2',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print(best_accuracy)
print(best_parameters)

# Second Run
from sklearn.model_selection import GridSearchCV
parameters = [{'n_estimators': [50, 60, 70, 80, 90],
              'base_score': [0.6],
              'learning_rate': [0.1],
              'booster': ['gbtree']}]
grid_search = GridSearchCV(estimator = xgbr,
                           param_grid = parameters,
                           scoring = 'r2',
                           cv = 10,
                           n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
print(best_accuracy)
print(best_parameters)

# Refitting xgboost Regression to the dataset
import xgboost as xgb
xgbr = xgb.XGBRegressor(base_score=0.6, booster='gbtree', n_estimators=60, n_jobs= -1)
xgbr.fit(X_train, y_train)

# calculate MAE, MSE, RMSE and r2
import sklearn.metrics as metrics
print('Mean Absolute Error (MAE) = ',metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error (MSE) = ',metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error (RMSE) = ',np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
print('R2 Score = ',metrics.r2_score(y_test, y_pred))

# Applying k-Fold Cross Validation
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = xgbr, X = X_train, y = y_train, cv = 10)
print('Accuracies mean = ',accuracies.mean())
print('Accuracies standard deviation = ' ,accuracies.std())

# Predicting a new result
y_pred = xgbr.predict(X_test)

# # Predciting Power from Custom User Input value

inputs = {0:'Minute', 1:'Hour', 2:'Day', 3:'Month', 4:'Year', 5:'Wind Speed (m/s)', 6:'Theoretical_Power_Curve (KWh)',
          7:'Wind Direction', 8:'maxtempC', 9:'windGustKmph', 10:'DewPointC', 11:'windChillC', 12:'humidity',
          13:'pressure'}

# Taking user input
a = []
for i in range(len(inputs)):
    print(inputs[i])
    a.append(input())

# Preprocesing the input
a = np.array(a)
a = a.astype('float64')
a = a.reshape(1, -1)

# prediction of result
result = xgbr.predict(a)
print('LV ActivePower',result)

# # Predicting and Visualization of Output

# ## One-month measurements in ten-minute intervals

# Plot y_pred v/s y_test)
b = dataset.iloc[46083:50530, 0:1].values
plt.figure(figsize=(20,5))
plt.plot(b, y_test, color='blue', label='Actual Value') 
plt.plot(b, y_pred, color='green', label='Predicted Value')
plt.title('Visualization y_pred v/s y_test')
plt.xlabel('Time')
plt.ylabel('LV ActivePower (kW)')
plt.show()

# ## One-week measurements in ten-minute intervals

# Plot y_pred v/s y_test)
b = dataset.iloc[46083:47091, 0:1].values
plt.figure(figsize=(20,5))
plt.plot(b, y_test[0:1008], color='blue', label='Actual Value') 
plt.plot(b, y_pred[0:1008], color='green', label='Predicted Value')
plt.title('Visualization y_pred v/s y_test')
plt.xlabel('Time')
plt.ylabel('LV ActivePower (kW)')
plt.show()

# ## One-day measurements in ten-minute intervals

# Plot y_pred v/s y_test)
b = dataset.iloc[46227:46371, 0:1].values
plt.figure(figsize=(20,5))
plt.plot(b, y_test[144:288], color='blue', label='Actual Value') 
plt.plot(b, y_pred[144:288], color='green', label='Predicted Value')
plt.title('Visualization y_pred v/s y_test')
plt.xlabel('Time')
plt.ylabel('LV ActivePower (kW)')
plt.show()

# # Suggest the best time to utilize the energy

# ## Over a Month

# ### Besrt Hour

# Hour
sum = [0]*24
sum = np.array(sum)
sum = sum.astype('float64')
k =[]
k = np.array(k)
d = -1
for i in range(30):
    for i in range(24):
        c = []
        c = np.array(c)
        temp = 0
        for i in range(6):
            d+=1
            temp+=y_test[d]
        c = np.append(c,values = temp)
    sum+=c

print('Best Hour to utilize the energy', sum.argmax())
print('The average production of Energy = ', max(sum)/30)
plt.bar(range(24), sum/30)
plt.show()

# ## Over a Year

# ### Day

# +
# Day
sum = [0]*30
sum = np.array(sum)
sum = sum.astype('float64')
k =[]
k = np.array(k)
d = -1

for i in range(11):
    c = []
    c = np.array(c)
    for i in range(30):
        temp = 0
        for i in range(144):
            d+=1
            temp+=y[d]
        c = np.append(c,values = temp)
    sum+=c
    
plt.bar(range(1, 31), sum/30)
plt.show()
# -

# ### Month

# Month
sum = []
sum = np.array(sum)
sum = sum.astype('float64')
k =[]
k = np.array(k)
d = -1
for i in range(12):
    temp = 0
    for i in range(4210):
        d+=1
        temp+=y[d]
    sum = np.append(sum, values = temp)

print('Best Month to utilize the energy', sum.argmax()+1)
print('The average production of Energy = ', max(sum)/4210,'each Minute')
plt.bar(range(1, 13), sum/4210)
plt.show()



StarcoderdataJupyterScriptsDedupFiltered