Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. package ca.pfv.spmf.test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.URL;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11.  
  12. import ca.pfv.spmf.algorithms.sequenceprediction.ipredict.database.Item;
  13. import ca.pfv.spmf.algorithms.sequenceprediction.ipredict.database.Sequence;
  14. import ca.pfv.spmf.algorithms.sequenceprediction.ipredict.database.SequenceDatabase;
  15. import ca.pfv.spmf.algorithms.sequenceprediction.ipredict.database.SequenceStatsGenerator;
  16. import ca.pfv.spmf.algorithms.sequenceprediction.ipredict.predictor.CPT.CPTPlus.CPTPlusPredictor;
  17.  
  18. /**
  19. * Example of how to use the CPT+ sequence prediction model in the source code.
  20. * Copyright 2015.
  21. */
  22. public class MainTestCPTPlus {
  23.  
  24. public static void main(String [] arg) throws IOException{
  25.  
  26. // Load the set of training sequences
  27. String inputPath = fileToPath("DATA20170214.txt");
  28. SequenceDatabase trainingSet = new SequenceDatabase();
  29. trainingSet.loadFileSPMFFormat(inputPath, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
  30.  
  31. // Print the training sequences to the console
  32. System.out.println("--- Training sequences ---");
  33. for(Sequence sequence : trainingSet.getSequences()) {
  34. System.out.println(sequence.toString());
  35. }
  36. System.out.println();
  37.  
  38. // Print statistics about the training sequences
  39. SequenceStatsGenerator.prinStats(trainingSet, " training sequences ");
  40.  
  41. // The following line is to set optional parameters for the prediction model.
  42. // We want to
  43. // activate the CCF and CBS strategies which generally improves its performance (see paper)
  44. String optionalParameters = "CCF:true CBS:true CCFmin:1 CCFmax:6 CCFsup:2 splitMethod:0 minPredictionRatio:1.0 noiseRatio:1.0";
  45. // List<List<Item>> itemsets = finder.findFrequentItemsets(trainingSequences, parameters.paramInt("CCFmin"), parameters.paramInt("CCFmax"), parameters.paramInt("CCFsup"));
  46.  
  47. // Train the prediction model
  48. CPTPlusPredictor predictionModel = new CPTPlusPredictor("CPT+", optionalParameters);
  49. predictionModel.Train(trainingSet.getSequences());
  50.  
  51. // Now we will make a prediction.
  52. // We want to predict what would occur after the sequence <1, 3>.
  53. // We first create the sequence
  54.  
  55. FileReader fr = new FileReader("D:\\Test.txt");
  56. BufferedReader br = new BufferedReader(fr);
  57. String line,Teststring;
  58. String[] TestArray= new String[500000];
  59. String[][] MainTestArray = new String[30000][8];
  60. ArrayList myList = new ArrayList();
  61. int Q=0;
  62. int SequenceT=0,member=0;
  63. while((line = br.readLine())!=null)
  64. {
  65. //br.readLine()是指讀取txt檔的每一行資料,把讀到的資料存到line
  66. //再將line丟給Teststring去儲存
  67. Teststring = line;
  68.  
  69. //因為我這個test檔的資料格式是-->一行有N個字串,用空白隔開,
  70. //tempstring.split("\\s") 會依照空白鍵來切割,剛好切三個,所以這邊我的tempArray的大小才會宣告3
  71. TestArray = Teststring.split("\\s");
  72.  
  73. //這邊就是按照順序,一行一行的儲存到動態陣列裡面
  74. for(Q=0;Q< TestArray.length;Q++)
  75. {
  76. if(TestArray[Q].equals(-2))
  77. {
  78. SequenceT = SequenceT + 1;
  79. member = 0;
  80. }
  81. else if(!TestArray[Q].equals(-1))
  82. {
  83. MainTestArray[SequenceT][member] = TestArray[Q];
  84. member = member+1;
  85. }
  86. }
  87. }
  88.  
  89.  
  90.  
  91. int i;
  92. int Test;
  93. int r=0;
  94. for(r=0;r<MainTestArray[SequenceT].length;r++)
  95. {
  96. String OutputTest[] = new String[30000] ;
  97. for (i = 1; i <= 30000; i++)
  98. {
  99. Scanner cin = new Scanner(MainTestArray[r].length);
  100. Test = 2;
  101. while(Test != 1)
  102. {
  103. Sequence sequence = new Sequence(0);
  104. int n = cin.nextInt();
  105. for(int x = 0 ; x<n ; x++)
  106. {
  107. sequence.addItem(new Item(cin.nextInt()));
  108. }
  109. Sequence thePredicition = predictionModel.Predict(sequence);
  110. // System.out.println("Prediction" + thePredicition);
  111. String X = String.valueOf(thePredicition);
  112. Test = 1;
  113. OutputTest[i-1] = X.substring(9,11);
  114. }
  115. // System.out.println(OutputTest[i-1]);
  116. //break;
  117.  
  118. }
  119.  
  120. // \r\n next line symbol
  121. FileWriter fw = new FileWriter("D:\\OutputTest.txt");
  122. for(int count=1 ; count <= 30000 ; count++)
  123. {
  124. fw.write(OutputTest[i-1] + "\r\n");
  125. }
  126. fw.close();
  127. }
  128.  
  129. }
  130.  
  131. public static String fileToPath(String filename) throws UnsupportedEncodingException{
  132. URL url = MainTestCPTPlus.class.getResource(filename);
  133. return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement