技术标签: apache 学习笔记 kmeans算法 math dbscan算法
使用apache common math 中的聚类方法
1)DBSCAN的使用
public class DBSCAN {
/**
* �����inputPathΪjaccardCoding.txt·��
*/
public static final String inputPath = "D:\\jaccardCoding.txt";
static List<Location> locations = new ArrayList<>();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DBSCAN dbscan = new DBSCAN();
Map<NodePair, Double> nodesPairMap = dbscan.getCodingFileMap(inputPath);
dbscan.getDBSCANResult(locations, nodesPairMap,0.5,10);
}
/**
* @author YYH
* @param locations �ڵ���
* @param nodesPairMap Ȩ��ӳ��
* @param eps the distance that defines the ��-neighborhood of a point
* @param minPts the minimum number of density-connected points required to form a cluster
*/
public void getDBSCANResult(
List<Location> locations,
Map<NodePair, Double> nodesPairMap,
double eps,
int minPts){
List<LocationWrapper> clusterInput = new ArrayList<LocationWrapper>(locations.size());
for (Location location : locations){
clusterInput.add(new LocationWrapper(location));
}
// initialize a new clustering algorithm.
// we use KMeans++ with 10 clusters and 10000 iterations maximum.
// we did not specify a distance measure; the default (euclidean distance) is used.
JaccardDistance jaccardDistance = new JaccardDistance(nodesPairMap);
DBSCANClusterer<LocationWrapper> clusterer = new DBSCANClusterer<LocationWrapper>(eps, minPts,jaccardDistance);
List<Cluster<LocationWrapper>> clusterResults = clusterer.cluster(clusterInput);
// output the clusters
for (int i=0; i<clusterResults.size(); i++){
System.out.println("Cluster " + i);
for (LocationWrapper locationWrapper : clusterResults.get(i).getPoints()){
System.out.println(locationWrapper.getLocation());
}
System.out.println();
}
}
/**
* ��ȡjaccardCoding.txt�ļ��������locations��Map(�������jaccardҪ��)
* @param inputPath
* @return
* @throws IOException
*/
public Map<NodePair, Double> getCodingFileMap(String inputPath) throws IOException{
BufferedReader bReader = FileUtil.getReader(inputPath);
Map<NodePair, Double> nodesPairMap = new HashMap<>();
Set<Location> locationSet = new HashSet<>();
String txtLine = "";
while ((txtLine = bReader.readLine())!=null){
String[] values = txtLine.split("\t");
NodePair nodePair = new NodePair();
nodePair.setNode1(values[0]);
nodePair.setNode2(values[1]);
nodesPairMap.put(nodePair, Double.valueOf(values[2]));
locationSet.add( new Location(Double.valueOf(values[0]))); //����ֻ�ǰѽڵ㻻��String2double���͵�
locationSet.add( new Location(Double.valueOf(values[1]))); //����ֻ�ǰѽڵ㻻��String2double���͵�
}
locations.addAll(locationSet);
if(bReader != null){
bReader.close();
}
return nodesPairMap;
}
/**
* @author YYH
* �ı���DistanceMeasure,�����������Լ��Ĺ��캯�������һ����Լ����ڲ��࣬����ô������ѽ
* ��jaccardϵ���ļ���ת��һ�£�����jaccard����
*
*/
public static class JaccardDistance implements DistanceMeasure{
private static final long serialVersionUID = 1L;
public static final String outPath = "D:\\Test.txt";
public Map<NodePair, Double> nodesPairMap;
public JaccardDistance(Map<NodePair, Double> nodesPairMap){
this.nodesPairMap = nodesPairMap;
}
public JaccardDistance(){
}
/**
* ���������һЩ��Ϣ���ò�����ʱ����Ե���
*/
@Override
public double compute(double[] a, double[] b) throws DimensionMismatchException {
BufferedWriter bwriter = FileUtil.getWriter(outPath);
double value = 0.;
String node1 = String.valueOf(a[0]);
String node2 = String.valueOf(b[0]);
node1 = node1.substring(0, node1.indexOf('.'));
node2 = node2.substring(0, node2.indexOf('.'));
NodePair nodePair = new NodePair(node1,node2);
if(nodesPairMap.containsKey(nodePair)){
value= nodesPairMap.get(nodePair);
}
System.out.println(node1+" : "+node2+" : "+value);
try {
bwriter.write(node1+" : "+node2+" : "+value);
bwriter.newLine();
bwriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bwriter != null){
try {
bwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return value;
}
}
/**
* @author YYH
* Location �ڵ�ı�ţ����߱�ǣ�
* points�ڵ���Ϣ�����ھ���ļ���
*
*/
public static class LocationWrapper implements Clusterable {
private double[] points;
private Location location;
public LocationWrapper(Location location) {
this.location = location;
this.points = new double[] { location.getX() };
}
public Location getLocation() {
return location;
}
public double[] getPoint() {
return points;
}
}
}
2)Kmeans++的使用
/**
* @author YYH
*
*/
public class KmeansPlusPlus {
/**
* �����inputPathΪjaccardCoding.txt·��
*/
public static final String inputPath = "D:\\jaccardCoding.txt";
static List<Location> locations = new ArrayList<>();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
KmeansPlusPlus kmeans = new KmeansPlusPlus();
Map<NodePair, Double> nodesPairMap = kmeans.getCodingFileMap(inputPath);
kmeans.getKMeansResult(locations, nodesPairMap,2,10000);
}
/**
* @author Administrator
* @param locations �ڵ���
* @param nodesPairMap Ȩ��ӳ��
* @param classNumber Ҫ���ֵ�����
* @param maxIterations ���Ҫ�����Ĵ���
*/
public void getKMeansResult(
List<Location> locations,
Map<NodePair, Double> nodesPairMap,
int classNumber,
int maxIterations){
List<LocationWrapper> clusterInput = new ArrayList<LocationWrapper>(locations.size());
for (Location location : locations){
clusterInput.add(new LocationWrapper(location));
}
// initialize a new clustering algorithm.
// we use KMeans++ with 10 clusters and 10000 iterations maximum.
// we did not specify a distance measure; the default (euclidean distance) is used.
JaccardDistance jaccardDistance = new JaccardDistance(nodesPairMap);
KMeansPlusPlusClusterer<LocationWrapper> clusterer = new KMeansPlusPlusClusterer<LocationWrapper>(classNumber, maxIterations,jaccardDistance);
List<CentroidCluster<LocationWrapper>> clusterResults = clusterer.cluster(clusterInput);
// output the clusters
for (int i=0; i<clusterResults.size(); i++){
System.out.println("Cluster " + i);
for (LocationWrapper locationWrapper : clusterResults.get(i).getPoints()){
System.out.println(locationWrapper.getLocation());
}
System.out.println();
}
}
/**
* ��ȡjaccardCoding.txt�ļ��������locations��Map(�������jaccardҪ��)
* @param inputPath
* @return
* @throws IOException
*/
public Map<NodePair, Double> getCodingFileMap(String inputPath) throws IOException{
BufferedReader bReader = FileUtil.getReader(inputPath);
Map<NodePair, Double> nodesPairMap = new HashMap<>();
Set<Location> locationSet = new HashSet<>();
String txtLine = "";
while ((txtLine = bReader.readLine())!=null){
String[] values = txtLine.split("\t");
NodePair nodePair = new NodePair();
nodePair.setNode1(values[0]);
nodePair.setNode2(values[1]);
nodesPairMap.put(nodePair, Double.valueOf(values[2]));
locationSet.add( new Location(Double.valueOf(values[0]))); //����ֻ�ǰѽڵ㻻��String2double���͵�
locationSet.add( new Location(Double.valueOf(values[1]))); //����ֻ�ǰѽڵ㻻��String2double���͵�
}
locations.addAll(locationSet);
if(bReader != null){
bReader.close();
}
return nodesPairMap;
}
/**
* @author YYH
* �ı���DistanceMeasure,�����������Լ��Ĺ��캯�������һ����Լ����ڲ��࣬����ô������ѽ
* ��jaccardϵ���ļ���ת��һ�£�����jaccard����
*
*/
public static class JaccardDistance implements DistanceMeasure{
private static final long serialVersionUID = 1L;
public static final String outPath = "D:\\Test.txt";
public Map<NodePair, Double> nodesPairMap;
public JaccardDistance(Map<NodePair, Double> nodesPairMap){
this.nodesPairMap = nodesPairMap;
}
public JaccardDistance(){
}
/**
* ���������һЩ��Ϣ���ò�����ʱ����Ե���
*/
@Override
public double compute(double[] a, double[] b) throws DimensionMismatchException {
BufferedWriter bwriter = FileUtil.getWriter(outPath);
double value = 0.;
String node1 = String.valueOf(a[0]);
String node2 = String.valueOf(b[0]);
node1 = node1.substring(0, node1.indexOf('.'));
node2 = node2.substring(0, node2.indexOf('.'));
NodePair nodePair = new NodePair(node1,node2);
if(nodesPairMap.containsKey(nodePair)){
value= nodesPairMap.get(nodePair);
}
System.out.println(node1+" : "+node2+" : "+value);
try {
bwriter.write(node1+" : "+node2+" : "+value);
bwriter.newLine();
bwriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(bwriter != null){
try {
bwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return value;
}
}
/**
* @author YYH
* Location �ڵ�ı�ţ����߱�ǣ�
* points�ڵ���Ϣ�����ھ���ļ���
*
*/
public static class LocationWrapper implements Clusterable {
private double[] points;
private Location location;
public LocationWrapper(Location location) {
this.location = location;
this.points = new double[] { location.getX() };
}
public Location getLocation() {
return location;
}
public double[] getPoint() {
return points;
}
}
}
文章浏览阅读1.2k次。其实,在真正的项目实战当中如果仅仅是使用硬盘缓存的话,程序是有明显短板的。而如果只使用内存缓存的话,程序当然也会有很大的缺陷。因此,一个优秀的程序必然会将内存缓存和硬盘缓存结合到一起使用,那么本篇文章我们就来看一看,如何才能将LruCache和DiskLruCache完美结合到一起。在 Android照片墙应用实现,再多的图片也不怕崩溃 这篇文章当中,我编写了一个照片墙的应用程序,但当时只_lrucache和disklrucached的照片墙
文章浏览阅读2.8k次,点赞3次,收藏8次。html:<div id="industrySectorScroll" :style="{ height: screenHeight + 'px' }"></div>js: export default { name: "industrySectorScroll", data() { return { screenWeight: 0, // 屏幕宽度 screenHeight: 0, // 屏幕高度 _vue给div赋值高度
文章浏览阅读2.6w次,点赞17次,收藏49次。简单的说就是两种不同的语言.但是它们之间既有联系又有区别_java和c的区别
文章浏览阅读4.1k次。Springboot整合swagger2时后,访问http://localhost:8080/swagger-ui.html时出现空白页如下我的swagger2版本为2.7.0。Swagger2的maven版本为2.7.0<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <versio_配置swagger2 解决访问/swagger-ui.html页面空白
文章浏览阅读2.2k次。--- a/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java+++ b/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/sy..._android 开机后首次状态栏下拉偏移
文章浏览阅读1w次。错误情况项目用 maven 打好 war 包后放到 tomcat 下,启动 tomcat,出现以下错误3-Nov-2017 12:21:44.346 严重 [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start compone_error in opening zip file
文章浏览阅读1.9k次,点赞6次,收藏9次。官网共11个表– 下载地址:http://www.quartz-scheduler.org/downloads/注意:创建,删除表要按顺序,涉计到外键约束drop table if exists qrtz_fired_triggers; -- 1 保存已经触发的触发器状态信息drop table if exists qrtz_paused_trigger_grps; -- 2 存放暂停掉的触发器表表drop table if exists qrtz_scheduler_state; _quartz 几张表quartz state
文章浏览阅读1.1k次。错误情形:错误原因:未知解决方法:去官网下载最新的jar包_idea导入mysql依赖标红
文章浏览阅读812次。原文地址:https://www.xuebuyuan.com/1722272.html一、基本概念一个视频图像可编码成一个或更多个条带,每个条带包含整数个宏块(MB),即每个条带至少一个MB,最多时每个条带包含整个图像的宏块。总之,一幅图像中每个条带的宏块数不一定固定。设条带的目的是为了限制误码的扩散和传输,应使编码条带相互间是独立的。某个条带的预测不能以其它条带中的宏块为参考图像,这样某一条带中的预测误差才不会传播到其它条带中去。Slice的类型如表1所示表1 Slice的类型说明_slice type
文章浏览阅读363次。 进程知识库 divx - divx.exe - 进程信息进程文件: divx 或者 divx.exe进程名称: Divx 描述:divx.exe被确认为是MASTAK病毒。这个文件建议终止。 出品者: 未知N/A属于: MASTAK virus系统进程: 否后台程序: 是使用_pcsvc.exe应用程序错误
文章浏览阅读1.5k次,点赞2次,收藏2次。Mac 安装 OpenCV 及示例程序安装OpenCV安装 Xcode安装Homebrew安装Python安装OpenCV安装 Xcode直接在APP Store中下载安装即可sudo xcodebuild -licensesudo xcode-select --install安装Homebrew≈Mac上的apt-get参考 Mac API那篇文章,那里已经安装了安装完可以用..._mac 安装openvap指定版本
文章浏览阅读617次。macOS+linux+windows+ios+android(QtCreator)系统:macos开发工具:Qt creator开发语言:C++ python下载地址:https://www.qt.io/zh-cn/download选择开源版macOS+linux+windows+ios+android(QtCreator)系统:macos开发工具:Qt creator开发语言:C++ python下载地址:https://www.qt.io/zh-..._macos10.13 qt-unified-mac-x64-online