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
| package com.redbean.noveldata;
import lombok.Data;
import java.util.*;
public class kMeansTest {
public static void main(String[] args) { ArrayList<float[]> dataSet = new ArrayList<float[]>(); dataSet.add(new float[] {1000025, 434464, 24143}); dataSet.add(new float[] {1000060, 557784, 62171}); dataSet.add(new float[] { 1, 4, 4}); dataSet.add(new float[] { 2, 6, 5}); dataSet.add(new float[] { 3, 9, 6}); dataSet.add(new float[] { 4, 5, 4}); dataSet.add(new float[] { 5, 4, 2}); dataSet.add(new float[] { 6, 9, 7}); dataSet.add(new float[] { 7, 9, 8}); dataSet.add(new float[] { 8, 2, 10}); dataSet.add(new float[] { 9, 9, 12}); dataSet.add(new float[] { 10, 8, 112}); dataSet.add(new float[] { 11, 8, 4}); KMeans kMeans = new KMeans(3, dataSet); Set<Cluster> run = kMeans.run(); System.out.println("迭代次数:" + kMeans.getIterRunTimes()); for (Cluster cluster : run) { System.out.println(cluster.getId()); System.out.println(cluster.getCenter()); System.out.println(cluster.getMembers()); System.out.println("===================="); } } }
@Data class Point { private float[] novelInfo; private float Id; private int clusterId; private float dist;
public Point(float Id, float[] novelInfo) { this.novelInfo = novelInfo; this.Id = Id; }
public Point(float[] novelInfo) { this.Id = -1; this.novelInfo = novelInfo; } }
@Data class Cluster { private int id; private Point center; private List<Point> members = new ArrayList<>();
public Cluster(int id, Point center) { this.id = id; this.center = center; }
public Cluster(int id, Point center, List<Point> members) { this.id = id; this.center = center; this.members = members; }
public void addPoint(Point newPoint) { if (!members.contains(newPoint)) { members.add(newPoint); } else { System.out.println("<<<<<<<<<<<<<<<<<< 样本数据{" + newPoint + "} 已经存在>>>>>>>>>>>>>>>>>>>>>>>"); } } }
class DistanceCompute { public double getEuclideanDis(Point point1, Point point2) { float[] novelInfo1 = point1.getNovelInfo(); float[] novelInfo2 = point2.getNovelInfo(); double dist_temp = 0;
for (int i = 0; i < novelInfo1.length; i++) { dist_temp += Math.pow(novelInfo1[i] - novelInfo2[i], 2); } return Math.sqrt(dist_temp); } }
class KMeans { private int kNum; private final int ITER_SUM = 10;
private final int ITER_MAX_TIMES = 1000000000; private int iterRunTimes = 0; private final float ITER_STOP = (float) 0.01;
private List<float[]> novelData = null; private static List<Point> pointList = null; private DistanceCompute distanceCompute = new DistanceCompute(); private int len = 0;
public KMeans(int kNum, List<float[]> novelData) { this.kNum = kNum; this.novelData = novelData; init(); }
private void init() { pointList = new ArrayList<Point>(); len = novelData.get(0).length - 1; int tmp_len = novelData.get(0).length - 1; for (int i = 0, j = novelData.size(); i < j; i++) { float[] tmp = new float[tmp_len];; for (int f = 0; f < tmp_len; f++) { tmp[f] = novelData.get(i)[f + 1]; } pointList.add(new Point(novelData.get(i)[0], tmp)); } }
public Set<Cluster> chooseCenterCluster() { HashSet<Cluster> clusterHashSet = new HashSet<Cluster>(); Random random = new Random(); for (int id = 0; id < kNum; ) { Point point = pointList.get(random.nextInt(pointList.size())); boolean flag = true; for (Cluster cluster : clusterHashSet) { if (cluster.getCenter().equals(point)) { flag = false; } } if (flag) { Cluster cluster = new Cluster(id, point); clusterHashSet.add(cluster); id++; } } return clusterHashSet; }
public void cluster(Set<Cluster> clusterSet) { for (Point point : pointList) { float min_dis = Integer.MAX_VALUE; for (Cluster cluster : clusterSet) { float tmp_dis = (float) Math.min(distanceCompute.getEuclideanDis(point, cluster.getCenter()), min_dis); if (tmp_dis != min_dis) { min_dis = tmp_dis; point.setClusterId(cluster.getId()); point.setDist(min_dis); } } } for (Cluster cluster : clusterSet) { cluster.getMembers().clear(); for (Point point : pointList) { if (point.getClusterId() == cluster.getId()) { cluster.addPoint(point); } } } }
public boolean getUpdate(Set<Cluster> hashSet) { boolean isNeedIter = false; for (Cluster cluster : hashSet) { List<Point> members = cluster.getMembers(); float[] sumAll = new float[len]; for (int i = 0; i < len; i++) { for (int j = 0; j < members.size(); j++) { sumAll[i] += members.get(j).getNovelInfo()[i]; } } for (int i = 0; i < len; i++) { sumAll[i] = (float) sumAll[i] / members.size(); }
if (distanceCompute.getEuclideanDis(cluster.getCenter(), new Point(sumAll)) > ITER_STOP) { isNeedIter = true; } cluster.setCenter(new Point(sumAll)); } return isNeedIter; }
public Set<Cluster> run() { Set<Cluster> clusters = chooseCenterCluster(); boolean ifNeedIter = true; while (ifNeedIter) { cluster(clusters); ifNeedIter = getUpdate(clusters); iterRunTimes ++; } return clusters; }
public int getIterRunTimes() { return iterRunTimes; } }
|