Commit 04ca7c33 authored by Muhammad Salman Shahid's avatar Muhammad Salman Shahid
Browse files

First commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.7 (disener)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (disener)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/AMPDs_energy_disaggregation.iml" filepath="$PROJECT_DIR$/.idea/AMPDs_energy_disaggregation.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PySciProjectComponent">
<option name="PY_MATPLOTLIB_IN_TOOLWINDOW" value="false" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
import numpy as np
from datetime import datetime
from preprocessing import AMPDs_dataframe
from methods import split_data, prediction_using_decision_trees, error_in_the_regression, dicts_n_mins, plot_subplots
######################################################################################################################
if __name__ == "__main__":
sampling1 = "1 minute"
print("Disaggreagtion on {}".format(sampling1))
# train_1min, val_1min, test_1min = split_data(dataframe=AMPDs_dataframe)
print(datetime.now().time())
train_1min, val_1min, test_1min = split_data(dataframe=AMPDs_dataframe, split_by_look_back=True)
appliances = list(train_1min.columns.values[1:])
min_samples_split = np.arange(2, 500, 10)
min_val_loss_1min, prediction_with_min_loss_1min = prediction_using_decision_trees(train_dataframe=train_1min,
val_dataframe=val_1min,
test_dataframe=test_1min,
appliances=appliances,
min_samples_split=min_samples_split,
sampling=sampling1)
mse_losses_1min, mae_losses_1min = error_in_the_regression(prediction_with_min_loss=prediction_with_min_loss_1min,
test=test_1min, appliances=appliances)
##########################################
sampling10 = "10 minute"
print("Disaggreagtion on {}".format(sampling10))
print(datetime.now().time())
train_10min = train_1min.resample('10T', closed='left').sum()
val_10min = val_1min.resample('10T', closed='left').sum()
test_10min = test_1min.resample('10T', closed='left').sum()
min_val_loss_10min, prediction_with_min_loss_10min = prediction_using_decision_trees(train_dataframe=train_10min,
val_dataframe=val_10min,
test_dataframe=test_10min,
appliances=appliances,
min_samples_split=min_samples_split,
sampling=sampling10)
mse_losses_10min, mae_losses_10min = error_in_the_regression(
prediction_with_min_loss=prediction_with_min_loss_10min,
test=test_10min, appliances=appliances)
##########################################
sampling30 = "30 minute"
print("Disaggreagtion on {}".format(sampling30))
print(datetime.now().time())
train_30min = train_1min.resample('30T', closed='left').sum()
val_30min = val_1min.resample('30T', closed='left').sum()
test_30min = test_1min.resample('30T', closed='left').sum()
min_val_loss_30min, prediction_with_min_loss_30min = prediction_using_decision_trees(train_dataframe=train_30min,
val_dataframe=val_30min,
test_dataframe=test_30min,
appliances=appliances,
min_samples_split=min_samples_split,
sampling=sampling30)
mse_losses_30min, mae_losses_30min = error_in_the_regression(
prediction_with_min_loss=prediction_with_min_loss_30min,
test=test_30min, appliances=appliances)
dict_1min = dicts_n_mins(test_dataframe=test_1min, prediction_dictionary=prediction_with_min_loss_1min,
appliances=appliances)
dict_10min = dicts_n_mins(test_dataframe=test_10min, prediction_dictionary=prediction_with_min_loss_10min,
appliances=appliances)
dict_30min = dicts_n_mins(test_dataframe=test_30min, prediction_dictionary=prediction_with_min_loss_30min,
appliances=appliances)
plot_subplots(dict_1min, dict_10min, dict_30min, appliances)
import time
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
def mse_loss(y_predict, y_test):
"""
:param y_predict:
:param y:
:return:
"""
return np.mean(np.square(y_predict - y_test))
def mae_loss(y_predict, y_test):
"""
:param y_predict:
:param y:
:return:
"""
return np.mean(np.abs(y_predict - y_test))
# using decision tree model on other appliances
def prediction_using_decision_trees(train_dataframe, val_dataframe, test_dataframe, appliances, min_samples_split,
sampling):
"""
:param train_dataframe:
:param val_dataframe:
:param test_dataframe:
:param sample_split:
:return:
"""
start = time.time()
prediction_with_min_loss = {}
min_val_loss = {}
for appliance in appliances:
print("Appliance: {}, Sampling: {}".format(appliance, sampling))
list_classifiers = []
losses = []
X_train = train_dataframe.loc[:, train_dataframe.columns[0]].values.reshape(-1, 1)
X_val = val_dataframe.loc[:, val_dataframe.columns[0]].values.reshape(-1, 1)
X_test = test_dataframe.loc[:, test_dataframe.columns[0]].values.reshape(-1, 1)
y_train = train_dataframe[appliance].values
y_val = val_dataframe[appliance].values
for split in min_samples_split:
# print("Appliance: {}, Split: {}".format(appliance, split))
classifier = DecisionTreeRegressor(min_samples_split=split)
classifier.fit(X_train, y_train)
y_predict_val = classifier.predict(X_val)
list_classifiers.append(classifier)
losses.append(mse_loss(y_predict_val, y_val))
min_loss_index = np.argmin(losses)
plot_losses(losses, min_samples_split, sampling, appliance)
print("Minimum mean square error loss for {} is {}".format(appliance, losses[min_loss_index]))
min_val_loss[appliance] = losses[min_loss_index]
prediction_with_min_loss[appliance] = list_classifiers[min_loss_index].predict(X_test)
print('Training time: ', (time.time() - start) / 60)
return min_val_loss, prediction_with_min_loss
def error_in_the_regression(prediction_with_min_loss, test, appliances):
"""
:param prediction_with_min_loss:
:param test:
:param appliances:
:return:
"""
mse_losses = {}
mae_losses = {}
for appliance in appliances:
mean_test_value = np.mean(test[appliance].values)
mse_losses[appliance] = mse_loss(prediction_with_min_loss[appliance], test[appliance].values)
mae_losses[appliance] = mae_loss(prediction_with_min_loss[appliance], test[appliance].values)
print('Appliance: {0}::::::: mean = {1:.2f} , mse = {2:.2f} - mae = {3:.2f}'.format(appliance, mean_test_value,
mse_losses[appliance],
mae_losses[appliance]))
return mse_losses, mae_losses
def plot_losses(losses, min_samples_split, sampling, appliance):
index = np.arange(len(min_samples_split))
bar_width = 0.4
opacity = 0.35
plt.bar(index, losses, bar_width, alpha=opacity, color='b')
plt.xlabel('min_samples_split', fontsize=30)
plt.ylabel('loss', fontsize=30)
plt.title('validation losses: sampling={}, appliance:{}'.format(sampling, appliance), fontsize='25')
plt.xticks(index + bar_width / 2, min_samples_split, fontsize=20)
plt.yticks(fontsize=20)
plt.rcParams["figure.figsize"] = [24, 15]
fig_name = "Decision Trees/Losses/MSE Loss - sampling - {}, appliance - {}.jpg".format(sampling, appliance)
plt.savefig(fig_name)
print(fig_name, "is saved.")
plt.close()
# plt.tight_layout()
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras.optimizers import Adam
from keras.regularizers import l2
def build_fc_model(layers):
fc_model = Sequential()
for i in range(len(layers)-1):
fc_model.add( Dense(input_dim=layers[i], output_dim= layers[i+1]) )#, W_regularizer=l2(0.1)) )
fc_model.add( Dropout(0.5) )
if i < (len(layers) - 2):
fc_model.add( Activation('relu') )
fc_model.summary()
return fc_model
import time
import numpy as np
from sklearn.ensemble import RandomForestRegressor
def mse_loss(y_predict, y_test):
"""
:param y_predict:
:param y:
:return:
"""
return np.mean(np.square(y_predict - y_test))
def mae_loss(y_predict, y_test):
"""
:param y_predict:
:param y:
:return:
"""
return np.mean(np.abs(y_predict - y_test))
# using decision tree model on other appliances
def prediction_using_random_forest(train_dataframe, val_dataframe, test_dataframe, appliances, sample_split=500):
"""
:param train_dataframe:
:param val_dataframe:
:param test_dataframe:
:param sample_split:
:return:
"""
start = time.time()
min_samples_split = np.arange(2, sample_split, 10)
prediction_with_min_loss = {}
min_val_loss = {}
for appliance in appliances:
list_classifiers = []
losses = []
X_train = train_dataframe.loc[:, train_dataframe.columns[0]].values.reshape(-1, 1)
X_val = val_dataframe.loc[:, val_dataframe.columns[0]].values.reshape(-1, 1)
X_test = test_dataframe.loc[:, test_dataframe.columns[0]].values.reshape(-1, 1)
y_train = train_dataframe[appliance].values
y_val = val_dataframe[appliance].values
for split in min_samples_split:
print("Appliance: {}, Split: {}".format(appliance, split))
classifier = RandomForestRegressor(min_samples_split=split)
classifier.fit(X_train, y_train)
y_predict_val = classifier.predict(X_val)
list_classifiers.append(classifier)
losses.append(mse_loss(y_predict_val, y_val))
min_loss_index = np.argmin(losses)
min_val_loss[appliance] = losses[min_loss_index]
prediction_with_min_loss[appliance] = list_classifiers[min_loss_index].predict(X_test)
print('Training time: ', (time.time() - start) / 60)
return min_val_loss, prediction_with_min_loss
def error_in_the_regression(prediction_with_min_loss, test, appliances):
"""
:param prediction_with_min_loss:
:param test:
:param appliances:
:return:
"""
mse_losses = {}
mae_losses = {}
for appliance in appliances:
mean_test_value = np.mean(test[appliance].values)
mse_losses[appliance] = mse_loss(prediction_with_min_loss[appliance], test[appliance].values)
mae_losses[appliance] = mae_loss(prediction_with_min_loss[appliance], test[appliance].values)
print('Appliance: {0}::::::: mean = {1:.2f} , mse = {2:.2f} - mae = {3:.2f}'.format(appliance, mean_test_value,
mse_losses[appliance],
mae_losses[appliance]))
return mse_losses, mae_losses
from .data_eng import *
from .frenchholidays import *
from .DT_method import *
from .RF_method import *
from .NN_method import *
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment