/** * Initialize with the fartherst first centers * * @param data the training data * @throws Exception if a problem occurs */ protected void farthestFirstInit(Instances data) throws Exception { FarthestFirst ff = new FarthestFirst(); ff.setNumClusters(m_NumClusters); ff.buildClusterer(data); m_ClusterCentroids = ff.getClusterCentroids(); }
/** * Main method for testing this class. * * @param argv should contain the following arguments: * <p> * -t training file [-N number of clusters] */ public static void main(String[] argv) { runClusterer(new FarthestFirst(), argv); } }
getCapabilities().testWithFail(data); m_instances = Filter.useFilter(data, m_ReplaceMissingFilter); initMinMax(m_instances); Random r = new Random(getSeed()); boolean[] selected = new boolean[n]; double[] minDistance = new double[n]; selected[firstI] = true; updateMinDistance(minDistance, selected, m_instances, m_instances.instance(firstI)); int nextI = farthestAway(minDistance, selected); m_ClusterCentroids.add(m_instances.instance(nextI)); selected[nextI] = true; updateMinDistance(minDistance, selected, m_instances, m_instances.instance(nextI));
/** Creates a default FarthestFirst */ public Clusterer getClusterer() { return new FarthestFirst(); }
protected void updateMinDistance(double[] minDistance, boolean[] selected, Instances data, Instance center) { for (int i = 0; i < selected.length; i++) { if (!selected[i]) { double d = distance(center, data.instance(i)); if (d < minDistance[i]) { minDistance[i] = d; } } } }
/** * Gets the current settings of FarthestFirst * * @return an array of strings suitable for passing to setOptions() */ @Override public String[] getOptions() { Vector<String> result = new Vector<String>(); result.add("-N"); result.add("" + getNumClusters()); Collections.addAll(result, super.getOptions()); return result.toArray(new String[result.size()]); }
/** * Classifies a given instance. * * @param instance the instance to be assigned to a cluster * @return the number of the assigned cluster as an integer if the class is * enumerated, otherwise the predicted value * @throws Exception if instance could not be classified successfully */ @Override public int clusterInstance(Instance instance) throws Exception { m_ReplaceMissingFilter.input(instance); m_ReplaceMissingFilter.batchFinished(); Instance inst = m_ReplaceMissingFilter.output(); return clusterProcessedInstance(inst); }
diff = difference(firstI, first.valueSparse(p1), second.valueSparse(p2)); p1++; p2++; } else if (firstI > secondI) { diff = difference(secondI, 0, second.valueSparse(p2)); p2++; } else { diff = difference(firstI, first.valueSparse(p1), 0); p1++;
getCapabilities().testWithFail(data); m_instances = Filter.useFilter(data, m_ReplaceMissingFilter); initMinMax(m_instances); Random r = new Random(getSeed()); boolean[] selected = new boolean[n]; double[] minDistance = new double[n]; selected[firstI] = true; updateMinDistance(minDistance, selected, m_instances, m_instances.instance(firstI)); int nextI = farthestAway(minDistance, selected); m_ClusterCentroids.add(m_instances.instance(nextI)); selected[nextI] = true; updateMinDistance(minDistance, selected, m_instances, m_instances.instance(nextI));
/** * Main method for testing this class. * * @param argv should contain the following arguments: * <p> * -t training file [-N number of clusters] */ public static void main(String[] argv) { runClusterer(new FarthestFirst(), argv); } }
/** Creates a default FarthestFirst */ public Clusterer getClusterer() { return new FarthestFirst(); }
protected void updateMinDistance(double[] minDistance, boolean[] selected, Instances data, Instance center) { for (int i = 0; i < selected.length; i++) { if (!selected[i]) { double d = distance(center, data.instance(i)); if (d < minDistance[i]) { minDistance[i] = d; } } } }
/** * Gets the current settings of FarthestFirst * * @return an array of strings suitable for passing to setOptions() */ @Override public String[] getOptions() { Vector<String> result = new Vector<String>(); result.add("-N"); result.add("" + getNumClusters()); Collections.addAll(result, super.getOptions()); return result.toArray(new String[result.size()]); }
/** * Classifies a given instance. * * @param instance the instance to be assigned to a cluster * @return the number of the assigned cluster as an integer if the class is * enumerated, otherwise the predicted value * @throws Exception if instance could not be classified successfully */ @Override public int clusterInstance(Instance instance) throws Exception { m_ReplaceMissingFilter.input(instance); m_ReplaceMissingFilter.batchFinished(); Instance inst = m_ReplaceMissingFilter.output(); return clusterProcessedInstance(inst); }
diff = difference(firstI, first.valueSparse(p1), second.valueSparse(p2)); p1++; p2++; } else if (firstI > secondI) { diff = difference(secondI, 0, second.valueSparse(p2)); p2++; } else { diff = difference(firstI, first.valueSparse(p1), 0); p1++;
/** * Initialize with the fartherst first centers * * @param data the training data * @throws Exception if a problem occurs */ protected void farthestFirstInit(Instances data) throws Exception { FarthestFirst ff = new FarthestFirst(); ff.setNumClusters(m_NumClusters); ff.buildClusterer(data); m_ClusterCentroids = ff.getClusterCentroids(); }
/** * clusters an instance that has been through the filters * * @param instance the instance to assign a cluster to * @return a cluster number */ protected int clusterProcessedInstance(Instance instance) { double minDist = Double.MAX_VALUE; int bestCluster = 0; for (int i = 0; i < m_NumClusters; i++) { double dist = distance(instance, m_ClusterCentroids.instance(i)); if (dist < minDist) { minDist = dist; bestCluster = i; } } return bestCluster; }
/** * clusters an instance that has been through the filters * * @param instance the instance to assign a cluster to * @return a cluster number */ protected int clusterProcessedInstance(Instance instance) { double minDist = Double.MAX_VALUE; int bestCluster = 0; for (int i = 0; i < m_NumClusters; i++) { double dist = distance(instance, m_ClusterCentroids.instance(i)); if (dist < minDist) { minDist = dist; bestCluster = i; } } return bestCluster; }