算法思想:
设
M
a
x
t
r
i
x
为
N
,
J
维
矩
阵
、
S
i
z
e
为
M
设Maxtrix为N, J维矩阵、Size为M
设Maxtrix为N,J维矩阵、Size为M
V
e
c
t
o
r
1
是
表
示
M
a
t
r
i
x
的
第
1
行
Vector_1 是表示Matrix的第1行
Vector1是表示Matrix的第1行
则
:
(
N
e
w
V
e
c
t
o
r
)
i
=
∑
1
≤
j
≤
M
j
∗
[
(
v
e
c
t
o
r
)
(
i
+
j
)
−
(
v
e
c
t
o
r
)
(
i
−
j
)
]
则:(NewVector)_i = \sum\limits_{1 \le j \le M} j*[(vector)_(i+j_) - (vector)_(i-j_)]
则:(NewVector)i=1≤j≤M∑j∗[(vector)(i+j)−(vector)(i−j)]
其
中
:
i
f
(
i
+
j
)
>
N
:
i
+
j
=
N
;
i
f
(
i
−
1
)
<
0
:
i
−
j
=
0
其中:if (i+j) > N : i+j = N;if (i-1)<0: i-j=0
其中:if(i+j)>N:i+j=N;if(i−1)<0:i−j=0
i
∈
[
0
,
N
)
,
i
∈
Z
i ∈[0, N), i∈Z
i∈[0,N),i∈Z
代码实现
def calculate_delta(vector, correlation_numb=2):
"""
Calculate and returns the delta of given feature vector matrix for evaluating correlation
:param vector:
:param correlation_numb:
:return:
"""
rows, cols = vector.shape
deltas = zeros((rows, 20)) # initial vector of deltas
n = correlation_numb # 2 order vector shape
for i in range(rows):
index = []
j = 1
while j <= n:
if i - j < 0:
first = 0
else:
first = i - j
if i + j > rows - 1:
second = rows - 1
else:
second = i + j
index.append((second, first))
j += 1
deltas[i] = sum([(i + 1) * (vector[index[i][0]] - vector[index[i][1]]) for i in range(len(index))]) / 10
return deltas
具体例子
// 原矩阵Matrix
[[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5] Vector1
[5 6 7 8 5 5 6 7 8 5 5 6 7 8 5 5 6 7 8 5] Vector2
[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5] ...
[5 6 7 8 5 5 6 7 8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