- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章PHP局部异常因子算法-Local Outlier Factor(LOF)算法的具体实现解析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
这两天在完善自己系统的过程中要实现一个查找异常的功能,于是在朋友的指点下学习并实现了异常点查找的一个基本算法“局部异常因子算法-Local Outlier Factor(LOF)算法”.
首先,找相关说明看看这是个什么东西吧.
我参考了这一篇文章: 异常点/离群点检测算法——LOF 。
大致明白了lof算法是在讲什么,我的理解还有很多不完善的地方,不过还是作为一个初学者写出来供大家批评指正.
根据我的理解大致描述如下:
1、 k-distance,点p的第k距离就是距离点p第k远的那个点的距离,k可以是任意值。在实际生活中可能会这样:小明说“小红家是离我家第五近的,小赵、小钱、小孙、小李家都比她家离我家近”所以此处小红家距离小明家的距离就是小明家k为5时的第k距离.
2、k-distance neighborhood of p,第k距离领域,按照上面的例子就是{小赵、小钱、小孙、小李、小红},把离p最近的k个点放入一个数组就是第k距离领域了.
3、reach-distance:可达距离。点o到点p的第k可达距离分两种情况,一种是p在o的第k距离领域那个数组中,这时候可达距离等于第k距离,第二种就是p离点o比较远,不在o的第k距离领域中,此时的可达距离即为真实距离。依然使用上述的例子,小赵家在小明家的第k邻域中,所以可达距离就是第k距离,就是小红家的距离,而二狗子家里小明家很远,可达距离就是真实距离了.
4、local reachability density:局部可达密度。点p的局部可达密度是指点p第k距离邻域中所有成员到点p的可达距离的平均值的倒数,有点复杂,不过多读几遍还是蛮好理解的,就不举例子了.
5、local outlier factor:局部离群因子。点p的局部离群因子即为领域中所有点的局部可达密度的平均数比点p的局部可达密度,不做解释。 到这里为止就是我对lof算法的一个大致理解,具体讲解还要看上面我参考的那篇文章,写的很清楚.
接下来我找了网上的一篇对此算法的实现,很遗憾没有php版本,于是我就找到了这篇文章:基于密度的局部离群点检测(lof算法) (Java 实现) 。
如题所示,是一篇Java实现,于是我就在大神的基础上对其进行修改,改成了一个php的版本。因为对迭代器理解的不是很好,所以迭代器实现部分改成了一般函数,有机会再进行完善.
如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
<?php
class
DataNode {
private
$nodeName
;
// 样本点名
private
$dimensioin
;
// 样本点的维度
private
$kDistance
;
// k-距离
private
$kNeighbor
=
array
();
// k-领域
private
$distance
;
// 到给定点的欧几里得距离
private
$reachDensity
;
// 可达密度
private
$reachDis
;
// 可达距离
private
$lof
;
// 局部离群因子
public
function
__construct() {
$num
= func_num_args();
//获得参数个数
$args
= func_get_args();
//获得参数列表数组
switch
(
$num
){
case
0:
break
;
case
2:
$this
->__call(
'__construct2'
,
$args
);
break
;
}
}
public
function
__call(
$name
,
$arg
)
//根据函数名调用函数
{
return
call_user_func_array(
array
(
$this
,
$name
),
$arg
);
}
public
function
__construct2(
$nodeName
,
$dimensioin
)
{
$this
->nodeName =
$nodeName
;
$this
->dimensioin =
$dimensioin
;
}
public
function
getNodeName() {
return
$this
->nodeName;
}
public
function
setNodeName(
$nodeName
) {
$this
->nodeName =
$nodeName
;
}
public
function
getDimensioin() {
return
$this
->dimensioin;
}
public
function
setDimensioin(
$dimensioin
) {
$this
->dimensioin =
$dimensioin
;
}
public
function
getkDistance() {
return
$this
->kDistance;
}
public
function
setkDistance(
$kDistance
) {
$this
->kDistance =
$kDistance
;
}
public
function
getkNeighbor() {
return
$this
->kNeighbor;
}
public
function
setkNeighbor(
$kNeighbor
) {
$this
->kNeighbor =
$kNeighbor
;
}
public
function
getDistance() {
return
$this
->distance;
}
public
function
setDistance(
$distance
) {
$this
->distance =
$distance
;
}
public
function
getReachDensity() {
return
$this
->reachDensity;
}
public
function
setReachDensity(
$reachDensity
) {
$this
->reachDensity =
$reachDensity
;
}
public
function
getReachDis() {
return
$this
->reachDis;
}
public
function
setReachDis(
$reachDis
) {
$this
->reachDis =
$reachDis
;
}
public
function
getLof() {
return
$this
->lof;
}
public
function
setLof(
$lof
) {
$this
->lof =
$lof
;
}
}
class
OutlierNodeDetect {
private
static
$INT_K
= 5;
//正整数K
// 1.找到给定点与其他点的欧几里得距离
// 2.对欧几里得距离进行排序,找到前5位的点,并同时记下k距离
// 3.计算每个点的可达密度
// 4.计算每个点的局部离群点因子
// 5.对每个点的局部离群点因子进行排序,输出。
public
function
getOutlierNode(
$allNodes
) {
$kdAndKnList
=
$this
->getKDAndKN(
$allNodes
);
$this
->calReachDis(
$kdAndKnList
);
$this
->calReachDensity(
$kdAndKnList
);
$this
->calLof(
$kdAndKnList
);
//降序排序
$kdAndKnList
=
$this
->rsortArr(
$kdAndKnList
);
return
$kdAndKnList
;
}
/**
* 计算每个点的局部离群点因子
* @param kdAndKnList
*/
private
function
calLof(
$kdAndKnList
) {
foreach
(
$kdAndKnList
as
$node
):
$tempNodes
=
$node
->getkNeighbor();
$sum
= 0.0;
foreach
(
$tempNodes
as
$tempNode
):
$rd
=
$this
->getRD(
$tempNode
->getNodeName(),
$kdAndKnList
);
$sum
=
$rd
/
$node
->getReachDensity() +
$sum
;
endforeach
;
$sum
=
$sum
/ (double) self::
$INT_K
;
$node
->setLof(
$sum
);
endforeach
;
}
/**
* 计算每个点的可达距离
* @param kdAndKnList
*/
private
function
calReachDensity(
$kdAndKnList
) {
foreach
(
$kdAndKnList
as
$node
):
$tempNodes
=
$node
->getkNeighbor();
$sum
= 0.0;
$rd
= 0.0;
foreach
(
$tempNodes
as
$tempNode
):
$sum
=
$tempNode
->getReachDis() +
$sum
;
endforeach
;
$rd
= (double) self::
$INT_K
/
$sum
;
$node
->setReachDensity(
$rd
);
endforeach
;
}
/**
* 计算每个点的可达密度,reachdis(p,o)=max{ k-distance(o),d(p,o)}
* @param kdAndKnList
*/
private
function
calReachDis(
$kdAndKnList
) {
//for (DataNode node : kdAndKnList) {
foreach
(
$kdAndKnList
as
$node
):
$tempNodes
=
$node
->getkNeighbor();
//for (DataNode tempNode : tempNodes) {
foreach
(
$tempNodes
as
$tempNode
):
//获取tempNode点的k-距离
$kDis
=
$this
->getKDis(
$tempNode
->getNodeName(),
$kdAndKnList
);
if
(
$kDis
<
$tempNode
->getDistance()) {
$tempNode
->setReachDis(
$tempNode
->getDistance());
}
else
{
$tempNode
->setReachDis(
$kDis
);
}
endforeach
;
endforeach
;
}
/**
* 获取某个点的k-距离(kDistance)
* @param nodeName
* @param nodeList
* @return
*/
private
function
getKDis(
$nodeName
,
$nodeList
) {
$kDis
= 0;
//for (DataNode node : nodeList) {
foreach
(
$nodeList
as
$node
):
if
(
$this
->strcomp(trim(
$nodeName
),trim(
$node
->getNodeName()))) {
$kDis
=
$node
->getkDistance();
break
;
}
endforeach
;
return
$kDis
;
}
private
function
strcomp(
$str1
,
$str2
){
if
(
$str1
==
$str2
){
return
TRUE;
}
else
{
return
FALSE;
}
}
/**
* 获取某个点的可达距离
* @param nodeName
* @param nodeList
* @return
*/
private
function
getRD(
$nodeName
,
$nodeList
) {
$kDis
= 0;
//for (DataNode node : nodeList) {
foreach
(
$nodeList
as
$node
):
//if (nodeName.trim().equals(node.getNodeName().trim())) {
if
(
$this
->strcomp(trim(
$nodeName
),trim(
$node
->getNodeName()))) {
$kDis
=
$node
->getReachDensity();
break
;
}
endforeach
;
return
$kDis
;
}
/**
* 计算给定点NodeA与其他点NodeB的欧几里得距离(distance),并找到NodeA点的前5位NodeB,然后记录到NodeA的k-领域(kNeighbor)变量。
* 同时找到NodeA的k距离,然后记录到NodeA的k-距离(kDistance)变量中。
* 处理步骤如下:
* 1,计算给定点NodeA与其他点NodeB的欧几里得距离,并记录在NodeB点的distance变量中。
* 2,对所有NodeB点中的distance进行升序排序。
* 3,找到NodeB点的前5位的欧几里得距离点,并记录到到NodeA的kNeighbor变量中。
* 4,找到NodeB点的第5位距离,并记录到NodeA点的kDistance变量中。
* @param allNodes
* @return List<Node>
*/
private
function
getKDAndKN(
$allNodes
) {
$kdAndKnList
=
array
();
for
(
$i
= 0 ;
$i
<
count
(
$allNodes
);
$i
++) {
$tempNodeList
=
array
();
$nodeA
=
new
DataNode(
$allNodes
[
$i
]->getNodeName(),
$allNodes
[
$i
]->getDimensioin());
//1,找到给定点NodeA与其他点NodeB的欧几里得距离,并记录在NodeB点的distance变量中。
for
(
$j
= 0;
$j
<
count
(
$allNodes
);
$j
++) {
$nodeB
=
new
DataNode(
$allNodes
[
$j
]->getNodeName(),
$allNodes
[
$j
]->getDimensioin());
//计算NodeA与NodeB的欧几里得距离(distance)
$tempDis
=
$this
->getDis(
$nodeA
,
$nodeB
);
$nodeB
->setDistance(
$tempDis
);
array_push
(
$tempNodeList
,
$nodeB
);
//$tempNodeList.add(nodeB);
}
//2,对所有NodeB点中的欧几里得距离(distance)进行升序排序。
$tempNodeList
=
$this
->sortArr(
$tempNodeList
);
$neighArr
=
array
();
for
(
$k
= 1;
$k
<= self::
$INT_K
;
$k
++) {
//3,找到NodeB点的前5位的欧几里得距离点,并记录到到NodeA的kNeighbor变量中。
array_push
(
$neighArr
,
$tempNodeList
[
$k
]);
if
(
$k
== self::
$INT_K
- 1) {
//4,找到NodeB点的第5位距离,并记录到NodeA点的kDistance变量中。
$nodeA
->setkDistance(
$tempNodeList
[
$k
]->getDistance());
}
}
$nodeA
->setkNeighbor(
$neighArr
);
array_push
(
$kdAndKnList
,
$nodeA
);
}
return
$kdAndKnList
;
}
/**
* 计算给定点A与其他点B之间的欧几里得距离。
* 欧氏距离的公式:
* d=sqrt( ∑(xi1-xi2)^2 ) 这里i=1,2..n
* xi1表示第一个点的第i维坐标,xi2表示第二个点的第i维坐标
* n维欧氏空间是一个点集,它的每个点可以表示为(x(1),x(2),...x(n)),
* 其中x(i)(i=1,2...n)是实数,称为x的第i个坐标,两个点x和y=(y(1),y(2)...y(n))之间的距离d(x,y)定义为上面的公式.
* @param A
* @param B
* @return
*/
private
function
getDis(
$A
,
$B
) {
$dis
= 0.0;
$dimA
=
$A
->getDimensioin();
$dimB
=
$B
->getDimensioin();
if
(
count
(
$dimA
) ==
count
(
$dimB
)) {
for
(
$i
= 0;
$i
<
count
(
$dimA
);
$i
++) {
$temp
= pow(
$dimA
[
$i
] -
$dimB
[
$i
], 2);
$dis
=
$dis
+
$temp
;
}
$dis
= pow(
$dis
, 0.5);
}
return
$dis
;
}
//Distance比较
private
function
compareAandB(
$arr
,
$A
,
$B
) {
if
((
$arr
[
$A
]->getDistance()-
$arr
[
$B
]->getDistance())<0)
return
-1;
else
if
((
$arr
[
$A
]->getDistance()-
$arr
[
$B
]->getDistance())>0)
return
1;
else
return
0;
}
//lof比较
private
function
compareAandBLof(
$arr
,
$A
,
$B
) {
if
((
$arr
[
$A
]->getLof()-
$arr
[
$B
]->getLof())<0)
return
-1;
else
if
((
$arr
[
$A
]->getLof()-
$arr
[
$B
]->getLof())>0)
return
1;
else
return
0;
}
private
function
changeAandB(
$arr
,
$A
,
$B
) {
$tempChange
=
$arr
[
$A
];
$arr
[
$A
] =
$arr
[
$B
];
$arr
[
$B
] =
$tempChange
;
return
$arr
;
}
//Distance升序
private
function
sortArr(
$arr
) {
for
(
$i
= 0;
$i
<
count
(
$arr
);
$i
++){
for
(
$j
=
$i
+ 1;
$j
<
count
(
$arr
);
$j
++){
if
(
$this
->compareAandB(
$arr
,
$i
,
$j
)>0){
$arr
=
$this
->changeAandB(
$arr
,
$i
,
$j
);
}
}
}
return
$arr
;
}
//lof降序
private
function
rsortArr(
$arr
) {
for
(
$i
= 0;
$i
<
count
(
$arr
);
$i
++){
for
(
$j
=
$i
+ 1;
$j
<
count
(
$arr
);
$j
++){
if
(
$this
->compareAandBLof(
$arr
,
$i
,
$j
)<0){
$arr
=
$this
->changeAandB(
$arr
,
$i
,
$j
);
}
}
}
return
$arr
;
}
public
static
function
main() {
$dpoints
=
array
();
$a
=
array
( 2, 3 );
$b
=
array
( 2, 4 );
$c
=
array
( 1, 4 );
$d
=
array
( 1, 3 );
$e
=
array
( 2, 2 );
$f
=
array
( 3, 2 );
$g
=
array
( 8, 7 );
$h
=
array
( 8, 6 );
$i
=
array
( 7, 7 );
$j
=
array
( 7, 6 );
$k
=
array
( 8, 5 );
$l
=
array
( 100, 2 );
// 孤立点
$m
=
array
( 8, 20 );
$n
=
array
( 8, 19 );
$o
=
array
( 7, 18 );
$p
=
array
( 7, 17 );
$yichen
=
array
( 8, 21 );
array_push
(
$dpoints
,
new
DataNode(
"a"
,
$a
));
array_push
(
$dpoints
,
new
DataNode(
"b"
,
$b
));
array_push
(
$dpoints
,
new
DataNode(
"c"
,
$c
));
array_push
(
$dpoints
,
new
DataNode(
"d"
,
$d
));
array_push
(
$dpoints
,
new
DataNode(
"e"
,
$e
));
array_push
(
$dpoints
,
new
DataNode(
"f"
,
$f
));
array_push
(
$dpoints
,
new
DataNode(
"g"
,
$g
));
array_push
(
$dpoints
,
new
DataNode(
"h"
,
$h
));
array_push
(
$dpoints
,
new
DataNode(
"i"
,
$i
));
array_push
(
$dpoints
,
new
DataNode(
"j"
,
$j
));
array_push
(
$dpoints
,
new
DataNode(
"k"
,
$k
));
array_push
(
$dpoints
,
new
DataNode(
"l"
,
$l
));
array_push
(
$dpoints
,
new
DataNode(
"m"
,
$m
));
array_push
(
$dpoints
,
new
DataNode(
"n"
,
$n
));
array_push
(
$dpoints
,
new
DataNode(
"o"
,
$o
));
array_push
(
$dpoints
,
new
DataNode(
"p"
,
$p
));
array_push
(
$dpoints
,
new
DataNode(
"yichen"
,
$yichen
));
$lof
=
new
OutlierNodeDetect();
$nodeList
=
$lof
->getOutlierNode(
$dpoints
);
foreach
(
$nodeList
as
$node
):
echo
(
$node
->getNodeName() .
"--"
.
round
(
$node
->getLof(),4));
echo
(
"<br>"
);
endforeach
;
}
}
OutlierNodeDetect::main();
?>
|
到此这篇关于PHP局部异常因子算法-Local Outlier Factor(LOF)算法的具体实现解析的文章就介绍到这了,更多相关PHP局部异常因子算法-Local Outlier Factor(LOF)算法内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/u013914886/article/details/70194318 。
最后此篇关于PHP局部异常因子算法-Local Outlier Factor(LOF)算法的具体实现解析的文章就讲到这里了,如果你想了解更多关于PHP局部异常因子算法-Local Outlier Factor(LOF)算法的具体实现解析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我想将引用保留为 Factor 中元组的成员。但是,当我尝试对其执行“调用”时,出现错误“无法将调用应用于运行时计算值”。请注意,将函数标记为“内联”不会执行任何操作。 示例代码: USING: ac
我最近看到 Matt Dowle 用 as.factor() 写了一些代码, 具体来说 for (col in names_factors) set(dt, j=col, value=as.facto
(所描述的行为是一个错误!) 我不明白 group_by 对作为因素的列和不是因素的列的影响。下面分析这两种可能性: library(dplyr) df % group_by(height) %>%
有没有人用 Factor 构建了一个 Web 应用程序? ?您在此过程中遇到了哪些绊脚石或问题? 最佳答案 试试 this . 关于factor-lang - 使用 Factor 构建 Web 应用程
例如,如果 CPU 有四个内核和八个内核线程。 我应该设置 核心池大小因子 一直设置到8? 的一般尺寸是多少?最大池大小因子 关于 核心池大小因子 ? 我提到的其他设置是否与 Akka 配置相关? 最
我正在尝试通过重新创建给我的数据结果来在 R 中实现 NB 分类器。现在我只是对训练数据本身进行测试,看看准确性如何。 数据集中有 29 个变量,其中一个称为“状态”。它有两个值:Win 和 Lose
当一个可被另一个整除时,数字的位之间是否存在任何关系? 36位与9位或4位或12位、10位(1010)与5位(101)、21位(10101)与7位(00111)的位序列有什么关系? 谢谢。如果有些句子
我学会了一种叫做“线性筛”的算法https://cp-algorithms.com/algebra/prime-sieve-linear.html能够在线性时间内得到所有小于 N 的素数。 这个算法有
我正在使用 R 中的一个数据集,它带有一个代码本,它基本上告诉我因子变量的不同级别的标签应该是什么。例如,使用密码本,我可以看到在我的“性别”变量中,0 是“女性”,1 是“男性”。我正在使用此信息相
我试图将数据集分成具有因子变量和非因子变量的部分。 我正在做类似的事情: 这部分工作: factorCols <- sapply(df1, is.factor) factorDf <- df1[,fa
我有以下设置。 df <- data.frame(aa = rnorm(1000), bb = rnorm(1000)) apply(df, 2, typeof) # aa bb
我现在正在自学 R。我正在尝试使用以下内容将整数变量转换为分类变量。 train[, c("Store", "DayOfWeek")] str(mtcars) 'data.frame': 32
我正在玩 Factor,试图对串联编程有一点了解。写一个词来平方一个数字是微不足道的: : square ( n -- n ) dup * ; 但对于我来说,我似乎无法弄清楚如何计算一个数字的立方:
给定一个数 x,我如何找到两个数 y 和 z,这样 x = y * z 和 y==Z 或者 y 和 z 彼此接近?此外,x、y、z 都是整数。 示例: x = 16484, y=z=128; x=
我有以下设置。 df <- data.frame(aa = rnorm(1000), bb = rnorm(1000)) apply(df, 2, typeof) # aa bb
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
如果你觉得这听起来很傻,我提前道歉,我是 splunk 的新手,并且学过 udemy 类(class),但无法弄清楚这一点。 If I check my indexes.conf file in cl
为什么此代码:as.factor(c("\U201C", '"3', "1", "2", "\U00B5")),在每个操作系统上返回因子级别的不同顺序? 在Linux上: > as.factor(c(
我是一名优秀的程序员,十分优秀!