文章传感数据分析——Entropy Weight Method (熵权法)中叙述了熵权法的基本步骤。
之前几期文章已经叙述了信息熵权法、TOPSIS熵权法、近似熵权法、样本熵权法。
本节将叙述模糊熵权法对多传感数据进行处理,并基于Python实现。
本文正文内容
熵权法的5个步骤可以参考我的另一篇文章传感数据分析——Entropy Weight Method (熵权法)。
F
u
z
z
y
E
n
FuzzyEn
FuzzyEn使用指数函数来模糊相似度公式。在实际实验中,
F
u
z
z
y
E
n
FuzzyEn
FuzzyEn、
S
a
m
p
E
n
SampEn
SampEn 以及
A
p
E
n
ApEn
ApEn得到的熵值差异并不明显,这与传感数据集本身有关。
重构数据
Y
Y
Y如下:
Y
i
=
[
x
(
i
)
,
x
(
i
+
1
)
,
?
?
,
x
(
i
+
m
?
1
)
]
?
x
0
(
i
)
,
i
=
1
,
2
,
?
?
,
N
?
m
+
1
Y_i=[x(i),x(i+1),\cdots,x(i+m-1)]-x_0(i),i=1,2,\cdots,N-m+1
Yi?=[x(i),x(i+1),?,x(i+m?1)]?x0?(i),i=1,2,?,N?m+1
其中
x
0
(
i
)
x_0(i)
x0?(i)如下:
x
0
(
i
)
=
1
m
∑
j
=
0
m
?
1
x
(
i
+
j
)
\begin{equation}x_0(i)=\dfrac{1}{m}\sum_{j=0}^{m-1}x(i+j)\end{equation}
x0?(i)=m1?j=0∑m?1?x(i+j)??
d
i
,
j
m
=
d
[
Y
i
,
Y
j
]
=
max
?
k
∈
(
0
,
m
?
1
)
∣
x
(
i
+
k
)
?
x
0
(
i
)
?
x
(
j
+
k
)
?
x
0
(
j
)
∣
\begin{equation} \begin{split}&d_{i,j}^m=d[Y_i,Y_j] \\ =&\max_{k\in(0,m-1)}|x(i+k)-x_0(i)-x(j+k)-x_0(j)| \end{split} \end{equation}
=?di,jm?=d[Yi?,Yj?]k∈(0,m?1)max?∣x(i+k)?x0?(i)?x(j+k)?x0?(j)∣???
D
i
,
j
m
=
exp
?
[
?
(
d
i
,
j
m
)
n
r
]
\begin{equation}D_{i,j}^m=\exp\biggl[-\frac{(d_{i,j}^m)^n}{r}\biggr]\end{equation}
Di,jm?=exp[?r(di,jm?)n?]??
ψ m + 1 ( r ) = 1 N ? m + 1 ∑ i = 1 N ? m + 1 ( 1 N ? m ∑ j = 1 , j ≠ i N ? m + 1 D i , j m ) \begin{equation}\begin{split}&\psi^{m+1}(r) \\ =&\frac{1}{N-m+1}\sum_{i=1}^{N-m+1}\left(\frac{1}{N-m}\sum_{j=1,j\neq i}^{N-m+1}D_{i,j}^{m}\right)\end{split} \end{equation} =?ψm+1(r)N?m+11?i=1∑N?m+1? ?N?m1?j=1,j=i∑N?m+1?Di,jm? ????
f F u c z y E n = ? ln ? ( ψ m + 1 ( r ) / ψ m ( r ) ) \begin{equation}f_{_{FuczyEn}}=-\ln(\psi^{m+1}(r)/\psi^{m}(r))\end{equation} fFuczyEn??=?ln(ψm+1(r)/ψm(r))??
式(1)~(5)中,
m
m
m 表示嵌入维度,
Y
Y
Y 表示
X
X
X 的相空间重构后的信号序列。
x
0
x_0
x0?是
m
m
m 个连续
x
(
i
+
j
)
x(i+j)
x(i+j)的平均值。
d
[
Y
i
,
Y
j
]
d[Y_i,Y_j]
d[Yi?,Yj?]表示
Y
i
Y_i
Yi?和
Y
j
Y_j
Yj?的对应端点之间的差值的最大值。
D
D
D 是使用模糊隶属函数后
Y
i
Y_i
Yi?和
Y
j
Y_j
Yj? 的相似度。
m
m
m是一个类似
ψ
m
+
1
(
i
)
\psi^{m+1}(i)
ψm+1(i)和
B
m
(
i
)
B^m(i)
Bm(i) 定义的函数。
F
u
z
z
y
E
n
FuzzyEn
FuzzyEn表示序列
X
i
X_i
Xi?的模糊熵值。
==========================================
相关细节叙述可以见大佬的博客:【熵与特征提取】从近似熵,到样本熵,到模糊熵,再到排列熵,究竟实现了什么?(第三篇)——“模糊熵”及其MATLAB实现。
系统: Windows 10 / Ubuntu 20.04
编程语言: Python 3.8
文本编译器: Vscode
所需库:pandas >= 0.23.0, matplotlib >= 2.2.2 , numpy >= 1.19.5,scikit-learn > 0.19.1
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
def fuzzy_entropy(dim, r, data, tau=1):
"""
模糊熵函数
输入参数:
dim: 嵌入维度(Embedding Dimension)
r: 容忍度值(Tolerance Value),一般为0.1 ~ 0.25 std(data)
data: 数据
tau: 下采样的延迟时间
"""
if tau > 1:
data = data[::tau]
N = len(data)
Phi = np.zeros(2)
for m in range(dim, dim + 2):
Ci = np.zeros(N - m + 1)
data_mat = np.zeros((m, N - m + 1))
# setting up data matrix - form vectors
for j in range(m):
data_mat[j, :] = data[j:N - m + j + 1]
# baseline
U0 = np.mean(data_mat, axis=0)
U0 = U0.reshape(len(U0), 1)
# remove baseline and calculate the absolute values
remapted_data = np.tile(U0.reshape(len(U0), 1), m)
Sm = np.abs(data_mat - remapted_data.T)
# Given vector Si, calculate the similarity degree between its'
# neighboring vector Sj
for i in range(N - m):
# excluded self-matches
Sm_tmp = np.delete( Sm, i, axis=1)
# maximum absolute difference of the corresponding scalar components
# of Si and Sj (j≠i)
dij = np.max(np.tile(Sm[:, i].reshape(len(Sm[:, i]), 1), N - m) - Sm_tmp, axis=0)
# similarity degree
Aij = np.exp(-np.log(2) * (dij / r) ** 2)
# averaging all the similarity degree of its neighboring vectors Sj
Ci[i] = np.sum(Aij) / (N - m)
# summing over the counts
Phi[m - dim] = np.sum(Ci) / (N - m + 1) # φ_m and φ_m+1
fuzzyen = np.log(Phi[0]) - np.log(Phi[1]) # fuzzyen = ln(φ_m)-ln(φ_m+1)
return fuzzyen
def fuzzy_en_method(x):
# 数据行列数
n, m = x.shape
# 数据的归一化处理
scaler = MinMaxScaler(feature_range=(0.002, 0.996))
x_normalized = scaler.fit_transform(x)
x_T = x_normalized.T
# 下采样的延迟时间
e_lag = 10
# 嵌入维度
e_dim = 2
# Calculate the proportion of each record for each indicator
p = np.zeros((n, m))
for i in range(n):
for j in range(m):
p[i, j] = x_normalized[i, j] / np.sum(x_normalized[:, j])
# Calculate the entropy value for each indicator
e = np.zeros(m)
r = 0.2 * np.std(x_normalized, axis=0)
for j in range(m):
e[j] = fuzzy_entropy(e_dim, r[j], x_T[j, :], e_lag)
# Calculate information entropy redundancy
d = np.ones(m) - e
# Calculate weights w
w = d / np.sum(d)
# Calculate composite scores s
s = np.dot(w, p.T)
# Scale the scores
S = s * 10000
return S, w
if __name__ == '__main__':
# 导入数据
data=pd.read_excel("dataset/dataset_test1.xlsx", sheet_name=0,header=0,index_col=0)
# 保存路径
save_path = './result/'
# 调用熵权法函数处理数据
score, weight = fuzzy_en_method(data)
# 保存FuzzyEn熵权法输出值
score_pd = pd.DataFrame(score, columns=['FuzzyEn-EWM结果'])
score_pd.to_excel(save_path + "score_FuzzyEn.xlsx", sheet_name="FuzzyEn-EWMscores")
# 绘图并保存
plt.figure(figsize=(16,9))
plt.plot(score)
plt.legend(['FuzzyEn-EWM Scores'])
plt.title("FuzzyEn-EWM Test", fontsize=18)
plt.savefig(save_path + "FuzzyEn-EWM_scores.png")
plt.show()
结果图
以上就是本节对传感数据进行模糊熵权法计算输出综合评价值的内容,本文简单介绍了传感数据分析中模糊熵权法基本公式及Python的实现,代码见传感数据分析-Fuzzy Entropy Weight Method(模糊熵权法),后续还会持续更新本系列,收藏过200,后台联系我,我会免费将熵权法全部相关的代码都发给你。