Advertisement
Mouamle

Split

Mar 25th, 2020
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. import weka.core.Instance;
  2. import weka.core.Instances;
  3. import weka.core.converters.ArffSaver;
  4. import weka.core.converters.ConverterUtils;
  5. import weka.core.converters.Saver;
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.List;
  12. import java.util.Random;
  13.  
  14. public class Split {
  15.  
  16.     private File testFile;
  17.     private File trainFile;
  18.  
  19.     private Split(String fileName, long seed, double split, int iterations) throws Exception {
  20.  
  21.         ConverterUtils.DataSource dataSource = new ConverterUtils.DataSource("split input/" + fileName);
  22.         Instances instances = dataSource.getDataSet();
  23.  
  24.         List<Instance> instanceList = new ArrayList<>(instances);
  25.  
  26.         int length = Math.round((float) split * instances.size());
  27.  
  28.         Random random = new Random(seed);
  29.         for (int i = 1; i < iterations + 1; i++) {
  30.             prepareFiles(fileName, i);
  31.  
  32.             Collections.shuffle(instanceList, random);
  33.  
  34.             ArffSaver trainSaver = new ArffSaver();
  35.             trainSaver.setStructure(instances);
  36.             trainSaver.setRetrieval(Saver.INCREMENTAL);
  37.             trainSaver.setFile(trainFile);
  38.             for (int index = 0; index < length; index++) {
  39.                 trainSaver.writeIncremental(instanceList.get(index));
  40.             }
  41.             trainSaver.getWriter().flush();
  42.             trainSaver.getWriter().close();
  43.  
  44.             ArffSaver testSaver = new ArffSaver();
  45.             testSaver.setStructure(instances);
  46.             testSaver.setRetrieval(Saver.INCREMENTAL);
  47.             testSaver.setFile(testFile);
  48.             for (int _index = 0; _index < instances.size() - length; _index++) {
  49.                 int index = length + _index;
  50.                 testSaver.writeIncremental(instanceList.get(index));
  51.             }
  52.             testSaver.getWriter().flush();
  53.             testSaver.getWriter().close();
  54.         }
  55.     }
  56.  
  57.     public static void main(String[] args) throws Exception {
  58.         new Split("Glass.arff", 5, 0.66, 5);
  59.     }
  60.  
  61.     private void prepareFiles(String fileName, int iteration) throws IOException {
  62.         String outputName = fileName.substring(0, fileName.lastIndexOf("."));
  63.  
  64.         File dir = new File(String.format("split/%s", outputName));
  65.  
  66.         testFile = new File(String.format("%s/%s_test_%d.arff", dir.getPath(), outputName, iteration));
  67.         trainFile = new File(String.format("%s/%s_train_%d.arff", dir.getPath(), outputName, iteration));
  68.  
  69.  
  70.         if (dir.mkdirs()) {
  71.             testFile.createNewFile();
  72.             trainFile.createNewFile();
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement