1、了解热水器采集数据基本情况
国内某热水器生产厂商新研发的一种高端智能热水器,在状态发生改变或者有水流状态时,会采集各项数据。抽取200个热水器用户的用水记录作为原始建模数据,热水器采集到用户用水数据如下表所示。
2、热水器数据特征说明
热水器采集的用水数据包含12个特征:热水器编码,发生时间,开关机状态,加热中,保温中,有无水流,实际温度,热水量,水流量,节能模式,加热剩余时间和当前设置温度。其解释说明如下表所示。
3、熟悉家用热水器用户行为分析的步骤与流程
3.1用水事件识别
在热水器用户行为分析过程中,用水事件识别是最为关键的环节。根据该热水器生产厂商提供的数据热水器用户用水事件划分与识别项目的整体目标如下。
(1)根据热水器采集到的数据,划分一次完整用水事件。
(2)在划分好的一次完整用水事件中,识别出洗浴事件。
3.2总体流程
4、构建行为事件分析的BP神经网络模型
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
import joblib
读取数据
Xtrain = pd.read_excel(‘…/tmp/sj_final.xlsx’)
ytrain = pd.read_excel(‘…/data/water_heater_log.xlsx’)
test = pd.read_excel(‘…/data/test_data.xlsx’)
训练集测试集区分。
x_train, x_test, y_train, y_test =
Xtrain.iloc[:,5:],test.iloc[:,4:-1],
ytrain.iloc[:,-1],test.iloc[:,-1]
标准化
stdScaler = StandardScaler().fit(x_train)
x_stdtrain = stdScaler.transform(x_train)
x_stdtest = stdScaler.transform(x_test)
建立模型
bpnn = MLPClassifier(hidden_layer_sizes = (17,10),
max_iter = 200, solver = ‘lbfgs’,random_state=45)
bpnn.fit(x_stdtrain, y_train)
保存模型
joblib.dump(bpnn,‘water_heater_nnet.m’)
print(‘构建的模型为:\n’,bpnn)
模型预测
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve
from sklearn.metrics import accuracy_score
import matplotlib as mpl
mpl.use(‘TkAgg’)
import matplotlib.pyplot as plt
bpnn = joblib.load(‘water_heater_nnet.m’) ## 加载模型
y_pred = bpnn.predict(x_stdtest) # 返回预测结果
print(‘神经网络预测结果评价报告:\n’,
classification_report(y_test,y_pred))
绘制roc曲线图
plt.rcParams[‘font.sans-serif’] = ‘SimHei’ ##显示中文
plt.rcParams[‘axes.unicode_minus’] = False ##显示负号
fpr, tpr, thresholds = roc_curve(y_pred,y_test) ## 求出TPR和FPR
plt.figure(figsize=(6,4))## 创建画布
plt.plot(fpr,tpr)## 绘制曲线
plt.title(‘用户用水事件识别ROC曲线’)##标题
plt.xlabel(‘FPR’)## x轴标签
plt.ylabel(‘TPR’)## y轴标签
plt.savefig(‘用户用水事件识别ROC曲线.png’)## 保存图片
plt.show()## 显示图形
5、运行结果