一、读入数据
数据集来源https://github.com/wuyimengmaths/data/tree/master
import pandas as pd
import numpy as np
path = 'model.xls'
df = pd.read_excel(path)
data = df.values
dianliang = data[:,0] ##电量趋势
xiansun = data[:,1] ##线损指标
gaojin = data[:,2] ##告警类指标
target = data[:,3] ##是否漏电 标签
二、拆分数据集
将样本前三列数据合并作为特征,第四列当作标签,进行有监督训练。通过train_test_split将数据集拆分成训练集和测试集(80%用于训练,20%用于测试)。
a = np.vstack((dianliang, xiansun))
feature = np.vstack((a,gaojin))
feature = feature.transpose() ##转置
from sklearn.model_selection import train_test_split
feature_train, feature_test, target_train, target_test = train_test_split(feature, target, test_size=0.2,random_state=0)
三、搭建神经网络
我们搭建一个简单的神经网络。
from keras.models import Sequential
from keras.layers.core import Dense, Activation
net = Sequential()
##因为特征是三列,input_dim是三维的
net.add(Dense(input_dim =3,output_dim =14))
net.add(Activation('relu'))
net.add(Dense(input_dim = 14,output_dim =18))
net.add(Activation('relu'))
net.add(Dense(input_dim =18,output_dim =1))
net.add(Activation('sigmoid'))
net.compile(loss='binary_crossentropy', optimizer = 'adam') ##编译神经网络, 加速
##训练过程中epochs=100表示数据将被“轮”100次
net.fit(feature_train,target_train,epochs=100,batch_size=1)
last = net.predict_classes(feature_test)
##这里测试集的真实标签和预测的结果一样就认为是对的。
print((target_test==last)[0].sum()/len(last))
网友评论