📄 classifierpanel.java
字号:
private class ConnectionService extends Thread { /** The address of the server to connect to */ private InetAddress serverAddress; /** The number of the fold to calculate */ private int index; /** * Gets the internet address of the computer to connect to by * looking up the server's name. * * @param serverName the name of computer to connect to */ ConnectionService(String serverName) { try { this.serverAddress = InetAddress.getByName(serverName); } catch(UnknownHostException e) { // There is not host known by that name, so exit cleanly. return; } } /** * The actual thread that will connect to the other computer, * send data to it, and receive data from it. */ public void run() { /** Used to send bytes to the server */ BufferedOutputStream bos; /** Used to send objects to the server */ ObjectOutputStream oos; /** Used to send ints to the server */ DataOutputStream dos; /** Used to receive objects from the server */ ObjectInputStream ois; /** The connection to the server */ Socket sock; try { // Determines the server's InetAddress and then connects // to this server sock = new Socket(serverAddress, port); try { synchronized(otherComputers) { otherComputers.append(serverAddress + "\n"); } try { // Determines which fold need to be done index = determineIndex(); if(index != -1) { // Create the output streams bos = new BufferedOutputStream( sock.getOutputStream()); oos = new ObjectOutputStream(bos); dos = new DataOutputStream(bos); // Write all necessary info to the server dos.writeInt(Connection.CV_GUI); dos.flush(); oos.writeObject(inst); oos.writeObject(classifier); oos.flush(); dos.writeInt(numFolds); dos.writeInt(index); dos.flush(); // Create the input stream and get the results // back from the server ois = new ObjectInputStream( new BufferedInputStream( sock.getInputStream())); Evaluation newEvaluation = (Evaluation)ois.readObject(); FastVector newPredictions = (FastVector)ois.readObject(); Instances newPredInstances = (Instances)ois.readObject(); FastVector newPlotShape = (FastVector)ois.readObject(); FastVector newPlotSize = (FastVector)ois.readObject(); // Aggregate all of the data that was sent // back from the server synchronized(status) { if(status[index] != Status.DONE) { status[index] = Status.DONE; synchronized(evaluation) { evaluation.aggregate(newEvaluation); } if(predictions != null) { synchronized(predictions) { for(int i=0; i < newPredictions.size(); i++) { predictions.addElement( newPredictions.elementAt(i)); } } } synchronized(predInstances) { for(int i=0; i < newPredInstances.numInstances(); i++) { predInstances.add( newPredInstances.instance(i)); } } synchronized(plotShape) { for(int i=0; i < newPlotShape.size(); i++) { plotShape.addElement( newPlotShape.elementAt(i)); } } synchronized(plotSize) { for(int i=0; i < newPlotSize.size(); i++) { plotSize.addElement( newPlotSize.elementAt(i)); } } foldsCompleted++; m_Log.statusMessage(foldsCompleted + "/" + numFolds + " folds completed"); } } // Determine which section we should train on index = determineIndex(); while(index != -1) { // Write this number to the server dos.writeInt(index); dos.flush(); // Get the results from the server newEvaluation = (Evaluation)ois.readObject(); newPredictions = (FastVector)ois.readObject(); newPredInstances = (Instances)ois.readObject(); newPlotShape = (FastVector)ois.readObject(); newPlotSize = (FastVector)ois.readObject(); // Aggregate all of the data sent back // from the server synchronized(status) { if(status[index] != Status.DONE) { status[index] = Status.DONE; synchronized(evaluation) { evaluation.aggregate(newEvaluation); } if(predictions != null) { synchronized(predictions) { for(int i=0; i < newPredictions.size(); i++) { predictions.addElement( newPredictions.elementAt(i)); } } } synchronized(predInstances) { for(int i=0; i < newPredInstances.numInstances(); i++) { predInstances.add( newPredInstances.instance(i)); } } synchronized(plotShape) { for(int i=0; i < newPlotShape.size(); i++) { plotShape.addElement( newPlotShape.elementAt(i)); } } synchronized(plotSize) { for(int i=0; i < newPlotSize.size(); i++) { plotSize.addElement( newPlotSize.elementAt(i)); } } foldsCompleted++; m_Log.statusMessage(foldsCompleted + "/" + numFolds + " folds completed"); } } // Determine which fold to do next index = determineIndex(); } // When all of the folds have been completed, // notify the main thread. synchronized(evaluation) { evaluation.notifyAll(); } } } catch (Exception e) { e.printStackTrace(); return; } } finally { // Close the connection no matter what exceptions were // thrown sock.close(); } } catch (Exception e) { // We could not connect to this server, so exit cleanly return; } } } } /* Register the property editors we need */ static { java.beans.PropertyEditorManager .registerEditor(weka.core.SelectedTag.class, weka.gui.SelectedTagEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.filters.Filter.class, weka.gui.GenericObjectEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.Classifier [].class, weka.gui.GenericArrayEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.DistributionClassifier.class, weka.gui.GenericObjectEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.Classifier.class, weka.gui.GenericObjectEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.CostMatrix.class, weka.gui.CostMatrixEditor.class); } /** * Creates the classifier panel */ public ClassifierPanel() { // Connect / configure the components m_OutText.setEditable(false); m_OutText.setFont(new Font("Monospaced", Font.PLAIN, 12)); m_OutText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); m_OutText.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) { m_OutText.selectAll(); } } }); m_History.setBorder(BorderFactory.createTitledBorder("Result list")); m_ClassifierEditor.setClassType(Classifier.class); m_ClassifierEditor.setValue(new weka.classifiers.ZeroR()); m_ClassifierEditor.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { repaint(); } }); m_ClassCombo.setToolTipText("Select the attribute to use as the class"); m_TrainBut.setToolTipText("Test on the same set that the classifier" + " is trained on"); m_CVBut.setToolTipText("Perform a n-fold cross-validation"); m_PercentBut.setToolTipText("Train on a percentage of the data and" + " test on the remainder"); m_TestSplitBut.setToolTipText("Test on a user-specified dataset"); m_StartBut.setToolTipText("Starts the classification"); m_StopBut.setToolTipText("Stops a running classification"); m_StorePredictionsBut. setToolTipText("Store predictions in the result list for later " +"visualization"); m_OutputModelBut .setToolTipText("Output the model obtained from the full training set"); m_OutputPerClassBut.setToolTipText("Output precision/recall & true/false" + " positives for each class"); m_OutputConfusionBut .setToolTipText("Output the matrix displaying class confusions"); m_OutputEntropyBut .setToolTipText("Output entropy-based evaluation measures"); m_EvalWRTCostsBut .setToolTipText("Evaluate errors with respect to a cost matrix"); m_StorePredictionsBut.setSelected(true); m_OutputModelBut.setSelected(true); m_OutputPerClassBut.setSelected(true); m_OutputConfusionBut.setSelected(true); m_ClassCombo.setEnabled(false); m_ClassCombo.setPreferredSize(COMBO_SIZE); m_ClassCombo.setMaximumSize(COMBO_SIZE); m_ClassCombo.setMinimumSize(COMBO_SIZE); m_CVBut.setSelected(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -