OPTICS光学算法
package com.my.optics; public class DataPoint {
private String name;//样本的名字
private double dimensioin[];//样本点的维度
private double coreDistance;//核心距离
private double reachableDistance;//可达距离
public DataPoint() {
}
public DataPoint(DataPoint e) {
this.name = e.name;
this.dimensioin = e.dimensioin;
this.coreDistance = e.coreDistance;
this.reachableDistance = e.reachableDistance;
}
public DataPoint(double dimensioin[],String name) {
this.name = name;
this.dimensioin = dimensioin;
this.coreDistance = -1;
this.reachableDistance = -1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double[] getDimensioin() {
return dimensioin;
}
public void setDimensioin(double[] dimensioin) {
this.dimensioin = dimensioin;
}
public double getCoreDistance() {
return coreDistance;
}
public void setCoreDistance(double coreDistance) {
this.coreDistance = coreDistance;
}
public double getReachableDistance() {
return reachableDistance;
}
public void setReachableDistance(double reachableDistance) {
this.reachableDistance = reachableDistance;
}
} package com.my.optics; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class ClusterAnalysis {
class ComparatorDp implements Comparator<DataPoint> { @Override
public int compare(DataPoint o1, DataPoint o2) {
double temp = o1.getReachableDistance() - o2.getReachableDistance();
int a = 0;
if(temp<0) {
a = -1;
}else {
a = 1;
}
return a;
}
}
public List<DataPoint> startAnalysis(List<DataPoint> dataPoints,double radius,int ObjectNum) {
List<DataPoint> dpList = new ArrayList<DataPoint>();
List<DataPoint> dpQue = new ArrayList<DataPoint>();
int total = 0;
while (total < dataPoints.size()) {
if (isContainedInList(dataPoints.get(total), dpList) == -1 ) {
List<DataPoint> tmpDpList = isKeyAndReturnObjects(dataPoints.get(total),
dataPoints, radius, ObjectNum);
if(tmpDpList != null && tmpDpList.size() > 0){
DataPoint newDataPoint=new DataPoint(dataPoints.get(total));
dpQue.add(newDataPoint);
}
}
while (!dpQue.isEmpty()) {
DataPoint tempDpfromQ = dpQue.remove(0);
DataPoint newDataPoint=new DataPoint(tempDpfromQ);
dpList.add(newDataPoint);
List<DataPoint> tempDpList = isKeyAndReturnObjects(tempDpfromQ,dataPoints, radius, ObjectNum);
System.out.println(newDataPoint.getName()+":"+newDataPoint.getReachableDistance());
if (tempDpList != null && tempDpList.size() > 0) {
for (int i = 0; i < tempDpList.size(); i++) {
DataPoint tempDpfromList = tempDpList.get(i);
int indexInList = isContainedInList(tempDpfromList,dpList);
int indexInQ = isContainedInList(tempDpfromList, dpQue);
if (indexInList == -1) {
if (indexInQ > -1) {
int index = -1;
for (DataPoint dataPoint : dpQue) {
index++;
if (index == indexInQ) {
if (dataPoint.getReachableDistance() > tempDpfromList.getReachableDistance()) {
dataPoint.setReachableDistance(tempDpfromList.getReachableDistance());
}
}
}
} else {
dpQue.add(new DataPoint(tempDpfromList));
}
}
}
// TODO:对Q进行重新排序
Collections.sort(dpQue, new ComparatorDp());
}
}
System.out.println("------");
total++;
}
return dpList;
}
public void displayDataPoints(List<DataPoint> dps){
for(DataPoint dp: dps){
System.out.println(dp.getName()+":"+dp.getReachableDistance());
}
}
private int isContainedInList(DataPoint dp, List<DataPoint> dpList) {
int index = -1;
for (DataPoint dataPoint : dpList) {
index++;
if (dataPoint.getName().equals(dp.getName())) {
return index;
}
}
return -1;
}
private List<DataPoint> isKeyAndReturnObjects(DataPoint dataPoint,List<DataPoint> dataPoints,double radius,int ObjectNum){
List<DataPoint> arrivableObjects=new ArrayList<DataPoint>();
//用来存储所有直接密度可达对象
List<Double> distances=new ArrayList<Double>();
//欧几里得距离
double coreDistance;
//核心距离
for (int i = 0; i < dataPoints.size(); i++) {
DataPoint dp = dataPoints.get(i);
double distance = getDistance(dataPoint, dp);
if (distance <= radius) {
distances.add(distance);
arrivableObjects.add(dp);
}
}
if(arrivableObjects.size()>=ObjectNum){
List<Double> newDistances=new ArrayList<Double>(distances);
Collections.sort(distances);
coreDistance=distances.get(ObjectNum-1);
for(int j=0;j<arrivableObjects.size();j++){
if (coreDistance > newDistances.get(j)) {
if(newDistances.get(j)==0){
dataPoint.setReachableDistance(coreDistance);
}
arrivableObjects.get(j).setReachableDistance(coreDistance);
}else{
arrivableObjects.get(j).setReachableDistance(newDistances.get(j));
}
}
return arrivableObjects;
} return null;
}
private double getDistance(DataPoint dp1,DataPoint dp2){
double distance=0.0;
double[] dim1=dp1.getDimensioin();
double[] dim2=dp2.getDimensioin();
if(dim1.length==dim2.length){
for(int i=0;i<dim1.length;i++){
double temp=Math.pow((dim1[i]-dim2[i]), 2);
distance=distance+temp;
}
distance=Math.pow(distance, 0.5);
return distance;
}
return distance;
}
public static void main(String[] args){
List<DataPoint> dpoints = new ArrayList<DataPoint>();
double[] a={2,3};
double[] b={2,4};
double[] c={1,4};
double[] d={1,3};
double[] e={2,2};
double[] f={3,2};
double[] g={8,7};
double[] h={8,6};
double[] i={7,7};
double[] j={7,6};
double[] k={8,5};
double[] l={100,2};//孤立点
double[] m={8,20};
double[] n={8,19};
double[] o={7,18};
double[] p={7,17};
double[] q={8,21};
dpoints.add(new DataPoint(a,"a"));
dpoints.add(new DataPoint(b,"b"));
dpoints.add(new DataPoint(c,"c"));
dpoints.add(new DataPoint(d,"d"));
dpoints.add(new DataPoint(e,"e"));
dpoints.add(new DataPoint(f,"f"));
dpoints.add(new DataPoint(g,"g"));
dpoints.add(new DataPoint(h,"h"));
dpoints.add(new DataPoint(i,"i"));
dpoints.add(new DataPoint(j,"j"));
dpoints.add(new DataPoint(k,"k"));
dpoints.add(new DataPoint(l,"l"));
dpoints.add(new DataPoint(m,"m"));
dpoints.add(new DataPoint(n,"n"));
dpoints.add(new DataPoint(o,"o"));
dpoints.add(new DataPoint(p,"p"));
dpoints.add(new DataPoint(q,"q"));
ClusterAnalysis ca=new ClusterAnalysis();
List<DataPoint> dps=ca.startAnalysis(dpoints, 2, 4);
ca.displayDataPoints(dps);
}
}
说是这个算法我也不是太明白,因为涉及到数学上的知识我就不做太多的描述了
OPTICS光学算法的更多相关文章
- OPTICS聚类算法原理
OPTICS聚类算法原理 基础 OPTICS聚类算法是基于密度的聚类算法,全称是Ordering points to identify the clustering structure,目标是将空间中 ...
- 【Python机器学习实战】聚类算法(2)——层次聚类(HAC)和DBSCAN
层次聚类和DBSCAN 前面说到K-means聚类算法,K-Means聚类是一种分散性聚类算法,本节主要是基于数据结构的聚类算法--层次聚类和基于密度的聚类算法--DBSCAN两种算法. 1.层次聚类 ...
- 应用.Net+Consul维护RabbitMq的高可用性
懒人学习的过程就是工作中老大让干啥让做啥就研究研究啥,国庆放假回来的周末老大通过钉钉给我布置了个任务, RabbitMQ高可用解决方案,我想说钉钉太坑了: 这是国庆过后9号周日晚上下班给的任务,我周一 ...
- tmp
Hello 大家好,这次给大家带来的是Gear VR4代,首先我得感谢下我们的虎友Hide兄弟友情提供Gear给我们测评,感谢 感谢.之前我录的前哨战也说过,这次Gear VR 4代较3代变化并不是很 ...
- English_word_learning
这次报名参加了学院的21天打卡活动,说实话,也是想给自己一个积累的平台. 毕竟,真的有时候感觉挺弱的 有的人用了一年考完了四六级,而有人却用四年还未考完. 听到有一位学长因为自己的四级成绩没有达到48 ...
- 收藏单词TOEFL备份托福英语
TOEFL托福词汇串讲(文本) alchemy(chem-化学)n. 炼金术 chemistry 化学 alder 赤杨树 联想:older 老人坐在赤杨树下 sloth 树懒 algae n.海藻 ...
- Electromagnetic
1. 电磁辐射 2. 电磁频谱 3. 可见光 4. 微波 5. 更多相关链接 1. 电磁辐射 https://en.wikipedia.org/wiki/Electromagnetic_radiati ...
- LeetCode 56,57,60,连刷三题不费劲
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第34篇文章,刚好接下来的题目比较简单,很多和之前的做法类似.所以我们今天出一个合集,一口气做完接下来的57.5 ...
- Streamlit:快速数据可视化界面工具
目录 Streamlit简介 Streamlit使用指南 常用命令 显示文本 显示数据 显示图表 显示媒体 交互组件 侧边栏 缓存机制 Streamlit使用Hack Streamlit的替代品 相关 ...
随机推荐
- 【模拟,时针分针秒针两两夹角】【没有跳坑好兴奋】hdu - 5387 (多校#8 1008)
算是最好写的一道题了吧,最近模拟没手感,一次过也是很鸡冻o(* ̄▽ ̄*)o 注意事项都在代码里,没有跳坑也不清楚坑点在哪~ #include<cstdio> #include<cst ...
- poj 1987 树的分治
思路:1741的A1送 1. #include<iostream> #include<cstring> #include<algorithm> #include&l ...
- poj 2983 差分约束
思路: 设dis[i]为标号为i的点到0号点的距离.对于P A B X,我们能得到等式dis[a]-dis[b]=x,那么可以化为两个不等式dis[a]-dis[b]>=x和dis[b]-dis ...
- P2184 贪婪大陆
P2184 贪婪大陆 题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前 ...
- less-1
说在前面的话,为什么用less: 1.需要编写的代码明显变少了 2.css管理更加容易 3.less学习成本低 4.使用less实现配色变得非常容易 5.兼容css3,实现各个浏览器中css的兼容写法 ...
- Linux 命令 - ping: 向网络主机发送 ICMP ECHO_REQUEST 包
ping 命令会向指定的网络主机发送特殊网络数据报 IMCP ECHO_REQUEST.多数网络设备收到该数据包后会做出回应,通过此法即可验证网络连接是否正常. 有时从安全角度出发,通常会配置部分网络 ...
- kafka集群安装与配置
一.集群安装 1. Kafka下载: 可以从kafka官方网站(http://kafka.apache.org)上找到下载地址,再wgetwget http://mirrors.cnnic.cn/ap ...
- Java Thread UncaughtExceptionHandler
有没有发生过这样的情况,你写的工作线程莫名其妙的挂了,如果不是被你刚好看到,拿只能抓瞎了,不知道啥原因了,因为异常的时候只会把stack trace打在控制台上,不会记在你想记得错误日志里,头皮都抓破 ...
- OC6-网址分割
// // HtmlManger.h // OC6-网址分割 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng ...
- 函数 sort,unique,stable_sort,count_if,谓词
bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...