⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preprocesspanel.java

📁 Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	      // write the data file	      for (int i = 0; i < inst.numInstances(); i++) {		Instance tempI = inst.instance(i);		for (int j = 0; j < inst.numAttributes(); j++) {		  if (j != classIndex) {		    if (tempI.isMissing(j)) {		      w2.write("?,");		    } else if (inst.attribute(j).isNominal() || 			       inst.attribute(j).isString()) {		      w2.write(inst.attribute(j).value((int)tempI.value(j))+",");		    } else {		      w2.write(""+tempI.value(j)+",");		    }		  }		}		//		w2.write(inst.instance(i).toString());		// write the class value		if (tempI.isMissing(classIndex)) {		  w2.write("?");		} else {		  w2.write(inst.attribute(classIndex).			   value((int)tempI.value(classIndex)));		}		w2.write("\n");	      }	      w2.close();	      m_Log.statusMessage("OK");	 	    } catch (Exception ex) {	      ex.printStackTrace();	      m_Log.logMessage(ex.getMessage());	    }	    m_IOThread = null;	  }	};      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    } else {      JOptionPane.showMessageDialog(this,				    "Can't save at this time,\n"				    + "currently busy with other IO",				    "Save c45 format",				    JOptionPane.WARNING_MESSAGE);    }  }  /**   * Saves the current instances in binary serialized form to a file   *   * @param f a value of type 'File'   * @param inst the instances to save   */  protected void saveSerializedInstancesToFile(final File f, 					       final Instances inst) {    if (m_IOThread == null) {      m_IOThread = new Thread() {	  public void run() {	    try {	      m_Log.statusMessage("Saving to file...");	      ObjectOutputStream oos = 		  new ObjectOutputStream(		  new BufferedOutputStream(		  new FileOutputStream(f)));	      oos.writeObject(inst);	      oos.flush();	      oos.close();	      m_Log.statusMessage("OK");	    } catch (Exception ex) {	      ex.printStackTrace();	      m_Log.logMessage(ex.getMessage());	    }	    m_IOThread = null;	  }	};      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    } else {      JOptionPane.showMessageDialog(this,				    "Can't save at this time,\n"				    + "currently busy with other IO",				    "Save binary serialized instances",				    JOptionPane.WARNING_MESSAGE);    }   }  /**   * Saves the current instances to the supplied file.   *   * @param f a value of type 'File'   * @param inst the instances to save   * @param saveHeader true to save in arff format, false to save in csv   */  protected void saveInstancesToFile(final File f, final Instances inst,				     final boolean saveHeader) {          if (m_IOThread == null) {      m_IOThread = new Thread() {	public void run() {	  try {	    m_Log.statusMessage("Saving to file...");	    Writer w = new BufferedWriter(new FileWriter(f));	    if (saveHeader) {	      Instances h = new Instances(inst, 0);	      w.write(h.toString());	      w.write("\n");	    } else {	      // csv - write attribute names as first row	      for (int i = 0; i < inst.numAttributes(); i++) {		w.write(inst.attribute(i).name());		if (i < inst.numAttributes()-1) {		  w.write(",");		}	      }	      w.write("\n");	    }	    for (int i = 0; i < inst.numInstances(); i++) {	      w.write(inst.instance(i).toString());	      w.write("\n");	    }	    w.close();	    m_Log.statusMessage("OK");	  } catch (Exception ex) {	    ex.printStackTrace();	    m_Log.logMessage(ex.getMessage());	  }	  m_IOThread = null;	}      };      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    } else {      JOptionPane.showMessageDialog(this,				    "Can't save at this time,\n"				    + "currently busy with other IO",				    "Save Instances",				    JOptionPane.WARNING_MESSAGE);    }  }  /**   * Pops up generic object editor with list of conversion filters   *   * @param f the File   */  private void converterQuery(final File f) {    final GenericObjectEditor convEd = new GenericObjectEditor(true);    try {      convEd.setClassType(weka.core.converters.Loader.class);      convEd.setValue(new weka.core.converters.CSVLoader());      ((GenericObjectEditor.GOEPanel)convEd.getCustomEditor())	.addOkListener(new ActionListener() {	    public void actionPerformed(ActionEvent e) {	      tryConverter((Loader)convEd.getValue(), f);	    }	  });    } catch (Exception ex) {    }    PropertyDialog pd = new PropertyDialog(convEd, 100, 100);  }  /**   * Applies the selected converter   *   * @param cnv the converter to apply to the input file   * @param f the input file   */  private void tryConverter(final Loader cnv, final File f) {    if (m_IOThread == null) {      m_IOThread = new Thread() {	  public void run() {	    try {	      cnv.setSource(f);	      Instances inst = cnv.getDataSet();	      setInstances(inst);	    } catch (Exception ex) {	      m_Log.statusMessage(cnv.getClass().getName()+" failed to load "				 +f.getName());	      JOptionPane.showMessageDialog(PreprocessPanel.this,					    cnv.getClass().getName()+" failed to load '"					    + f.getName() + "'.\n"					    + "Reason:\n" + ex.getMessage(),					    "Convert File",					    JOptionPane.ERROR_MESSAGE);	      m_IOThread = null;	      converterQuery(f);	    }	    m_IOThread = null;	  }	};      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    }  }  /**   * Loads results from a set of instances contained in the supplied   * file. This is started in the IO thread, and a dialog is popped up   * if there's a problem.   *   * @param f a value of type 'File'   */  public void setInstancesFromFile(final File f) {          if (m_IOThread == null) {      m_IOThread = new Thread() {	public void run() {	  String fileType = f.getName();	  try {	    m_Log.statusMessage("Reading from file...");	    if (f.getName().toLowerCase().endsWith(Instances.FILE_EXTENSION)) {	    	      fileType = "arff";	      Reader r = new BufferedReader(new FileReader(f));	      setInstances(new Instances(r));	      r.close();	    } else if (f.getName().toLowerCase().endsWith(CSVLoader.FILE_EXTENSION)) {	      fileType = "csv";	      CSVLoader cnv = new CSVLoader();	      cnv.setSource(f);	      Instances inst = cnv.getDataSet();	      setInstances(inst);	    } else if (f.getName().toLowerCase().endsWith(C45Loader.FILE_EXTENSION)) {	      fileType = "C45 names";	      C45Loader cnv = new C45Loader();	      cnv.setSource(f);	      Instances inst = cnv.getDataSet();	      setInstances(inst);	    } else if (f.getName().toLowerCase().		       endsWith(Instances.SERIALIZED_OBJ_FILE_EXTENSION)		       || f.getName().toLowerCase().endsWith(".tmp")) {	      ObjectInputStream ois = 		new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));	      setInstances((Instances)ois.readObject());	      ois.close();	    } else {	      throw new Exception("Unrecognized file type");	    }	  } catch (Exception ex) {	    m_Log.statusMessage("File '" + f.getName() + "' not recognised as an "				+fileType+" file.");	    m_IOThread = null;	    if (JOptionPane.showOptionDialog(PreprocessPanel.this,					     "File '" + f.getName()					     + "' not recognised as an "					     +fileType+" file.\n"					     + "Reason:\n" + ex.getMessage(),					     "Load Instances",					     0,					     JOptionPane.ERROR_MESSAGE,					     null,					     new String[] {"OK", "Use Converter"},					     null) == 1) {	    	      converterQuery(f);	    }	  }	  m_IOThread = null;	}      };      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    } else {      JOptionPane.showMessageDialog(this,				    "Can't load at this time,\n"				    + "currently busy with other IO",				    "Load Instances",				    JOptionPane.WARNING_MESSAGE);    }  }    /**   * Loads instances from a database   *   * @param iq the InstanceQuery object to load from (this is assumed   * to have been already connected to a valid database).   */  public void setInstancesFromDB(final InstanceQuery iq) {    if (m_IOThread == null) {      m_IOThread = new Thread() {	public void run() {	  	  try {	    m_Log.statusMessage("Reading from database...");	    final Instances i = iq.retrieveInstances();	    SwingUtilities.invokeAndWait(new Runnable() {	      public void run() {		setInstances(new Instances(i));	      }	    });	    iq.disconnectFromDatabase();	  } catch (Exception ex) {	    m_Log.statusMessage("Problem executing DB query "+m_SQLQ);	    JOptionPane.showMessageDialog(PreprocessPanel.this,					  "Couldn't read from database:\n"					  + ex.getMessage(),					  "Load Instances",					  JOptionPane.ERROR_MESSAGE);	  }	   m_IOThread = null;	}      };      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    } else {       JOptionPane.showMessageDialog(this,				    "Can't load at this time,\n"				    + "currently busy with other IO",				    "Load Instances",				    JOptionPane.WARNING_MESSAGE);    }  }  /**   * Loads instances from a URL.   *   * @param u the URL to load from.   */  public void setInstancesFromURL(final URL u) {    if (m_IOThread == null) {      m_IOThread = new Thread() {	public void run() {	  try {	    m_Log.statusMessage("Reading from URL...");	    Reader r = new BufferedReader(		       new InputStreamReader(u.openStream()));	    setInstances(new Instances(r));	    r.close();	  } catch (Exception ex) {	    ex.printStackTrace();	    m_Log.statusMessage("Problem reading " + u);	    JOptionPane.showMessageDialog(PreprocessPanel.this,					  "Couldn't read from URL:\n"					  + u + "\n"					  + ex.getMessage(),					  "Load Instances",					  JOptionPane.ERROR_MESSAGE);	  }	  m_IOThread = null;	}      };      m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority      m_IOThread.start();    } else {      JOptionPane.showMessageDialog(this,				    "Can't load at this time,\n"				    + "currently busy with other IO",				    "Load Instances",				    JOptionPane.WARNING_MESSAGE);    }  }  /**   * Backs up the current state of the dataset, so the changes can be undone.   */  public void addUndoPoint() throws Exception {        if (m_Instances != null) {      // create temporary file      File tempFile = File.createTempFile("weka", null);      tempFile.deleteOnExit();      ObjectOutputStream oos = 	new ObjectOutputStream(	new BufferedOutputStream(	new FileOutputStream(tempFile)));          oos.writeObject(m_Instances);      oos.flush();      oos.close();      // update undo file list      if (m_tempUndoFiles[m_tempUndoIndex] != null) {	// remove undo points that are too old	m_tempUndoFiles[m_tempUndoIndex].delete();      }      m_tempUndoFiles[m_tempUndoIndex] = tempFile;      if (++m_tempUndoIndex >= m_tempUndoFiles.length) {	// wrap pointer around	m_tempUndoIndex = 0;      }      m_UndoBut.setEnabled(true);    }  }  /**   * Reverts to the last backed up version of the dataset.   */  public void undo() {    if (--m_tempUndoIndex < 0) {      // wrap pointer around      m_tempUndoIndex = m_tempUndoFiles.length-1;    }        if (m_tempUndoFiles[m_tempUndoIndex] != null) {      // load instances from the temporary file      setInstancesFromFile(m_tempUndoFiles[m_tempUndoIndex]);      // update undo file list      m_tempUndoFiles[m_tempUndoIndex] = null;    }        // update undo button    int temp = m_tempUndoIndex-1;    if (temp < 0) {      temp = m_tempUndoFiles.length-1;    }    m_UndoBut.setEnabled(m_tempUndoFiles[temp] != null);  }    /**   * edits the current instances object in the viewer    */  public void edit() {    ViewerDialog        dialog;    int                 result;    Instances           copy;    Instances           newInstances;        final int classIndex = m_AttVisualizePanel.getColoringIndex();    copy   = new Instances(m_Instances);    copy.setClassIndex(classIndex);    dialog = new ViewerDialog(null);    result = dialog.showDialog(copy);    if (result == ViewerDialog.APPROVE_OPTION) {      try {        addUndoPoint();      }      catch (Exception e) {        e.printStackTrace();      }      // if class was not set before, reset it again after use of filter      newInstances = dialog.getInstances();      if (m_Instances.classIndex() < 0)        newInstances.setClassIndex(-1);      setInstances(newInstances);    }  }    /**   * Tests out the instance-preprocessing panel from the command line.   *   * @param args ignored   */  public static void main(String [] args) {    try {      final JFrame jf = new JFrame("Weka Explorer: Preprocess");      jf.getContentPane().setLayout(new BorderLayout());      final PreprocessPanel sp = new PreprocessPanel();      jf.getContentPane().add(sp, BorderLayout.CENTER);      weka.gui.LogPanel lp = new weka.gui.LogPanel();      sp.setLog(lp);      jf.getContentPane().add(lp, BorderLayout.SOUTH);      jf.addWindowListener(new WindowAdapter() {	public void windowClosing(WindowEvent e) {	  jf.dispose();	  System.exit(0);	}      });      jf.pack();      jf.setSize(800, 600);      jf.setVisible(true);    } catch (Exception ex) {      ex.printStackTrace();      System.err.println(ex.getMessage());    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -