Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Beans;
  7.  
  8. import java.util.ArrayList;
  9. import javax.faces.application.FacesMessage;
  10. import javax.faces.bean.ManagedBean;
  11. import javax.faces.bean.RequestScoped;
  12. import javax.faces.context.FacesContext;
  13. import weka.classifiers.Classifier;
  14. import weka.core.Attribute;
  15. import weka.core.DenseInstance;
  16. import weka.core.Instance;
  17. import weka.core.Instances;
  18. import weka.core.SparseInstance;
  19.  
  20. /**
  21. *
  22. * @author Kaprawiec
  23. */
  24. @ManagedBean
  25. @RequestScoped
  26. public class Classify {
  27.  
  28. public String getLCORE() {
  29. return LCORE;
  30. }
  31.  
  32. public void setLCORE(String LCORE) {
  33. this.LCORE = LCORE;
  34. }
  35.  
  36. public String getLSURF() {
  37. return LSURF;
  38. }
  39.  
  40. public void setLSURF(String LSURF) {
  41. this.LSURF = LSURF;
  42. }
  43.  
  44. public String getLO2() {
  45. return LO2;
  46. }
  47.  
  48. public void setLO2(String LO2) {
  49. this.LO2 = LO2;
  50. }
  51.  
  52. public String getLBP() {
  53. return LBP;
  54. }
  55.  
  56. public void setLBP(String LBP) {
  57. this.LBP = LBP;
  58. }
  59.  
  60. public String getSURFSTBL() {
  61. return SURFSTBL;
  62. }
  63.  
  64. public void setSURFSTBL(String SURFSTBL) {
  65. this.SURFSTBL = SURFSTBL;
  66. }
  67.  
  68. public String getCORESTBL() {
  69. return CORESTBL;
  70. }
  71.  
  72. public void setCORESTBL(String CORESTBL) {
  73. this.CORESTBL = CORESTBL;
  74. }
  75.  
  76. public String getBPSTBL() {
  77. return BPSTBL;
  78. }
  79.  
  80. public void setBPSTBL(String BPSTBL) {
  81. this.BPSTBL = BPSTBL;
  82. }
  83.  
  84. public int getCOMFORT() {
  85. return COMFORT;
  86. }
  87.  
  88. /**
  89. * Creates a new instance of java
  90. */
  91. public void setCOMFORT(int COMFORT) {
  92. this.COMFORT = COMFORT;
  93. }
  94.  
  95. private String LCORE;
  96. private String LSURF;
  97. private String LO2;
  98. private String LBP;
  99. private String SURFSTBL;
  100. private String CORESTBL;
  101. private String BPSTBL;
  102. private int COMFORT;
  103.  
  104. public Classify() {
  105. }
  106. public void classify() throws Exception
  107. {
  108. FacesContext context = FacesContext.getCurrentInstance();
  109. Classifier cl = (Classifier) weka.core.SerializationHelper.read("d:\\weka\\model.model");
  110.  
  111.  
  112. // CREATE NOMINAL VALUES
  113. ArrayList<String> cls = new ArrayList<>();
  114. cls.add("I");
  115. cls.add("S");
  116. cls.add("A");
  117.  
  118. ArrayList<String> hml = new ArrayList<>();
  119. hml.add("high");
  120. hml.add("mid");
  121. hml.add("low");
  122.  
  123. ArrayList<String> egfp = new ArrayList<>();
  124. egfp.add("excellent");
  125. egfp.add("good");
  126. egfp.add("fair");
  127. egfp.add("poor");
  128.  
  129. ArrayList<String> smu = new ArrayList<>();
  130. smu.add("stable");
  131. smu.add("mod-stable");
  132. smu.add("unstable");
  133.  
  134. //CREATE ATTRIBUTES
  135. Attribute lcore = new Attribute("L-CORE",hml);
  136. Attribute lsurf = new Attribute("L-SURF",hml);
  137. Attribute lo2 = new Attribute("L-O2",egfp);
  138. Attribute lbp = new Attribute("L-BP",hml);
  139. Attribute surfstbl = new Attribute("SURF-STBL",smu);
  140. Attribute corestbl = new Attribute("CORE-STBL",smu);
  141. Attribute bpstbl = new Attribute("BP-STBL",smu);
  142. Attribute comfort = new Attribute("COMFORT");
  143. Attribute admdecs=new Attribute("ADM-DECS",cls);
  144.  
  145.  
  146. ArrayList<Attribute> attributes = new ArrayList<Attribute>();
  147. attributes.add(lcore);
  148. attributes.add(lsurf);
  149. attributes.add(lo2);
  150. attributes.add(lbp);
  151. attributes.add(surfstbl);
  152. attributes.add(corestbl);
  153. attributes.add(bpstbl);
  154. attributes.add(comfort);
  155. attributes.add(admdecs);
  156.  
  157. Instances dataset = new Instances("Test-dataset",attributes,0);
  158.  
  159. double[]values = new double[dataset.numAttributes()];
  160. values[0]=hml.indexOf(LCORE);
  161. values[1]=hml.indexOf(LSURF);
  162. values[2]=egfp.indexOf(LO2);
  163. values[3]=hml.indexOf(LBP);
  164. values[4]=smu.indexOf(SURFSTBL);
  165. values[5]=smu.indexOf(CORESTBL);
  166. values[6]=smu.indexOf(BPSTBL);
  167. values[7]=COMFORT;
  168. values[8]=0;
  169. for(double a : values)
  170. {
  171. System.out.print(a+" ");
  172. }
  173. System.out.print("\n");
  174.  
  175. Instance inst = new DenseInstance(1.0,values);
  176. dataset.add(inst);
  177.  
  178. System.out.println("$$$$$$$$$"+" "+(dataset.numAttributes()-1));
  179.  
  180.  
  181.  
  182.  
  183. System.out.println(LCORE+" "+LSURF+" "+LO2+" "+LBP+" "+SURFSTBL+" "+CORESTBL+" "+BPSTBL+" "+COMFORT);
  184. dataset.setClassIndex(dataset.numAttributes()-1);
  185. /* Instances nowy=new Instances(dataset);
  186.  
  187. double t = cl.classifyInstance(dataset.instance(0));*/
  188.  
  189.  
  190.  
  191.  
  192.  
  193. //////////////////////
  194. Instances nowy=new Instances(dataset);
  195.  
  196.  
  197.  
  198. for(int i=0; i<nowy.numInstances();++i)
  199. {
  200. double clsLAbel=cl.classifyInstance(dataset.instance(i));
  201. System.out.println("$$$$$$$$$"+" "+clsLAbel);
  202. nowy.instance(i).setClassValue(clsLAbel);
  203. }
  204.  
  205.  
  206. // TODO output your page here
  207. Instance x=nowy.instance(0);
  208. String wynik1 = Double.toString(x.value(admdecs));
  209. ///////////////////////
  210.  
  211. String wynik="";
  212. switch((int)x.value(admdecs))
  213. {
  214. case 0:
  215. wynik="Pacjent wysłany na intensywną terapie (klasyfikcja: I)";
  216. break;
  217. case 1:
  218. wynik="Pacjent zwolniony do domu (klasyfikcja: S)";
  219. break;
  220. case 2:
  221. wynik="Pacjent wysłany do szpitlnych sal (klasyfikcja: A)";
  222. break;
  223. }
  224. // TODO output your page here
  225.  
  226. context.addMessage(null, new FacesMessage(wynik));
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement