<jupyter_start><jupyter_text><jupyter_code>####code to  upload the data and resample the data (day wise)
from sklearn.externals import joblib
from keras.layers import Dense ,LSTM
from keras.models import Sequential 
from sklearn.metrics import mean_absolute_error,mean_squared_error
import pandas as pd
import numpy as np
from math import sqrt
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
from matplotlib import pyplot
from keras.layers import Dropout
from keras.layers import LeakyReLU
from keras.layers import LSTM
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers import Flatten
from sklearn.metrics import r2_score as r2
import matplotlib.pyplot as plt
import tensorflow as tf

# import r2
# import mane

import keras.backend as K

import os
from google.colab import drive
drive.mount('/content/drive',force_remount=True)
import keras.backend as K

def r2_score(y_true, y_pred):
    SS_res =  K.sum(K.square(y_true-y_pred))
    SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
    return (1 - SS_res/(SS_tot + K.epsilon()))
def return_data(final_data, i=0, steps=128, training_fraction=0.6):

    # training_fraction = float(input("Enter the fraction of data to be sent to training :"))
    length = len(final_data)
    train_size = int(training_fraction*length)
    
    # copy = final_data[:,i].reshape(-1,1)
    # sc = MinMaxScaler()
    # copy = sc.fit_transform(copy)
    
    #now we are going to divide into training and test set
    x_train = final_data[:train_size, i]
    y_train = final_data[:train_size, i]
    #x_train = x_train.reshape((-1,1,1))
    train_x = []
    train_y = []
    # print("enter n steps : ")
    n_steps = steps
    list_i = 0
    print("Entering loop")
    print(x_train.shape)
    print(y_train.shape)
    k=0
    while k<x_train.shape[0]-n_steps-1:
        train = x_train[k:k+n_steps]
        train_x.append([float(x) for x in train])
        train_y.append(y_train[k+n_steps])
        k= k+1
    
    train_x = np.asarray(train_x)
    train_y = np.asarray(train_y)    
    train_x = train_x.reshape((-1,n_steps,1))
    train_y = train_y.reshape((-1,1,1))
    
    x_test = final_data[train_size:, i]
    y_test = final_data[train_size:, i]
    #x_test = x_test.reshape((-1,1,1))
    test_x = []
    test_y = []
    k=0
    while k < x_test.shape[0]-n_steps-1:
        test= x_test[k:k+n_steps]
        test_x.append([x for x in test])
        test_y.append(y_test[k+n_steps])
        k = k+1
    test_x = np.asarray(test_x)
    test_y = np.asarray(test_y)
    
    test_x = test_x.reshape((-1,n_steps,1))
    test_y = test_y.reshape((-1,1,1))
    
    '''
    regressor = Sequential()
    regressor.add(LSTM(units = 10,activation = "relu",input_shape= (None,1)))
    regressor.add(Dense(units = 1))
    regressor.compile(optimizer = "adam",loss = "mean_squared_error",metrics = ["accuracy"])
    regressor.fit(train_x,train_y,epochs = 100 ,batch_size = 7,validation_split = 0.1)
    '''
    # print("Enter 1 if you want to process next equipment and 0 if not :")
    # decision = int(input())
    # if decision==1:
    #     pass
    # else:
    #     break

    return train_x, test_x, train_y, test_y
# equipment = int(input("Equipment: "))
# file_path = './drive/My Drive/ukdale_equip_'+str(equipment)+'_house1.pkl'
# data = joblib.load(file_path)
# data = np.array(data)
# final_data = data
# train_x, test_x, train_y, test_y = return_data(final_data)
# np.save(file='./drive/My Drive/ukdale_5.npy', arr=test_x)
# def load_data_all_eq(equipment):
#   if(equipment!=4 and equipment!=5):
#     file_path = './drive/My Drive/UkDale/ukdale_equip_'+str(equipment)+'_house1.pkl'
#     data = joblib.load(file_path)
#     data = np.array(data)
#     final_data = data
#     train_x, test_x, train_y, test_y = return_data(final_data)
#     np.save(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_x.npy', arr=train_x)
#     np.save(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_y.npy', arr=train_y)
#     np.save(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_tx.npy', arr=test_x)
#     np.save(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_ty.npy', arr=test_y)
#     print(equipment, " done")

# for i in range(14):
#   load_data_all_eq(i+1)
equipment = input("equipment: ")
train_x = np.load(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_x.npy')
train_y = np.load(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_y.npy')
test_x = np.load(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_tx.npy')
test_y = np.load(file='./drive/My Drive/UkDale/ukdale_'+str(equipment)+'_ty.npy')
print(train_x.shape, train_y.shape, test_x.shape, test_y.shape)
print(np.mean(train_x.reshape(-1)), np.mean(train_y.reshape(-1)))
print(np.mean(test_x.reshape(-1)), np.mean(test_y.reshape(-1)))<jupyter_output>2.6438399292263006 2.6419917913135795
5.671209476111833 5.671206039887863<jupyter_text>Ensemble:  Sub-Models:- Using 3 CNN_LSTMs<jupyter_code>def fit_cnn_lstm(x_train, y_train):
    
    n_timesteps, n_features = x_train.shape[1], x_train.shape[2]
    
    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,1)))
    model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
    model.add(MaxPooling1D(pool_size=2))
    model.add(Flatten())
    model.add(RepeatVector(1))
    model.add(LSTM(200, activation='relu', return_sequences=True))
    model.add(TimeDistributed(Dense(100, activation='relu')))
    model.add(TimeDistributed(Dense(1)))
    model.summary()
    model.compile(loss='mse', optimizer='adam',metrics = ["accuracy"])

    model.fit(x_train, y_train, epochs=10, batch_size=128, verbose = 1)
    
    return model

# os.makedirs('ensemble')

# fit and save models
n_members = 3
for i in range(n_members):
    model = fit_cnn_lstm(train_x, train_y)
    filename = './drive/My Drive/ukdale5_model_' + str(i + 1) + '.h5'
    model.save(filename)
    print('>>> Saved %s' % filename)
from keras.models import load_model

def load_all_models(n_models):
    all_models = list()
    for i in range(n_models):
        # define filename for this ensemble
        filename = './drive/My Drive/ukdale5_model_' + str(i + 1) + '.h5'
        # load model from file
        model = load_model(filename)
        # add to list of members
        all_models.append(model)
        print('>>> loaded %s' % filename)
    return all_models

n_members = 3
members = load_all_models(n_members)
print('Loaded %d models' % len(members))
#### Evaluating the 3 CNN_LSTMS Individually
for model in members:
    # testy_enc = to_categorical(testy)
    _, acc = model.evaluate(test_x, test_y, verbose=0)
    print('Model Accuracy: %.3f' % acc)
preds = []
for j in range(n_members):
  model_preds = []
  for i in range(1000):
      model_preds.append(members[j].predict(test_x[i].reshape(1,-1,1))[0][0][0])
  preds.append(model_preds)
plt.plot(test_y[:300].reshape(-1), c = 'orange')
list_colrs = ['red', 'blue', 'green']
for j in range(n_members):
  plt.plot(preds[j], c = list_colrs[j])
plt.show()<jupyter_output><empty_output><jupyter_text>Creating Dataset to go into XGBoost- Outputs from the 3 sub models flow into XGBoost<jupyter_code>def stacked_dataset(members, inputX):
    stackX = None
    for model in members:
        # make prediction
        yhat = model.predict(inputX, verbose=0)
        # stack predictions into [rows, members, probabilities]
        if stackX is None:
            stackX = yhat
        else:
            stackX = np.dstack((stackX, yhat))

    # flatten [rows, members x probabilities]
    stackX = stackX.reshape((stackX.shape[0], stackX.shape[1]*stackX.shape[2]))
    return stackX
from xgboost import XGBClassifier
import xgboost
def fit_stacked_model(members, inputX, inputy, n_est=45, alpha = 10):
    # create dataset using ensemble
    stackedX = stacked_dataset(members, inputX)
    # fit standalone model
    model = xgboost.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1, max_depth = 5, alpha = alpha, n_estimators = n_est)
    model.fit(stackedX, inputy)
    return model
model = fit_stacked_model(members, test_x, test_y)<jupyter_output>[15:21:18] WARNING: /workspace/src/objective/regression_obj.cu:152: reg:linear is now deprecated in favor of reg:squarederror.<jupyter_text>Compiling Ensemble Model<jupyter_code>def stacked_prediction(members, model, inputX):
    # create dataset using ensemble
    stackedX = stacked_dataset(members, inputX)
    # make a prediction
    yhat = model.predict(stackedX)
    return yhat<jupyter_output><empty_output><jupyter_text>Evaluating<jupyter_code># yhat = stacked_prediction(members, model, test_x)
# acc = r2_score(testy, yhat)
# print('--> Stacked Test Accuracy: %.3f' % acc)
# for i in range(1000):
#   print(test_x[i].reshape(1,-1,1).shape)
n_e_list = [15,20,25,30,35]           # number of estimators going into xgboost
for n_e in n_e_list:
  model = fit_stacked_model(members, test_x, test_y, n_e)  
  preds = stacked_prediction(members, model, test_x[:1000])
  plt.plot(preds)
  plt.plot(test_y[:1000].reshape(-1))
  plt.show()
model = fit_stacked_model(members, test_x, test_y, 40)  
preds = stacked_prediction(members, model, test_x[:1000])
plt.plot(preds)
plt.plot(test_y[:1000].reshape(-1))
plt.show()
model = fit_stacked_model(members, test_x, test_y, 50)  
preds = stacked_prediction(members, model, test_x[:1000])
plt.plot(preds)
plt.plot(test_y[:1000].reshape(-1))
plt.show()
model = fit_stacked_model(members, test_x, test_y, 60)  
preds = stacked_prediction(members, model, test_x[:1000])
plt.plot(preds)
plt.plot(test_y[:1000].reshape(-1))
plt.show()
model = fit_stacked_model(members, test_x, test_y, 60)  
preds = stacked_prediction(members, model, test_x[:200])
plt.plot(preds)
plt.plot(test_y[:200].reshape(-1))
plt.show()
# model = fit_stacked_model(members, test_x, test_y, 60)  
preds = stacked_prediction(members, model, test_x[:200])
plt.plot(preds)
plt.plot(test_y[:200].reshape(-1))
plt.show()
model = fit_stacked_model(members, test_x, test_y, 100, 20)  
t1 = time.time()
e_preds = stacked_prediction(members, model, test_x[150:200])
t2 = time.time()
for i in range(50):
  members[0].predict(test_x[150+i].reshape(1,-1,1))[0][0][0]
t3 = time.time()
print("Time taken by Ensemble to test 50 samples: ", t2-t1, " seconds")
print("Time taken by CNN-LSTM to test 50 samples: ", t3-t2, " seconds")
print("Time taken by ARIMA to test 50 samples: ", 0.10671567916870117, " seconds")
preds = []
for j in range(n_members):
  model_preds = []
  for i in range(100):
      model_preds.append(members[j].predict(test_x[800+i].reshape(1,-1,1))[0][0][0])
  preds.append(model_preds)
e_preds = stacked_prediction(members, model, test_x[800:900])
preds.append(e_preds)

plt.plot(test_y[800:900].reshape(-1), c = 'orange')
list_colrs = ['red', 'blue', 'green', 'black']
labels = ['cnn-lstm 1', 'cnn-lstm 2', 'cnn-lstm 3', 'xgboost ensemble']
for j in range(n_members+1):
  plt.plot(preds[j], c = list_colrs[j], label=labels[j])
  plt.legend()
plt.show()
arima_preds = np.load('./drive/My Drive/arima_preds_200.npy')

preds = []
lstm_preds = []
for i in range(200):
  lstm_preds.append(members[0].predict(test_x[i].reshape(1,-1,1))[0][0][0])
e_preds = stacked_prediction(members, model, test_x[:200])
preds.append(lstm_preds)
preds.append(e_preds)
preds.append(arima_preds)
print(len(preds))

plt.plot(test_y[:200].reshape(-1), c = 'orange', label='actual')
list_colrs = ['red', 'black', 'blue']
labels = ['cnn-lstm', 'xgboost ensemble', 'ARIMA']
for j in range(3):
  plt.plot(preds[j], c = list_colrs[j], label=labels[j])
  plt.legend()
plt.show()
import time
time.time()
arima_preds = np.load('./drive/My Drive/arima_preds_200.npy')

preds = []
lstm_preds = []
for i in range(50):
  lstm_preds.append(members[0].predict(test_x[i].reshape(1,-1,1))[0][0][0])
e_preds = stacked_prediction(members, model, test_x[:50])
preds.append(lstm_preds)
preds.append(e_preds)
preds.append(arima_preds[:50])
print(len(preds))

plt.plot(test_y[:50].reshape(-1), c = 'orange', label='actual')
list_colrs = ['red', 'black', 'blue']
labels = ['cnn-lstm', 'xgboost ensemble', 'ARIMA']
for j in range(3):
  plt.plot(preds[j], c = list_colrs[j], label=labels[j])
  plt.legend()
plt.show()
arima_preds = np.load('./drive/My Drive/arima_preds_1000_eq3.npy')
def make_comparison_graph(n_test, idx=0):
    preds = []
    lstm_preds = []
    for i in range(n_test):
      lstm_preds.append(members[0].predict(test_x[i].reshape(1,-1,1))[0][0][0])
    e_preds = stacked_prediction(members, model, test_x[idx:idx+n_test])
    preds.append(lstm_preds)
    preds.append(e_preds)
    preds.append(arima_preds[idx:idx+n_test])
    print(len(preds))

    plt.plot(test_y[idx:idx+n_test].reshape(-1), c = 'orange', label='actual')
    list_colrs = ['red', 'black', 'blue']
    labels = ['cnn-lstm', 'xgboost ensemble', 'ARIMA']
    for j in range(3):
      plt.plot(preds[j], c = list_colrs[j], label=labels[j])
    plt.legend()
    plt.show()
make_comparison_graph(1000, idx=0)
for i in range(0, 100, 10):
  make_comparison_graph(10, 300)
arima_preds = np.load('./drive/My Drive/arima_preds_1000.npy')

preds = []
lstm_preds = []
for i in range(1000):
  lstm_preds.append(members[0].predict(test_x[i].reshape(1,-1,1))[0][0][0])
e_preds = stacked_prediction(members, model, test_x[:1000])
preds.append(lstm_preds)
preds.append(e_preds)
preds.append(arima_preds)
print(len(preds))

plt.plot(test_y[:1000].reshape(-1), c = 'orange', label='actual')
list_colrs = ['red', 'black', 'blue']
labels = ['cnn-lstm', 'xgboost ensemble', 'ARIMA']
for j in range(3):
  plt.plot(preds[j], c = list_colrs[j], label=labels[j])
  plt.legend()
plt.show()
import pickle
pickle.dump(model, open("ensemble_weights.dat", "wb"))
# model = Sequential()
# model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(28,28)))
# model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
# model.add(MaxPooling1D(pool_size=2))
# model.add(Flatten())
# model.add(RepeatVector(1))
# model.add(LSTM(200, activation='relu', return_sequences=True))
# model.add(TimeDistributed(Dense(100, activation='relu')))
# model.add(TimeDistributed(Dense(1)))
# model.compile(loss= mane_loss, optimizer='adam',metrics = ["accuracy", r2])
# # fit network
# history = model.fit(x_train, y_train, epochs=30, batch_size=5, validation_split = 0.1)

# pyplot.plot(history.history['loss'])
# pyplot.plot(history.history['val_loss'])
# pyplot.title('model train vs validation loss')
# pyplot.ylabel('loss')
# pyplot.xlabel('epoch')
# pyplot.legend(['train', 'validation'], loc='upper right')
# pyplot.show()
    y_pred = model.predict(test_x)
    y_pred = y_pred.reshape((-1,1))
    test_y  = test_y.reshape((-1,1))
    mse = mean_squared_error(test_y,y_pred)
    rms = sqrt(mse)
    mae = mean_absolute_error(test_y,y_pred)
    print("mean_squared_error : ",mse)
    print("root_mean_squared_error : ",rms)
    print("mean_absolute_error : ",mae)
    #errors_saver["errors_equipment{}".format(i+1)] = {"mean_squared_error":mse,"mean_absolute_error":mae,"root_mean_squared_error":rms}
    y_pred = sc.inverse_transform(y_pred)
    test_y = sc.inverse_transform(test_y)


    l=0
    while l<len(test_y)-7:
        plt.scatter([1,2,3,4,5,6,7],y_pred[l:l+7,:],color  = "cyan",label = "predicted_day")
        #plt.savefig("equipment2_day(prediction){}".format(i//288))
        #plt.legend()
        #plt.show()
        plt.scatter([1,2,3,4,5,6,7],test_y[l:l+7,:],color = "blue",label = "real_values")
        plt.legend()
        plt.xlabel("day")
        plt.ylabel("power_consumption")
        #plt.savefig("equipment9_day{}.png".format(i//24))
        plt.show()
        l = l+7<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1591194968553459, 'fraction_numerical': 0.0330188679245283, 'mean_word_length': 4.650675195451315, 'pattern_counts': {'":': 3, '<': 22, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '440545', 'license': 'MIT', 'n_tokens_mistral': 6561, 'n_tokens_neox': 6172, 'n_words': 1121, 'path': 'Notebooks/Ensemble_Ukdale.ipynb', 'repo_name': 'Home-Electricity-Manager/Power-Forecasting', 'size': '15900'}