/** * internal function for determining the class distribution for an instance, * will be overridden by derived classes. <br/> * Here we just retrieve the class value for the given instance that was * calculated during building our classifier. It just returns "1" for the * class that was predicted, no real distribution. * Note: the ReplaceMissingValues filter is applied to a copy of the given * instance. * * @param instance the instance to get the distribution for * @return the distribution for the given instance * @see Classifier#distributionForInstance(Instance) * @see ReplaceMissingValues * @throws Exception if something goes wrong */ @Override protected double[] getDistribution(Instance instance) throws Exception { double[] result; int i; result = new double[instance.numClasses()]; for (i = 0; i < result.length; i++) result[i] = 0.0; i = Arrays.binarySearch(m_LabeledTestset, instance, new InstanceComparator(false)); if (i >= 0) { result[(int) m_LabeledTestset[i].classValue()] = 1.0; } else { CollectiveHelper.writeToTempFile("train.txt", m_TrainsetNew.toString()); CollectiveHelper.writeToTempFile("test.txt", m_TestsetNew.toString()); throw new Exception("Cannot find test instance: " + instance + "\n -> pos=" + i + " = " + m_LabeledTestset[StrictMath.abs(i)]); } return result; }
/** * performs the actual building of the classifier * @throws Exception if building fails */ @Override protected void buildClassifier() throws Exception { int i; i = 0; do { i++; if (getVerbose()) System.out.println("Iteration " + i); update(); } while (!isFinished()); // move instances to sorted array m_LabeledTestset = new Instance[m_NeighborsTestset.length]; for (i = 0; i < m_NeighborsTestset.length; i++) m_LabeledTestset[i] = m_NeighborsTestset[i].getInstance(); Arrays.sort(m_LabeledTestset, new InstanceComparator(false)); }
/** Need to remove non-nominal attributes, set class index */ protected void setUp() throws Exception { super.setUp(); // class index m_Instances.setClassIndex(1); // only nominal attributes int i = 0; while (i < m_Instances.numAttributes()) { if (!m_Instances.attribute(i).isNominal()) m_Instances.deleteAttributeAt(i); else i++; } m_Comparator = new InstanceComparator(true); }
/** Need to set class index */ protected void setUp() throws Exception { super.setUp(); m_Instances.setClassIndex(1); m_Comparator = new InstanceComparator(true); }
/** * initializes the comparator * * @param includeClass whether to include the class attribute */ public RankedInstanceComparator(boolean includeClass) { super(); m_Comparator = new InstanceComparator(includeClass); }
protected void setUp() throws Exception { super.setUp(); m_Comparator = new InstanceComparator(true); }