📄 rsw.java
字号:
if (m_Classifier != null) { try { vec.addElement(new Option("", "", 0, "\nOptions specific to classifier " + m_Classifier.getClass().getName() + ":")); Enumeration enum = ((OptionHandler)m_Classifier).listOptions(); while (enum.hasMoreElements()) { vec.addElement(enum.nextElement()); } } catch (Exception e) { } } return vec.elements(); } /** * Parses a given list of options. Valid options are:<p> * * * -W classifier <br> * Sets the base classifier (required).<p> * * -A lic <br> * Sets the left input context. <p> * * -B ric <br> * Sets the right input context <p> * * -Y loc <br> * Sets the left output context. <p> * * -Z roc <br> * Sets the right output context. <p> * * @param options the list of options as an array of strings * @exception Exception if an option is not supported * */ public void setOptions(String[] options) throws Exception { String licString = Utils.getOption('A', options); if (licString.length() != 0) { setLic(Integer.parseInt(licString)); } else { setLic(3); } String ricString = Utils.getOption('B', options); if (ricString.length() != 0) { setRic(Integer.parseInt(ricString)); } else { setRic(3); } String locString = Utils.getOption('Y', options); if (locString.length() != 0) { setLoc(Integer.parseInt(locString)); } else { setLoc(0); } String rocString = Utils.getOption('Z', options); if (rocString.length() != 0) { setRoc(Integer.parseInt(rocString)); } else { setRoc(3); } if((loc*roc) != 0) { throw new Exception("One of the output contexts has to be zero"); } if((lic < 0) || (ric < 0) || (loc < 0) || (roc < 0)) { throw new Exception("Windowizing contexts cannot be negative"); } String classifierName = Utils.getOption('W', options); if (classifierName.length() == 0) { throw new Exception("A classifier must be specified with" + " the -W option."); } setClassifier((Classifier) Classifier.forName(classifierName, Utils.partitionOptions(options))); } /** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions * */ public String [] getOptions() { String [] classifierOptions = new String[0]; if ((m_Classifier != null) && (m_Classifier instanceof OptionHandler)) { classifierOptions = ((OptionHandler)m_Classifier).getOptions(); } String [] options = new String[classifierOptions.length + 20]; int current = 0; options[current++] = "-A"; options[current++] = "" + lic; options[current++] = "-B"; options[current++] = "" + ric; options[current++] = "-Y"; options[current++] = "" + loc; options[current++] = "-Z"; options[current++] = "" + roc; if (getClassifier() != null) { options[current++] = "-W"; options[current++] = getClassifier().getClass().getName(); } options[current++] = "--"; System.arraycopy(classifierOptions, 0, options, current, classifierOptions.length); current += classifierOptions.length; while (current < options.length) { options[current++] = ""; } return options; } /** * returns the value of the distribution classifier. */ public Classifier getClassifier() { return (m_Classifier); } /** * returns the value of the left input context. */ public int getLic() { return (lic); } /** * returns the value of the right input context. */ public int getRic() { return (ric); } /** * returns the value of the left output context. */ public int getLoc() { return (loc); } /** * returns the value of the right output context. */ public int getRoc() { return (roc); } /** * sets the value of the distribution classifier. */ public void setClassifier(Classifier cf) { m_Classifier = cf; } /** * sets the value of the left input context. */ public void setLic(int num) { lic = num; } /** * sets the value of the right input context. */ public void setRic(int num) { ric = num; } /** * sets the value of the left output context. */ public void setLoc(int num) { loc = num; } /** * sets the value of the right output context. */ public void setRoc(int num) { roc = num; } /** * Classifies all instances * @param instances the test instances */ public double [][] distributionForSequence(Instances instances) throws Exception { Filter window = new Windowise(lic, ric, loc, roc); window.setInputFormat(instances); Instances windowed = Filter.useFilter(instances, window); windowed.sort(1); windowed.deleteAttributeAt(0); windowed.deleteAttributeAt(0); double [] line; double temp; int startWrite = 0, startRead, tempDisp = 0, lineLen = 0; int winNumClasses = m_Data.numClasses(); double [][] result = new double [windowed.numInstances()][winNumClasses]; int classifiedSoFar = 0; if(loc != 0) { startWrite = classIndexStart-loc; tempDisp = -1; lineLen = loc - 1; } if (roc != 0){ startWrite = classIndexStart+2; tempDisp = 1; lineLen = roc-1; } line = new double[lineLen]; startRead = classIndexStart - loc + 1; double [] classes = new double[windowed.numInstances()]; temp = winNumClasses - 1; for(int i = 0; i<lineLen; i++) { line[i] = winNumClasses - 1; } if(loc != 0) { for (int k = windowed.numInstances() - 1; k >= 0; k--) { Instance inst = (Instance)windowed.instance(k); inst.setValue(classIndexStart+tempDisp, temp); inst = overWrite(inst, line, startWrite); inst.setDataset(windowed); double [] probDist = ((Classifier)m_Classifier).distributionForInstance(inst); temp = Utils.maxIndex(probDist); line = overRead(inst, startRead, startRead+lineLen); result[windowed.numInstances()-k] = probDist; } } else { for (int k = 0; k < windowed.numInstances(); k++) { Instance inst = (Instance)windowed.instance(k); inst.setValue(classIndexStart+tempDisp, temp); inst = overWrite(inst, line, startWrite); inst.setDataset(m_Data/*windowed*/); double [] probDist = ((Classifier)m_Classifier).distributionForInstance(inst); temp = Utils.maxIndex(probDist); line = overRead(inst, startRead, startRead+lineLen); result[windowed.numInstances()-k-1] = probDist; } } return result; } /** * Classifies a given sequence. * * @param instance the instances to be classified * @return the predicted sequence, as an array of class labels */ public double[] classifySequence(Instances insts) throws Exception { int count = insts.numInstances(); double [] pred = new double[count]; double [][] dist = distributionForSequence(insts); for(int i = 0; i<count; i++) { pred[i] = Utils.maxIndex(dist[i]); } return pred;} /** * The main function for testing this class */ public static void main(String args[]) { try { System.out.println(SequentialEvaluation. evaluateModel(new RSW(), args)); } catch (Exception e) { System.err.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -