java k means_基于K-means 切割多边形 JAVA实现

基于K-means 切割多边形 JAVA实现

思路初稿详见多边形等分

依赖

geotools

ekmeans

org.locationtech.jts

jts-core

1.16.0

junit

junit

4.12

test

org.geotools

gt-shapefile

${geotools.version}

org.geotools

gt-main

${geotools.version}

org.geotools

gt-swing

${geotools.version}

org.geotools

gt-geojson

${geotools.version}

org.geotools

gt-geometry

${geotools.version}

com.vividsolutions

jts

1.13

org.geotools

gt-geojson

${geotools.version}

ca.pjer

ekmeans

2.0.0

compile

实现过程

结果类 KmeanPolygonResult

package com.huifer.planar.asEt.entity;

import java.util.ArrayList;

import java.util.List;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import org.locationtech.jts.geom.Geometry;

import org.locationtech.jts.geom.Point;

import org.locationtech.jts.geom.Polygon;

/**

*

Title : KmeanPolygonResult

*

Description : Kmean+泰森多边形平分多边形结果

*

* @author huifer

* @date 2019-01-16

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class KmeanPolygonResult {

/**

* 原始平面

*/

private Polygon polygon;

/**

* 随机点集合

*/

private ArrayList pointList;

/**

* 分组后组id

*/

private int[] assignments;

/**

* 中心集合

*/

private double[][] centroids;

/**

* xlist

*/

private ArrayList xlist;

/**

* ylist

*/

private ArrayList ylist;

/**

* 构造泰森多边形

*/

private List voronoi;

}

k-means 聚合类

package com.huifer.planar.asEt.utils;

import ca.pjer.ekmeans.EKmeans;

import lombok.Data;

/**

*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
K-means是一种聚类算法,可以通过Java实现。以下是简单的Java代码示例: 首先,定义一个Cluster类表示聚类中心: ``` public class Cluster { private List<Point> points; private Point centroid; public Cluster(Point centroid) { this.centroid = centroid; this.points = new ArrayList<>(); } public List<Point> getPoints() { return points; } public void addPoint(Point point) { points.add(point); } public void clear() { points.clear(); } public Point getCentroid() { return centroid; } public void setCentroid(Point centroid) { this.centroid = centroid; } } ``` 定义一个Point类表示数据点: ``` public class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public double distance(Point other) { double dx = x - other.getX(); double dy = y - other.getY(); return Math.sqrt(dx * dx + dy * dy); } } ``` 然后实现KMeans类: ``` public class KMeans { private int k; private List<Point> points; private List<Cluster> clusters; private Random random; public KMeans(int k, List<Point> points) { this.k = k; this.points = points; this.clusters = new ArrayList<>(); this.random = new Random(); } public void run() { // 初始化聚类中心 for (int i = 0; i < k; i++) { Point centroid = points.get(random.nextInt(points.size())); Cluster cluster = new Cluster(centroid); clusters.add(cluster); } boolean converged = false; while (!converged) { // 分配数据点到最近的聚类中心 for (Point point : points) { Cluster closest = null; double minDistance = Double.MAX_VALUE; for (Cluster cluster : clusters) { double distance = point.distance(cluster.getCentroid()); if (distance < minDistance) { closest = cluster; minDistance = distance; } } closest.addPoint(point); } // 更新聚类中心 converged = true; for (Cluster cluster : clusters) { Point oldCentroid = cluster.getCentroid(); Point newCentroid = calculateCentroid(cluster.getPoints()); if (!oldCentroid.equals(newCentroid)) { cluster.setCentroid(newCentroid); cluster.clear(); converged = false; } } } } private Point calculateCentroid(List<Point> points) { double sumX = 0; double sumY = 0; for (Point point : points) { sumX += point.getX(); sumY += point.getY(); } double centerX = sumX / points.size(); double centerY = sumY / points.size(); return new Point(centerX, centerY); } public List<Cluster> getClusters() { return clusters; } } ``` 使用方法如下: ``` List<Point> points = Arrays.asList( new Point(1, 1), new Point(1, 2), new Point(2, 2), new Point(8, 8), new Point(9, 8), new Point(8, 9) ); KMeans kMeans = new KMeans(2, points); kMeans.run(); List<Cluster> clusters = kMeans.getClusters(); for (Cluster cluster : clusters) { System.out.println(cluster.getCentroid()); for (Point point : cluster.getPoints()) { System.out.println(" " + point); } } ``` 输出结果如下: ``` (1.3333333333333333, 1.6666666666666667) (1.0, 1.0) (1.0, 2.0) (2.0, 2.0) (8.666666666666666, 8.333333333333334) (8.0, 8.0) (9.0, 8.0) (8.0, 9.0) ``` 其中,第一组数据点被分到了一个聚类中心,第二组数据点被分到了另一个聚类中心。这就是K-means聚类的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值