📄 classifier.java
字号:
m_Classifier.buildClassifier(m_trainingSet); } } } catch (Exception ex) { ex.printStackTrace(); } // Notify incremental classifier listeners of new batch System.err.println("NOTIFYING NEW BATCH"); m_ie.setStructure(dataset); m_ie.setClassifier(m_Classifier); notifyIncrementalClassifierListeners(m_ie); return; } else { if (m_trainingSet == null) { // simply return. If the training set is still null after // the first instance then the classifier must not be updateable // and hasn't been previously batch trained - therefore we can't // do anything meaningful return; } } try { // test on this instance int status = IncrementalClassifierEvent.WITHIN_BATCH; /* if (m_incrementalEvent.getStatus() == InstanceEvent.FORMAT_AVAILABLE) { status = IncrementalClassifierEvent.NEW_BATCH; */ /* } else */ if (m_incrementalEvent.getStatus() == InstanceEvent.BATCH_FINISHED) { status = IncrementalClassifierEvent.BATCH_FINISHED; } m_ie.setStatus(status); m_ie.setClassifier(m_Classifier); m_ie.setCurrentInstance(m_incrementalEvent.getInstance()); notifyIncrementalClassifierListeners(m_ie); // now update on this instance (if class is not missing and classifier // is updateable and user has specified that classifier is to be // updated) if (m_Classifier instanceof weka.classifiers.UpdateableClassifier && m_updateIncrementalClassifier == true && !(m_incrementalEvent.getInstance(). isMissing(m_incrementalEvent.getInstance(). dataset().classIndex()))) { ((weka.classifiers.UpdateableClassifier)m_Classifier). updateClassifier(m_incrementalEvent.getInstance()); } if (m_incrementalEvent.getStatus() == InstanceEvent.BATCH_FINISHED) { if (m_textListeners.size() > 0) { String modelString = m_Classifier.toString(); String titleString = m_Classifier.getClass().getName(); titleString = titleString. substring(titleString.lastIndexOf('.') + 1, titleString.length()); modelString = "=== Classifier model ===\n\n" + "Scheme: " +titleString+"\n" + "Relation: " + m_trainingSet.relationName() + "\n\n" + modelString; titleString = "Model: " + titleString; TextEvent nt = new TextEvent(this, modelString, titleString); notifyTextListeners(nt); } } } catch (Exception ex) { if (m_log != null) { m_log.logMessage(ex.toString()); } ex.printStackTrace(); } } /** * Unused at present */ private void startIncrementalHandler() { if (m_buildThread == null) { m_buildThread = new Thread() { public void run() { while (true) { synchronized(m_dummy) { try { m_dummy.wait(); } catch (InterruptedException ex) { // m_buildThread = null; // System.err.println("Here"); return; } } Classifier.this.handleIncrementalEvent(); m_state = IDLE; block(false); } } }; m_buildThread.setPriority(Thread.MIN_PRIORITY); m_buildThread.start(); // give thread a chance to start try { Thread.sleep(500); } catch (InterruptedException ex) { } } } /** * Accepts a training set and builds batch classifier * * @param e a <code>TrainingSetEvent</code> value */ public void acceptTrainingSet(final TrainingSetEvent e) { if (e.isStructureOnly()) { // no need to build a classifier, instead just generate a dummy // BatchClassifierEvent in order to pass on instance structure to // any listeners - eg. PredictionAppender can use it to determine // the final structure of instances with predictions appended BatchClassifierEvent ce = new BatchClassifierEvent(this, m_Classifier, new DataSetEvent(this, e.getTrainingSet()), e.getSetNumber(), e.getMaxSetNumber()); notifyBatchClassifierListeners(ce); return; } if (m_buildThread == null) { try { if (m_state == IDLE) { synchronized (this) { m_state = BUILDING_MODEL; } m_trainingSet = e.getTrainingSet(); final String oldText = m_visual.getText(); m_buildThread = new Thread() { public void run() { try { if (m_trainingSet != null) { if (m_trainingSet.classIndex() < 0) { // assume last column is the class m_trainingSet.setClassIndex(m_trainingSet.numAttributes()-1); if (m_log != null) { m_log.logMessage("Classifier : assuming last " +"column is the class"); } } m_visual.setAnimated(); m_visual.setText("Building model..."); if (m_log != null) { m_log.statusMessage("Classifier : building model..."); } buildClassifier(); if (m_Classifier instanceof weka.core.Drawable && m_graphListeners.size() > 0) { String grphString = ((weka.core.Drawable)m_Classifier).graph(); int grphType = ((weka.core.Drawable)m_Classifier).graphType(); String grphTitle = m_Classifier.getClass().getName(); grphTitle = grphTitle.substring(grphTitle. lastIndexOf('.')+1, grphTitle.length()); grphTitle = "Set " + e.getSetNumber() + " (" +e.getTrainingSet().relationName() + ") " +grphTitle; GraphEvent ge = new GraphEvent(Classifier.this, grphString, grphTitle, grphType); notifyGraphListeners(ge); } if (m_textListeners.size() > 0) { String modelString = m_Classifier.toString(); String titleString = m_Classifier.getClass().getName(); titleString = titleString. substring(titleString.lastIndexOf('.') + 1, titleString.length()); modelString = "=== Classifier model ===\n\n" + "Scheme: " +titleString+"\n" + "Relation: " + m_trainingSet.relationName() + ((e.getMaxSetNumber() > 1) ? "\nTraining Fold: "+e.getSetNumber() :"") + "\n\n" + modelString; titleString = "Model: " + titleString; TextEvent nt = new TextEvent(Classifier.this, modelString, titleString); notifyTextListeners(nt); } } } catch (Exception ex) { ex.printStackTrace(); } finally { m_visual.setText(oldText); m_visual.setStatic(); m_state = IDLE; if (isInterrupted()) { // prevent any classifier events from being fired m_trainingSet = null; if (m_log != null) { m_log.logMessage("Build classifier interrupted!"); m_log.statusMessage("OK"); } } else { // save header m_trainingSet = new Instances(m_trainingSet, 0); } if (m_log != null) { m_log.statusMessage("OK"); } block(false); } } }; m_buildThread.setPriority(Thread.MIN_PRIORITY); m_buildThread.start(); // make sure the thread is still running before we block // if (m_buildThread.isAlive()) { block(true); // } m_buildThread = null; m_state = IDLE; } } catch (Exception ex) { ex.printStackTrace(); } } } /** * Accepts a test set for a batch trained classifier * * @param e a <code>TestSetEvent</code> value */ public void acceptTestSet(TestSetEvent e) { if (m_trainingSet != null) { try { if (m_state == IDLE) { synchronized(this) { m_state = CLASSIFYING; } m_testingSet = e.getTestSet(); if (m_testingSet != null) { if (m_testingSet.classIndex() < 0) { m_testingSet.setClassIndex(m_testingSet.numAttributes()-1); } } if (m_trainingSet.equalHeaders(m_testingSet)) { BatchClassifierEvent ce = new BatchClassifierEvent(this, m_Classifier, new DataSetEvent(this, e.getTestSet()), e.getSetNumber(), e.getMaxSetNumber()); // System.err.println("Just before notify classifier listeners"); notifyBatchClassifierListeners(ce); // System.err.println("Just after notify classifier listeners"); } m_state = IDLE; } } catch (Exception ex) { ex.printStackTrace(); } } } private void buildClassifier() throws Exception { m_Classifier.buildClassifier(m_trainingSet); } /** * Sets the visual appearance of this wrapper bean * * @param newVisual a <code>BeanVisual</code> value */ public void setVisual(BeanVisual newVisual) { m_visual = newVisual; } /** * Gets the visual appearance of this wrapper bean */ public BeanVisual getVisual() { return m_visual; } /** * Use the default visual appearance for this bean */ public void useDefaultVisual() { // try to get a default for this package of classifiers String name = m_Classifier.getClass().toString(); String packageName = name.substring(0, name.lastIndexOf('.')); packageName = packageName.substring(packageName.lastIndexOf('.')+1, packageName.length()); if (!m_visual.loadIcons(BeanVisual.ICON_PATH+"Default_"+packageName +"Classifier.gif", BeanVisual.ICON_PATH+"Default_"+packageName +"Classifier_animated.gif")) { m_visual.loadIcons(BeanVisual. ICON_PATH+"DefaultClassifier.gif", BeanVisual. ICON_PATH+"DefaultClassifier_animated.gif"); } } /** * Add a batch classifier listener * * @param cl a <code>BatchClassifierListener</code> value */ public synchronized void addBatchClassifierListener(BatchClassifierListener cl) { m_batchClassifierListeners.addElement(cl); } /** * Remove a batch classifier listener * * @param cl a <code>BatchClassifierListener</code> value */ public synchronized void removeBatchClassifierListener(BatchClassifierListener cl) { m_batchClassifierListeners.remove(cl); } /** * Notify all batch classifier listeners of a batch classifier event * * @param ce a <code>BatchClassifierEvent</code> value */ private void notifyBatchClassifierListeners(BatchClassifierEvent ce) { Vector l; synchronized (this) { l = (Vector)m_batchClassifierListeners.clone(); } if (l.size() > 0) { for(int i = 0; i < l.size(); i++) { ((BatchClassifierListener)l.elementAt(i)).acceptClassifier(ce); } } } /** * Add a graph listener * * @param cl a <code>GraphListener</code> value */ public synchronized void addGraphListener(GraphListener cl) { m_graphListeners.addElement(cl);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -