@Override public int[][] performClustering(double[][] data) { return new IndexClusters(cluster(data).assignments).clusters(); }
@Override public byte[] binaryHeader() { return asciiHeader().getBytes(); }
@Override public String toString() { final int[][] clusters = this.clusters(); int i = 0; final StringWriter sw = new StringWriter(); final PrintWriter out = new PrintWriter(sw); out.println("N-Clusters: " + this.numClusters()); out.println("Entities: " + this.numEntries()); String str = sw.toString(); for (final int[] member : clusters) { str += String.format("%d %s\n", i++, Arrays.toString(member)); } return str; } }
/** * * @param c the underlying {@link IndexClusters} * @param valvects the eigen values and vectors */ public SpectralIndexedClusters(IndexClusters c, IndependentPair<double[], double[][]>valvects) { this.clusters = c.clusters(); this.nEntries = c.numEntries(); this.valvects = valvects; }
public void correctClusters(IndexClusters clstrs){ int[][] clusters = clstrs.clusters(); for (int i = 0; i < clusters.length; i++) { int[] cluster = clusters[i]; for (int j = 0; j < cluster.length; j++) { cluster[j] = indexCorrection.get(cluster[j]); } } } }
@Override public IndexClusters cluster(SparseMatrix data) { int nClusters = 0; if (this.forceClusters > 0) nClusters = this.forceClusters; else nClusters = this.random.nextInt(data.rowCount()); final Map<Integer, TIntArrayList> clusters = new HashMap<Integer, TIntArrayList>(); for (int i = 0; i < data.rowCount(); i++) { final int cluster = this.random.nextInt(nClusters); TIntArrayList l = clusters.get(cluster); if (l == null) { clusters.put(cluster, l = new TIntArrayList()); } l.add(i); } final int[][] outClusters = new int[clusters.size()][]; int i = 0; for (final Entry<Integer, TIntArrayList> is : clusters.entrySet()) { outClusters[i++] = is.getValue().toArray(); } return new IndexClusters(outClusters, data.rowCount()); }
@Override public void writeASCII(PrintWriter out) throws IOException { out.println(this.numClusters()); out.println(this.nEntries); for (int i = 0; i < this.clusters.length; i++) { final int[] cluster = this.clusters[i]; out.println(i); out.println(cluster.length); for (int j = 0; j < cluster.length; j++) { out.println(cluster[j]); } } }
@Override public String toString() { String str = String.format("%s", Arrays.toString(this.noise)); return super.toString() + "\n" + str; }
@Override public int[][] performClustering(SparseMatrix data) { return this.cluster(data).clusters(); }
@Override public void writeBinary(DataOutput out) throws IOException { out.writeInt(this.numClusters()); out.writeInt(nEntries); for (int i = 0; i < this.clusters.length; i++) { final int[] cluster = this.clusters[i]; out.writeInt(i); out.writeInt(cluster.length); for (int j = 0; j < cluster.length; j++) { out.writeInt(cluster[j]); } } }
@Override public int[][] performClustering(int[][] data) { return new IndexClusters(this.cluster(data).defaultHardAssigner().assign(data)).clusters(); } }
@Override public int[][] performClustering(SparseMatrix data) { return this.cluster(data).clusters(); }
@Override public int[][] performClustering(byte[][] data) { ByteCentroidsResult res = this.cluster(data); return new IndexClusters(res.defaultHardAssigner().assign(data)).clusters(); }
@Override public int[][] performClustering(List<SparseMatrix> data) { return this.cluster(data).clusters(); }
@Override public int[][] performClustering(int[][] data) { IntCentroidsResult res = this.cluster(data); return new IndexClusters(res.defaultHardAssigner().assign(data)).clusters(); }
/** * Given the old and new clusters, make a decision as to which rows are now inactive, * and therefore which clusters are now completed * @param oldClusters * @param newClusters * @param inactiveRows * @param completedClusters */ protected void detectInactive(IndexClusters oldClusters, IndexClusters newClusters, TIntSet inactiveRows, List<int[]> completedClusters) { Map<Integer, IntDoublePair> stability = calculateStability(oldClusters,newClusters,inactiveRows); for (Entry<Integer, IntDoublePair> e : stability.entrySet()) { if(e.getValue().second >= threshold){ int[] completedCluster = oldClusters.clusters()[e.getKey()]; inactiveRows.addAll(completedCluster); completedClusters.add(completedCluster); if(threshold == 1){ newClusters.clusters()[e.getValue().first] = new int[0]; } } } }
@Override public int[][] performClustering(long[][] data) { LongCentroidsResult clusters = this.cluster(data); return new IndexClusters(clusters.defaultHardAssigner().assign(data)).clusters(); }
@Override protected void detectInactive(IndexClusters oldClusters, IndexClusters newClusters, TIntSet inactiveRows, List<int[]> completedClusters) { Map<Integer, IntDoublePair> stability = calculateStability(oldClusters,newClusters, inactiveRows); for (Entry<Integer, IntDoublePair> e : stability.entrySet()) { int[] completedCluster = oldClusters.clusters()[e.getKey()]; if(e.getValue().second >= threshold){ for (int i = 0; i < completedCluster.length; i++) { int index = completedCluster[i]; inactiveRows.add(index); this.seenCount.remove(index); } completedClusters.add(completedCluster); } else{ for (int i = 0; i < completedCluster.length; i++) { int index = completedCluster[i]; int newCount = this.seenCount.adjustOrPutValue(index, 1, 1); if(newCount>= this.lifetime){ this.seenCount.remove(index); inactiveRows.add(index); completedClusters.add(new int[]{index}); } } } } }
@Override public int[][] performClustering(float[][] data) { FloatCentroidsResult clusters = this.cluster(data); return new IndexClusters(clusters.defaultHardAssigner().assign(data)).clusters(); }