/** * Checks whether the scheme says it can handle instance weights. * * @return true if the clusterer handles instance weights */ protected boolean[] weightedInstancesHandler() { boolean[] result = new boolean[2]; print("weighted instances clusterer..."); if (m_Clusterer instanceof WeightedInstancesHandler) { println("yes"); result[0] = true; } else { println("no"); result[0] = false; } return result; }
/** * Checks whether the scheme says it can handle instance weights. * * @return true if the associator handles instance weights */ protected boolean[] weightedInstancesHandler() { boolean[] result = new boolean[2]; print("weighted instances associator..."); if (m_Associator instanceof WeightedInstancesHandler) { println("yes"); result[0] = true; } else { println("no"); result[0] = false; } return result; }
/** * Checks whether the scheme says it can handle instance weights. * * @return true if the scheme handles instance weights */ protected boolean[] weightedInstancesHandler() { boolean[] result = new boolean[2]; print("weighted instances scheme..."); if (getTestObject() instanceof WeightedInstancesHandler) { println("yes"); result[0] = true; } else { println("no"); result[0] = false; } return result; }
/** * Checks whether the scheme says it can handle instance weights. * * @return true if the classifier handles instance weights */ protected boolean[] weightedInstancesHandler() { boolean[] result = new boolean[2]; print("weighted instances classifier..."); if (m_Classifier instanceof WeightedInstancesHandler) { println("yes"); result[0] = true; } else { println("no"); result[0] = false; } return result; }
/** * Checks whether the scheme says it can handle instance weights. * * @return true if the kernel handles instance weights */ protected boolean[] weightedInstancesHandler() { boolean[] result = new boolean[2]; print("weighted instances kernel..."); if (m_Kernel instanceof WeightedInstancesHandler) { println("yes"); result[0] = true; } else { println("no"); result[0] = false; } return result; }
/** * Checks whether the scheme says it can handle instance weights. * * @return true if the estimator handles instance weights */ protected boolean[] weightedInstancesHandler() { boolean[] result = new boolean[2]; print("weighted instances estimator..."); if (m_Estimator instanceof WeightedInstancesHandler) { println("yes"); result[0] = true; } else { println("no"); result[0] = false; } return result; }
/** * the standard collective classifier accepts only nominal, binary classes * otherwise an exception is thrown. Additionally, all classifiers must be * able to handle weighted instances. * @throws Exception if the data doesn't have a nominal, binary class */ @Override protected void checkRestrictions() throws Exception { int i; String nonWeighted; super.checkRestrictions(); // do all implement the WeightedInstancesHandler? nonWeighted = ""; for (i = 0; i < getClassifiers().length; i++) { if (!(getClassifiers()[i] instanceof WeightedInstancesHandler)) { if (nonWeighted.length() > 0) nonWeighted += ", "; nonWeighted += getClassifiers()[i].getClass().getName(); } } if (nonWeighted.length() > 0) throw new Exception( "The following classifier(s) cannot handle weighted instances:\n" + nonWeighted); }
@Override public void buildClassifier(Instances data) throws Exception { /** Changed here: Using the provided classifiers */ /** End */ // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class data = new Instances(data); data.deleteWithMissingClass(); // only class? -> build ZeroR model if (data.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(data); return; } else { m_ZeroR = null; } m_NumClasses = data.numClasses(); if ((!m_UseResampling) && (m_Classifier instanceof WeightedInstancesHandler)) { buildClassifierWithWeights(data); } else { buildClassifierUsingResampling(data); } }
/** * Builds the committee of randomizable classifiers. * * @param data the training data to be used for generating the * bagged classifier. * @exception Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(data); // get fresh instances m_data = new Instances(data); super.buildClassifier(m_data); if (!(m_Classifier instanceof Randomizable)) { throw new IllegalArgumentException("Base learner must implement Randomizable!"); } m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations); Random random = m_data.getRandomNumberGenerator(m_Seed); // Resample data based on weights if base learner can't handle weights if (!(m_Classifier instanceof WeightedInstancesHandler)) { m_data = m_data.resampleWithWeights(random); } for (int j = 0; j < m_Classifiers.length; j++) { // Set the random number seed for the current classifier. ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt()); // Build the classifier. // m_Classifiers[j].buildClassifier(m_data); } buildClassifiers(); // save memory m_data = null; }
/** * Initialize the classifier. * * @param data the training data to be used for generating the boosted * classifier. * @throws Exception if the classifier could not be built successfully */ public void initializeClassifier(Instances data) throws Exception { super.buildClassifier(data); // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class data = new Instances(data); data.deleteWithMissingClass(); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(data); m_NumClasses = data.numClasses(); m_Betas = new double[m_Classifiers.length]; m_NumIterationsPerformed = 0; m_TrainingData = new Instances(data); m_RandomInstance = new Random(m_Seed); if ((m_UseResampling) || (!(m_Classifier instanceof WeightedInstancesHandler))) { // Normalize weights so that they sum to one and can be used as sampling probabilities double sumProbs = m_TrainingData.sumOfWeights(); for (int i = 0; i < m_TrainingData.numInstances(); i++) { m_TrainingData.instance(i).setWeight(m_TrainingData.instance(i).weight() / sumProbs); } } }
/** * Generates the classifier. * * @param instances set of instances serving as training data * @throws Exception if the classifier has not been generated successfully */ public void buildClassifier(Instances instances) throws Exception { if (!(m_Classifier instanceof WeightedInstancesHandler)) { throw new IllegalArgumentException("Classifier must be a " + "WeightedInstancesHandler!"); } // can classifier handle the data? getCapabilities().testWithFail(instances); // remove instances with missing class instances = new Instances(instances); instances.deleteWithMissingClass(); // only class? -> build ZeroR model if (instances.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(instances); return; } else { m_ZeroR = null; } m_Train = new Instances(instances, 0, instances.numInstances()); m_NNSearch.setInstances(m_Train); }
/** * Boosting method. * * @param data the training data to be used for generating the * boosted classifier. * @throws Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { super.buildClassifier(data); // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class data = new Instances(data); data.deleteWithMissingClass(); // only class? -> build ZeroR model if (data.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(data); return; } else { m_ZeroR = null; } m_NumClasses = data.numClasses(); if ((!m_UseResampling) && (m_Classifier instanceof WeightedInstancesHandler)) { buildClassifierWithWeights(data); } else { buildClassifierUsingResampling(data); } }
/** * Builds the committee of randomizable classifiers. * * @param data the training data to be used for generating the * bagged classifier. * @exception Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class m_data = new Instances(data); m_data.deleteWithMissingClass(); super.buildClassifier(m_data); if (!(m_Classifier instanceof Randomizable)) { throw new IllegalArgumentException("Base learner must implement Randomizable!"); } m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations); Random random = m_data.getRandomNumberGenerator(m_Seed); // Resample data based on weights if base learner can't handle weights if (!(m_Classifier instanceof WeightedInstancesHandler)) { m_data = m_data.resampleWithWeights(random); } for (int j = 0; j < m_Classifiers.length; j++) { // Set the random number seed for the current classifier. ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt()); // Build the classifier. // m_Classifiers[j].buildClassifier(m_data); } buildClassifiers(); // save memory m_data = null; }
/** * Builds the boosted classifier * * @param data the data to train the classifier with * @throws Exception if building fails, e.g., can't handle data */ public void initializeClassifier(Instances data) throws Exception { m_RandomInstance = new Random(m_Seed); int classIndex = data.classIndex(); if (m_Classifier == null) { throw new Exception("A base classifier has not been specified!"); } if (!(m_Classifier instanceof WeightedInstancesHandler) && !m_UseResampling) { m_UseResampling = true; } // can classifier handle the data? getCapabilities().testWithFail(data); if (m_Debug) { System.err.println("Creating copy of the training data"); } // remove instances with missing class m_data = new Instances(data); m_data.deleteWithMissingClass(); // only class? -> build ZeroR model if (m_data.numAttributes() == 1) { System.err .println("Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(m_data); return; } else { m_ZeroR = null; } m_NumClasses = m_data.numClasses(); m_ClassAttribute = m_data.classAttribute(); // Create the base classifiers if (m_Debug) { System.err.println("Creating base classifiers"); } m_Classifiers = new ArrayList<Classifier[]>(); // Build classifier on all the data int numInstances = m_data.numInstances(); m_trainFs = new double[numInstances][m_NumClasses]; m_trainYs = new double[numInstances][m_NumClasses]; for (int j = 0; j < m_NumClasses; j++) { for (int i = 0, k = 0; i < numInstances; i++, k++) { m_trainYs[i][j] = (m_data.instance(k).classValue() == j) ? 1.0 - m_Offset : 0.0 + (m_Offset / (double) m_NumClasses); } } // Make class numeric m_data.setClassIndex(-1); m_data.deleteAttributeAt(classIndex); m_data.insertAttributeAt(new Attribute("'pseudo class'"), classIndex); m_data.setClassIndex(classIndex); m_NumericClassData = new Instances(m_data, 0); // Perform iterations m_probs = initialProbs(numInstances); m_logLikelihood = logLikelihood(m_trainYs, m_probs); m_NumGenerated = 0; if (m_Debug) { System.err.println("Avg. log-likelihood: " + m_logLikelihood); } m_sumOfWeights = m_data.sumOfWeights(); }
/** * Builds the boosted classifier * * @param data the data to train the classifier with * @throws Exception if building fails, e.g., can't handle data */ public void initializeClassifier(Instances data) throws Exception { m_RandomInstance = new Random(m_Seed); int classIndex = data.classIndex(); if (m_Classifier == null) { throw new Exception("A base classifier has not been specified!"); } if (!(m_Classifier instanceof WeightedInstancesHandler) && !m_UseResampling) { m_UseResampling = true; } // can classifier handle the data? getCapabilities().testWithFail(data); if (m_Debug) { System.err.println("Creating copy of the training data"); } // remove instances with missing class m_data = new Instances(data); m_data.deleteWithMissingClass(); // only class? -> build ZeroR model if (m_data.numAttributes() == 1) { System.err.println( "Cannot build model (only class attribute present in data!), " + "using ZeroR model instead!"); m_ZeroR = new weka.classifiers.rules.ZeroR(); m_ZeroR.buildClassifier(m_data); return; } else { m_ZeroR = null; } m_NumClasses = m_data.numClasses(); m_ClassAttribute = m_data.classAttribute(); // Create the base classifiers if (m_Debug) { System.err.println("Creating base classifiers"); } m_Classifiers = new ArrayList<Classifier[]>(); // Build classifier on all the data int numInstances = m_data.numInstances(); m_trainFs = new double [numInstances][m_NumClasses]; m_trainYs = new double [numInstances][m_NumClasses]; for (int j = 0; j < m_NumClasses; j++) { for (int i = 0, k = 0; i < numInstances; i++, k++) { m_trainYs[i][j] = (m_data.instance(k).classValue() == j) ? 1.0 - m_Offset: 0.0 + (m_Offset / (double)m_NumClasses); } } // Make class numeric m_data.setClassIndex(-1); m_data.deleteAttributeAt(classIndex); m_data.insertAttributeAt(new Attribute("'pseudo class'"), classIndex); m_data.setClassIndex(classIndex); m_NumericClassData = new Instances(m_data, 0); // Perform iterations m_probs = initialProbs(numInstances); m_logLikelihood = logLikelihood(m_trainYs, m_probs); m_NumGenerated = 0; if (m_Debug) { System.err.println("Avg. log-likelihood: " + m_logLikelihood); } m_sumOfWeights = m_data.sumOfWeights(); }
@Override public boolean supportsWeightedData() { return wekaClassifier instanceof WeightedInstancesHandler; }