本方法主要利用基于卷积神经网络的非侵入式负荷分解方法实现住宅设备的识别,输入数据为在设备运行时获得的瞬态功率信号数据。训练卷积神经网络使用数据为开源数据REDD(1Hz),具体实现原理请参考文献下载链接。只供学习参考,Python实现代码如下:

1 第一部分:数据可视化

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv("data.csv")
#print(data)n = int(data.max(axis = 0, skipna = True)[1]) + 1  # gets the number of readings
h = int(data.max(axis = 0, skipna = True)[0]) # gets the number of houseshouses = np.zeros((n,5,h))  # time, tv inst power, aggr power, filtered tv , on/offfor i in range(h):houses[:,0:3,i] =  data[data['House']==i+1  ].values[:,1:]   # data is now seperated by house# visulise data
plt.figure()
plt.suptitle('Tv power and agg power seperated ')
plt.subplot(211)
for i in range(h):plt.plot(houses[:,0,i],houses[:,1,i], label = 'House ' + str(i+1) )
plt.subplot(212)
for i in range(h):plt.plot(houses[:,0,i],houses[:,2,i], label = 'House ' + str(i+1) )
plt.legend()
# plt.show()plt.figure()
plt.suptitle('Normalised data, shown by house')
for i in range(h):plt.subplot(3,1,i+1)plt.plot(houses[:,0,i],houses[:,1,i]/np.average(houses[:,1,i]) )plt.plot(houses[:, 0, i], houses[:, 2, i]/np.average( houses[:, 2, i]) )
plt.legend()
plt.show()

实验结果:

过滤并标准化电视即时功率

from scipy.signal import *
houses[:,3,:] = savgol_filter(houses[:, 1, :] / np.average(houses[:, 1, :],axis=0), 11, 2,axis=0)
thres = 1.15  # the threshold for seperation
plt.figure()
plt.suptitle('Filtered inst power, and determined state')
for i in range(h):houses[ np.argwhere( houses[:,3,i] >= thres ) ,4,i  ] +=1   # makes the 4th row qual to 1 if the filtered result is higher then the thresholdplt.subplot(3, 1, i + 1)plt.plot(  houses[:, 0, i],houses[:, 3, i]  ) # plot filtered curveplt.plot(houses[:, 0, i], houses[:, 4, i]) # plot on stateplt.plot(houses[:, 0, i], houses[:, 1, i] / np.average(houses[:, 1, i])) # plot normalised curve
plt.show()

实验结果:

2 卷积神经网络实现负荷识别

#总功率问题
# - 存在非常大的峰
# - 其他电器出现的一些周期性峰值,这些在每个房子中的频率不同
# - 噪声/峰值平坦度与一号房的电视电源使用量相似
# 想法
# - 针对开/关状态测试/检查
# - 测试/检查状态改变的时间
# - 使用 CNN/ANN/MLP 进行分类
# - 输入应该是整个时间,还是 N 个时间步长的夹头
# - N 时间步长意味着它可以推广到不同的测试输入大小
# - 然后应该建立这 N 个大输入的训练集和测试集,每一张幻灯片
# - 允许在输入中打开状态的位置进行概括
# - 输出N 1/0(或开启的概率,然后是阈值)每个时间步对应的输出
import numpy as np
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.models import Sequential
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
def sep_data(houses, T=0.8):# seperate the house data into windowed sections winin each house# split into training (T%) and test set, no validation set is being used# record the class as a one hot vector of if the last state is on/offwindow_len = 20#at size 20 the classifier was classfiing the periodic sections as the tv being on in house one #filter sized 6,3 wights 1.8,1#at length 30 a similar thing happened, filters 6,3 weights at 3:1, not as accurate as ^# at 10, with 3,3, stride and 2.2:1 similar problemsntrain = int((n-window_len)*T) # amount of data from each house to train ontrain = np.zeros((ntrain*h,window_len,1,2))                         # [#windows x samples per window x 1(as only one input feature) x 2 (for rata, and weights)test = np.zeros( ( (n-window_len-ntrain)*h,window_len,1,2)  )for j in range(h): #each housefor i in range(n-window_len):train_index = i+j*ntrain ## which row in the reformatted array is being filledtest_index = i-ntrain + j*(n-window_len-ntrain)if i<ntrain:  #part of training settrain[train_index,:,:,0 ] =np.reshape(houses[i:i+window_len,2,j] , (window_len,1))     # window up the aggregated power#train[train_index, :,:,1] = np.reshape(houses[i:i + window_len,4, j], (window_len,1)) # no longer used, was used when every state in window was a classtrain[train_index, 0 , :, 1] = houses[i + window_len, 4, j]                            # identify state of last step in windowtrain[train_index, 1, :, 1] = -(train[train_index, 0 , :, 1] -1)                       # one hot encode the category,cat 0 = on, cat 1 = offelse: # test settest[test_index , :,:, 0] =np.reshape( houses[i:i + window_len, 2, j], (window_len,1))#test[test_index), :,:, 1] = np.reshape(houses[i:i + window_len, 4, j], (window_len,1))test[test_index, 0, :, 1] = houses[i + window_len, 4, j]test[test_index, 1, :, 1] = -(test[test_index, 0, :, 1] - 1)#return train[:,:,:,0],train[:,:,0,1],test[:,:,:,0],test[:,:,0,1]# how uneven is the data? this could be a problem when training, as a large group can overpower and always be predictedwratio = np.sum(train[:,1,:,1])/np.sum(train[:,0,:,1])print(wratio)return train[:,:,:,0],train[:,0:2,0,1],test[:,:,:,0],test[:,0:2,0,1]# 训练与评估CNN模型
def run_model(trainx, trainy, testx, testy):verbose, epochs, batch_size = 0, 10, 64n_timesteps, n_features, n_outputs = trainx.shape[1], trainx.shape[2], trainy.shape[1]# 使用 keras 构建卷积神经网络#卷积应该有希望识别窗口时间序列数据中的特征,以指示最终时间状态model = Sequential()model.add(Conv1D(filters=120, kernel_size=6, activation='sigmoid', input_shape=(n_timesteps, n_features)))model.add(Conv1D(filters=120, kernel_size=3, activation='sigmoid'))model.add(Dropout(0.5)) #这一层有助于规范化,并希望减少对训练集的过度拟合model.add(MaxPooling1D(pool_size=2))model.add(Flatten()) # 展平model.add(Dense(100, activation='sigmoid')) #全连接层model.add(Dense(n_outputs, activation='softmax'))  # 在这里使用 softmax 来预测每个类中的概率#当每个时间步被预测为开或关时使用,sigmoid函数可以作为多个类,并且需要不同的损失度量model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])# 编译模型model.summary()model.fit(trainx, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose, class_weight = {0:2.2,1:1 }) # 实施类权重以修复类不平衡,并停止对所有状态进行预测# 评估模型pr = model.predict(testx)pt = model.predict(trainx)plt.figure()ax1 = plt.subplot(211)ax1.title.set_text('Training set')ax1.plot(np.round(pt[:,0]), label = 'Predicted class')ax1.plot(trainy[:,0], label ='class (1=on) ' )ax1.plot(pt[:, 0], label='Class probability')ax2 = plt.subplot(212)ax2.title.set_text('Test set')ax2.plot(np.round(pr[:, 0]), label = 'Predicted class')ax2.plot(testy[:, 0],label ='class (1=on) ' )ax2.plot(pr[:, 0],label='Class probability')ax2.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fancybox=True, shadow=True, ncol=3)plt.show() loss1, accuracy = model.evaluate(testx, testy, batch_size=batch_size, verbose=0)loss2, at = model.evaluate(trainx, trainy, batch_size=batch_size, verbose=0)print('Training accuracy: %.3f ' % at)print('Training loss: %.3f ' % loss2)print('Test accuracy: %.3f' % accuracy)print('Test loss: %.3f' % loss1)return accuracy,model#模型评估
def model_variability(scores):print(scores)m, s = np.mean(scores), np.std(scores)print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))#将数据重新格式化为训练集和测试集。
# 数据正在加窗,最后一次的开/关状态记录在一个热向量中
trainx, trainy, testx, testy = sep_data(houses)repeats = 3
scores = list()
for r in range(repeats):score,model = run_model(trainx, trainy, testx, testy)score *=100.0print('>#%d: %.3f' % (r + 1, score))scores.append(score)model_variability(scores)

数据:下载

【数据挖掘】基于卷积神经网络的非侵入式负荷分解(NILM)Python实现相关推荐

  1. 非侵入式负荷分解方法

    非侵入式负荷监测(non-intrusive load monitoring,NILM) 0 引言 电力负荷设备检测和分解方法分为:侵入式和非侵入式. 侵入式 在每个用户的电气设备上都安装传感器以获得 ...

  2. 何为非侵入式负荷分解

    0 解答 有同学提出了一些问题,我在这里再说明一次. 1)深度学习算法应用于非侵入式负荷分解是不需要事件检测的吗? 回答:是的,不需要事件检测.深度学习算法应用于负荷辨识的话,必须要有事件检测. 2) ...

  3. 非侵入式负荷分解之BLUED数据集

    非侵入式负荷分解之BLUED数据集 BLUED数据集包含大约8天之内来自单个美国家庭的高频(12 kHz)家庭级数据.数据集的论文:<BLUED: A Fully Labeled Public ...

  4. 非侵入式负荷matlab程序,非侵入式负荷分解之BLUED数据集

    非侵入式负荷分解之BLUED数据集 非侵入式负荷分解之BLUED数据集 BLUED数据集包含大约8天之内来自单个美国家庭的高频(12 kHz)家庭级数据.该数据集还包含一个事件列表,该列表涉及家庭中每 ...

  5. 【NILM】非侵入式负荷分解数据集下载链接

    非侵入式负荷分解数据集 1.UK-DALE数据集下载: 下载链接 2.REDD数据集下载: 下载链接 用户名: redd           密码: disaggregatetheenergy 3.R ...

  6. 基于KPCA 和 STFT 非侵入式负荷监控(Matlab代码实现)

  7. 何为非侵入式负荷分解-深度学习算法实现

    1 前言 做负荷分解的网络很多,本篇用contrib中的几个网络对AMPds数据集进行训练和测试.本篇内容较短.仅展示部分网络的代码和结果. 2 数据集 AMPds数据集所选电器见下表.电器选择原则如 ...

  8. “泰迪杯”挑战赛 -利用非侵入式负荷检测进行高效率数据挖掘(完整数学模型)

    目录 1 研究背景与意义 2 变量说明 3 问题分析 4 问题一 4.1 数据预处理 4.1.1 降噪处理 4.1.2 数据变换 4.2 负荷特征分析 4.2.1 暂态特征 4.2.2 稳态特征 5 ...

  9. 微分算法 非侵入式负荷识别_一种非侵入式用电负荷识别方法与流程

    本发明涉及智能电网领域,具体地,涉及一种非侵入式用电负荷识别方法. 背景技术: 在智能电网环境下,智能量测设备会逐步得到广泛应用,从而能够得到用户负荷准确的.海量的数据.利用数据挖掘方法对用户负荷大数 ...

最新文章

  1. 多个互相有联系的checkbox的单选逻辑
  2. 基于WebGL架构的3D可视化平台—三维设备管理(ThingJS实现楼宇设备管理3D可视化)...
  3. linux IP局域网监控工具——iptraf
  4. C语言数据结构(大话数据结构——笔记3)第五章:串(字符串)
  5. mysql怎么查看代码_MySQL中的编码查看与设置(转载)
  6. VS设置程序启动权限为管理员权限
  7. php使用PHPMailer发送邮件示例
  8. Jquery 小技巧
  9. Hadoop中的Streaming(20)
  10. 解决mavencommons-cli:commons-cli:jar:1.0 下载不下来的错误
  11. 计算机word表格平均分,Word表格自动求和_word求平均值 - 学无忧
  12. window的git extensions保存密码
  13. 批量修改图片的后缀名格式
  14. Python带你在朋友圈环球旅行
  15. c语言:用二分法求方程在(-10,10)之间的根:2x^3-4x^2+3x-6=0.
  16. nginx反向代理是什么意思
  17. 自学php多久可以工作_PHP语言自学要多久才能工作
  18. python循环3次停止_【Python】quot;为所欲为quot;怎么成语接龙?
  19. matlab 中阿拉伯字母,常用阿拉伯字母念法
  20. web canvas图片标点 得像素坐标数组

热门文章

  1. 牛客网——字符串逆序
  2. python调用海康威视sdk库_HikVision SDK: C++ 至 Python
  3. 无向图双连通分量(DCC)
  4. 深度学习中的Normalization模型——张俊林 阅读笔记
  5. 山东大学离散数学1知识点整理
  6. IPAD,售前演示必备利器
  7. SAP-PM设备模块-PM主数据之计量点
  8. 【每日一题】 1518. 换酒问题
  9. Freedos在运行时从实模式进入保护模式
  10. 删除文件的正确姿势-Linux权限探索