日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二十八、电力窃漏电案例模型构建

發布時間:2024/9/16 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二十八、电力窃漏电案例模型构建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 綜合案例模型構建

  • 構建竊漏電用戶識別模型;
  • 構建LM神經網絡模型;
  • 構建CART決策樹模型;
  • 模型評價

2. 構建竊漏電用戶識別模型

2.1 構建專家樣本

專家樣本準備完成后,需要劃分測試樣本和訓練樣本,隨機選取20%作為測試樣本,剩下的作為訓練樣本。

2.3 資源

數據集

  • 專家樣本數據集的內容包括時間、用戶編號、電量趨勢下降指標、線損指標、告警類指標和是否竊漏電標簽,數據集共有291條樣本數據。

    數據集詳情參考model.xls

工具庫

pandas==0.24.2

2.4 步驟

  • 導入數據分析庫pandas
  • 導入隨機函數shuffle,用于打亂順序
  • 設置訓練數據比例,劃分數據
  • 2.5 代碼

    數據劃分實現代碼

    import pandas as pd #導入數據分析庫 from random import shuffle #導入隨機函數shuffle,用來打算數據datafile = '../data/model.xls' #數據名 data = pd.read_excel(datafile) #讀取數據,數據的前三列是特征,第四列是標簽 data = data.as_matrix() #將表格轉換為矩陣 shuffle(data) #隨機打亂數據p = 0.8 #設置訓練數據比例 train = data[:int(len(data)*p),:] #前80%為訓練集 test = data[int(len(data)*p):,:] #后20%為測試集

    3. 構建LM神經網絡

    3.1 模型搭建

    使用Keras庫建立神經網絡模型。設定LM神經網絡有三層模型,輸入層為3個節點,隱藏層為10個結點,輸出層為1個結點,使用Adam方法求解。

    LM神經網絡的混淆矩陣

    • 通過混淆矩陣可得,模型的分類準確率為(161+58)/(161+58+6+7)=94.4%,正常用戶被誤判為漏電用戶占正常用戶對7/(161+7)=4.2%,竊漏電用戶被誤判為正常用戶占竊漏電用戶的6/(6+58)=9.4%。

    3.2 資源

    資源庫

    pandas==0.24.2 scipy==1.1.0 keras=2.2.0 Tensorflow=1.10

    數據集

    數據集詳情參考model.xls

    3.3 步驟

  • 導入神經網絡初始化函數
  • 導入神經網絡層函數,激活函數
  • 構建模型存儲路徑
  • 建立神經網絡
  • 編譯模型,使用adam方法求解
  • 保存模型
  • 顯示混淆矩陣及可視化結果
  • 3.4 代碼

    LM神經網絡實現代碼

    #構建LM神經網絡模型 from keras.models import Sequential #導入神經網絡初始化函數 from keras.layers.core import Dense, Activation #導入神經網絡層函數、激活函數netfile = '../tmp/net.model' #構建的神經網絡模型存儲路徑net = Sequential() #建立神經網絡 net.add(Dense(input_dim = 3, output_dim = 10)) #添加輸入層(3節點)到隱藏層(10節點)的連接 net.add(Activation('relu')) #隱藏層使用relu激活函數 net.add(Dense(input_dim = 10, output_dim = 1)) #添加隱藏層(10節點)到輸出層(1節點)的連接 net.add(Activation('sigmoid')) #輸出層使用sigmoid激活函數 net.compile(loss = 'binary_crossentropy', optimizer = 'adam', class_mode = "binary") #編譯模型,使用adam方法求解net.fit(train[:,:3], train[:,3], nb_epoch=1000, batch_size=1) #訓練模型,循環1000次 net.save_weights(netfile) #保存模型predict_result = net.predict_classes(train[:,:3]).reshape(len(train)) #預測結果變形

    4 構建CART決策樹模型

    4.1 使用sklearn機器學習庫構建決策樹模型

    通過scikit-learn利用訓練樣本構建CART決策樹模型,得到的混淆矩陣如下圖所示,分類準確率為(160+56)/(160+56+3+13)=93.1%,正常用戶被誤判為竊漏電用戶占正常用戶的 13/(13+160)=7.5%,竊漏電用戶被誤判為正常用戶占竊漏電用戶的3/(3+56)=5.1%。

    4. 2代碼

    決策樹構建竊漏電用戶識別代碼
    #構建CART決策樹模型
    from sklearn.tree import DecisionTreeClassifier #導入決策樹模型

    treefile = '../tmp/tree.pkl' #模型輸出名字 tree = DecisionTreeClassifier() #建立決策樹模型 tree.fit(train[:,:3], train[:,3]) #訓練#保存模型 from sklearn.externals import joblib joblib.dump(tree, treefile)from cm_plot import * #導入自行編寫的混淆矩陣可視化函數 cm_plot(train[:,3], tree.predict(train[:,:3])).show() #顯示混淆矩陣可視化結果 #注意到Scikit-Learn使用predict方法直接給出預測結果。

    5 綜合案例模型評價

    5.1 使用測試數據

    對于訓練樣本,LM神經網絡和CART決策樹的分類準確相差不大,分別為94%和93%,為了進一步評估模型分類的性能,故利用測試樣本對兩個模型進行評價,采用ROC曲線評價方法進行評估。

    6 完成代碼

    6.1 Cart決策樹模型

    #-*- coding: utf-8 -*- #構建并測試CART決策樹模型import pandas as pd #導入數據分析庫 from random import shuffle #導入隨機函數shuffle,用來打算數據datafile = '../data/model.xls' #數據名 data = pd.read_excel(datafile) #讀取數據,數據的前三列是特征,第四列是標簽 data = data.as_matrix() #將表格轉換為矩陣 shuffle(data) #隨機打亂數據p = 0.8 #設置訓練數據比例 train = data[:int(len(data)*p),:] #前80%為訓練集 test = data[int(len(data)*p):,:] #后20%為測試集#構建CART決策樹模型 from sklearn.tree import DecisionTreeClassifier #導入決策樹模型treefile = '../tmp/tree.pkl' #模型輸出名字 tree = DecisionTreeClassifier() #建立決策樹模型 tree.fit(train[:,:3], train[:,3]) #訓練#保存模型 from sklearn.externals import joblib joblib.dump(tree, treefile)from cm_plot import * #導入自行編寫的混淆矩陣可視化函數 cm_plot(train[:,3], tree.predict(train[:,:3])).show() #顯示混淆矩陣可視化結果 #注意到Scikit-Learn使用predict方法直接給出預測結果。from sklearn.metrics import roc_curve #導入ROC曲線函數fpr, tpr, thresholds = roc_curve(test[:,3], tree.predict_proba(test[:,:3])[:,1], pos_label=1) plt.plot(fpr, tpr, linewidth=2, label = 'ROC of CART', color = 'green') #作出ROC曲線 plt.xlabel('False Positive Rate') #坐標軸標簽 plt.ylabel('True Positive Rate') #坐標軸標簽 plt.ylim(0,1.05) #邊界范圍 plt.xlim(0,1.05) #邊界范圍 plt.legend(loc=4) #圖例 plt.show() #顯示作圖結果

    6.2 LM神經網絡模型

    #-*- coding: utf-8 -*-import pandas as pd from random import shuffledatafile = '../data/model.xls' data = pd.read_excel(datafile) data = data.as_matrix() shuffle(data)p = 0.8 #設置訓練數據比例 train = data[:int(len(data)*p),:] test = data[int(len(data)*p):,:]#構建LM神經網絡模型 from keras.models import Sequential #導入神經網絡初始化函數 from keras.layers.core import Dense, Activation #導入神經網絡層函數、激活函數netfile = '../tmp/net.model' #構建的神經網絡模型存儲路徑net = Sequential() #建立神經網絡 net.add(Dense(input_dim = 3, output_dim = 10)) #添加輸入層(3節點)到隱藏層(10節點)的連接 net.add(Activation('relu')) #隱藏層使用relu激活函數 net.add(Dense(input_dim = 10, output_dim = 1)) #添加隱藏層(10節點)到輸出層(1節點)的連接 net.add(Activation('sigmoid')) #輸出層使用sigmoid激活函數 net.compile(loss = 'binary_crossentropy', optimizer = 'adam', class_mode = "binary") #編譯模型,使用adam方法求解net.fit(train[:,:3], train[:,3], nb_epoch=1000, batch_size=1) #訓練模型,循環1000次 net.save_weights(netfile) #保存模型predict_result = net.predict_classes(train[:,:3]).reshape(len(train)) #預測結果變形 '''這里要提醒的是,keras用predict給出預測概率,predict_classes才是給出預測類別,而且兩者的預測結果都是n x 1維數組,而不是通常的 1 x n'''from cm_plot import * #導入自行編寫的混淆矩陣可視化函數 cm_plot(train[:,3], predict_result).show() #顯示混淆矩陣可視化結果from sklearn.metrics import roc_curve #導入ROC曲線函數predict_result = net.predict(test[:,:3]).reshape(len(test)) fpr, tpr, thresholds = roc_curve(test[:,3], predict_result, pos_label=1) plt.plot(fpr, tpr, linewidth=2, label = 'ROC of LM') #作出ROC曲線 plt.xlabel('False Positive Rate') #坐標軸標簽 plt.ylabel('True Positive Rate') #坐標軸標簽 plt.ylim(0,1.05) #邊界范圍 plt.xlim(0,1.05) #邊界范圍 plt.legend(loc=4) #圖例 plt.show() #顯示作圖結果

    6.3 混淆矩陣代碼

    def cm_plot(y, yp):from sklearn.metrics import confusion_matrix #導入混淆矩陣函數cm = confusion_matrix(y, yp) #混淆矩陣import matplotlib.pyplot as plt #導入作圖庫plt.matshow(cm, cmap=plt.cm.Greens) #畫混淆矩陣圖,配色風格使用cm.Greens,更多風格請參考官網。plt.colorbar() #顏色標簽for x in range(len(cm)): #數據標簽for y in range(len(cm)):plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')plt.ylabel('True label') #坐標軸標簽plt.xlabel('Predicted label') #坐標軸標簽return pltfor x in range(len(cm)): #數據標簽for y in range(len(cm)):plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')plt.ylabel('True label') #坐標軸標簽plt.xlabel('Predicted label') #坐標軸標簽return plt

    總結

    以上是生活随笔為你收集整理的二十八、电力窃漏电案例模型构建的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。