数据挖掘实战——电力窃漏电用户自动识别
这次我们来用数据挖掘的方法来进行电力窃漏电用户自动识别具体文件与源代码可以从我的GitHub地址获取https://github.com/liuzuoping/MeachineLearning-Case读取数据并分析import numpy as npimport pandas as pdinputfile = '../data/data.csv' # 输入的数据文件data = ...
这次我们来用数据挖掘的方法来进行电力窃漏电用户自动识别
具体文件与源代码可以从我的GitHub地址获取
https://github.com/liuzuoping/MeachineLearning-Case
读取数据并分析
import numpy as np
import pandas as pd
inputfile = '../data/data.csv' # 输入的数据文件
data = pd.read_csv(inputfile) # 读取数据
# 描述性统计分析
description = [data.min(), data.max(), data.mean(), data.std()] # 依次计算最小值、最大值、均值、标准差
description = pd.DataFrame(description, index = ['Min', 'Max', 'Mean', 'STD']).T # 将结果存入数据框
print('描述性统计结果:\n',np.round(description, 2)) # 保留两位小数
# 相关性分析
corr = data.corr(method = 'pearson') # 计算相关系数矩阵
print('相关系数矩阵为:\n',np.round(corr, 2)) # 保留两位小数
# 绘制热力图
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['font.sans-serif']=['KaiTi']
plt.rcParams['axes.unicode_minus']=False
plt.subplots(figsize=(10, 10)) # 设置画面大小
sns.heatmap(corr, annot=True, vmax=1, square=True, cmap="Blues")
plt.title('相关性热力图')
plt.show()
plt.close
描述性统计结果:
Min Max Mean STD
x1 3831732.00 7599295.00 5579519.95 1262194.72
x2 181.54 2110.78 765.04 595.70
x3 448.19 6882.85 2370.83 1919.17
x4 7571.00 42049.14 19644.69 10203.02
x5 6212.70 33156.83 15870.95 8199.77
x6 6370241.00 8323096.00 7350513.60 621341.85
x7 525.71 4454.55 1712.24 1184.71
x8 985.31 15420.14 5705.80 4478.40
x9 60.62 228.46 129.49 50.51
x10 65.66 852.56 340.22 251.58
x11 97.50 120.00 103.31 5.51
x12 1.03 1.91 1.42 0.25
x13 5321.00 41972.00 17273.80 11109.19
y 64.87 2088.14 618.08 609.25
相关系数矩阵为:
x1 x2 x3 x4 x5 x6 … x9 x10 x11 x12 x13 y
x1 1.00 0.95 0.95 0.97 0.97 0.99 … 0.98 0.98 -0.29 0.94 0.96 0.94
x2 0.95 1.00 1.00 0.99 0.99 0.92 … 0.98 0.98 -0.13 0.89 1.00 0.98
x3 0.95 1.00 1.00 0.99 0.99 0.92 … 0.98 0.99 -0.15 0.89 1.00 0.99
x4 0.97 0.99 0.99 1.00 1.00 0.95 … 0.99 1.00 -0.19 0.91 1.00 0.99
x5 0.97 0.99 0.99 1.00 1.00 0.95 … 0.99 1.00 -0.18 0.90 0.99 0.99
x6 0.99 0.92 0.92 0.95 0.95 1.00 … 0.97 0.96 -0.34 0.95 0.94 0.91
x7 0.95 0.99 1.00 0.99 0.99 0.93 … 0.98 0.99 -0.15 0.89 1.00 0.99
x8 0.97 0.99 0.99 1.00 1.00 0.95 … 0.99 1.00 -0.15 0.90 1.00 0.99
x9 0.98 0.98 0.98 0.99 0.99 0.97 … 1.00 0.99 -0.23 0.91 0.99 0.98
x10 0.98 0.98 0.99 1.00 1.00 0.96 … 0.99 1.00 -0.17 0.90 0.99 0.99
x11 -0.29 -0.13 -0.15 -0.19 -0.18 -0.34 … -0.23 -0.17 1.00 -0.43 -0.16 -0.12
x12 0.94 0.89 0.89 0.91 0.90 0.95 … 0.91 0.90 -0.43 1.00 0.90 0.87
x13 0.96 1.00 1.00 1.00 0.99 0.94 … 0.99 0.99 -0.16 0.90 1.00 0.99
y 0.94 0.98 0.99 0.99 0.99 0.91 … 0.98 0.99 -0.12 0.87 0.99 1.00
[14 rows x 14 columns]
自定义灰色预测函数
def GM11(x0): #自定义灰色预测函数
import numpy as np
x1 = x0.cumsum() #1-AGO序列
z1 = (x1[:len(x1)-1] + x1[1:])/2.0 #紧邻均值(MEAN)生成序列
z1 = z1.reshape((len(z1),1))
B = np.append(-z1, np.ones_like(z1), axis = 1)
Yn = x0[1:].reshape((len(x0)-1, 1))
[[a],[b]] = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Yn) #计算参数
f = lambda k: (x0[0]-b/a)*np.exp(-a*(k-1))-(x0[0]-b/a)*np.exp(-a*(k-2)) #还原值
delta = np.abs(x0 - np.array([f(i) for i in range(1,len(x0)+1)]))
C = delta.std()/x0.std()
P = 1.0*(np.abs(delta - delta.mean()) < 0.6745*x0.std()).sum()/len(x0)
return f, a, b, x0[0], C, P #返回灰色预测函数、a、b、首项、方差比、小残差概率
进行预测
import sys
sys.path.append('../code') # 设置路径
import numpy as np
import pandas as pd
from GM11 import GM11 # 引入自编的灰色预测函数
inputfile1 = '../tmp/new_reg_data.csv' # 输入的数据文件
inputfile2 = '../data/data.csv' # 输入的数据文件
new_reg_data = pd.read_csv(inputfile1) # 读取经过特征选择后的数据
data = pd.read_csv(inputfile2) # 读取总的数据
new_reg_data.index = range(1994, 2014)
new_reg_data.loc[2014] = None
new_reg_data.loc[2015] = None
l = ['x1', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x13']
for i in l:
f = GM11(new_reg_data.loc[range(1994, 2014),i].as_matrix())[0]
new_reg_data.loc[2014,i] = f(len(new_reg_data)-1) # 2014年预测结果
new_reg_data.loc[2015,i] = f(len(new_reg_data)) # 2015年预测结果
new_reg_data[i] = new_reg_data[i].round(2) # 保留两位小数
outputfile = '../tmp/new_reg_data_GM11.xls' # 灰色预测后保存的路径
y = list(data['y'].values) # 提取财政收入列,合并至新数据框中
y.extend([np.nan,np.nan])
new_reg_data['y'] = y
new_reg_data.to_excel(outputfile) # 结果输出
print('预测结果为:\n',new_reg_data.loc[2014:2015,:]) # 预测结果展示
预测结果为:
Unnamed: 0 x1 x3 … x8 x13 y
2014 NaN 8142148.24 7042.31 … 18686.28 44506.47 NaN
2015 NaN 8460489.28 8166.92 … 21474.47 49945.88 NaN
[2 rows x 10 columns]
更多推荐
所有评论(0)