📄 smoreg.java
字号:
// checkAlphas(); // Compute (i_low, b_low) and (i_up, b_up) by applying the conditions // mentionned above, using only i1, i2 and indices in I_0 m_bLow = -Double.MAX_VALUE; m_bUp = Double.MAX_VALUE; m_iLow =-1; m_iUp = -1; for (int i = m_I0.getNext(-1); i != -1; i = m_I0.getNext(i)) { if(0 < m_alpha_[i] && m_alpha_[i] < m_C * m_data.instance(i).weight() && (m_fcache[i] + m_epsilon > m_bLow)){ m_bLow = m_fcache[i] + m_epsilon; m_iLow = i; } else if(0 < m_alpha[i] && m_alpha[i] < m_C * m_data.instance(i).weight() && (m_fcache[i] - m_epsilon > m_bLow)){ m_bLow = m_fcache[i] - m_epsilon; m_iLow = i; } if(0 < m_alpha[i] && m_alpha[i] < m_C * m_data.instance(i).weight() && (m_fcache[i] - m_epsilon < m_bUp)){ m_bUp = m_fcache[i] - m_epsilon; m_iUp = i; } else if(0 < m_alpha_[i] && m_alpha_[i] < m_C * m_data.instance(i).weight() && (m_fcache[i] + m_epsilon < m_bUp)){ m_bUp = m_fcache[i] + m_epsilon; m_iUp = i; } } if(!m_I0.contains(i1)){ if(m_I2.contains(i1) && (m_fcache[i1] + m_epsilon > m_bLow)){ m_bLow = m_fcache[i1] + m_epsilon; m_iLow = i1; } else if (m_I1.contains(i1) && (m_fcache[i1] - m_epsilon > m_bLow)){ m_bLow = m_fcache[i1] - m_epsilon; m_iLow = i1; } if(m_I3.contains(i1) && (m_fcache[i1] - m_epsilon < m_bUp)){ m_bUp = m_fcache[i1] - m_epsilon; m_iUp = i1; } else if (m_I1.contains(i1) && (m_fcache[i1] + m_epsilon < m_bUp)){ m_bUp = m_fcache[i1] + m_epsilon; m_iUp = i1; } } if(!m_I0.contains(i2)){ if(m_I2.contains(i2) && (m_fcache[i2] + m_epsilon > m_bLow)){ m_bLow = m_fcache[i2] + m_epsilon; m_iLow = i2; } else if (m_I1.contains(i2) && (m_fcache[i2] - m_epsilon > m_bLow)){ m_bLow = m_fcache[i2] - m_epsilon; m_iLow = i2; } if(m_I3.contains(i2) && (m_fcache[i2] - m_epsilon < m_bUp)){ m_bUp = m_fcache[i2] - m_epsilon; m_iUp = i2; } else if (m_I1.contains(i2) && (m_fcache[i2] + m_epsilon < m_bUp)){ m_bUp = m_fcache[i2] + m_epsilon; m_iUp = i2; } } if(m_iLow == -1 || m_iUp == -1){ throw new RuntimeException("Fatal error! The program failled to " + "initialize i_Low, iUp."); } return true; } else { return false; } } /** * Classifies a given instance. * * @param inst the instance to be classified * @return the classification * @throws Exception if instance could not be classified * successfully */ public double classifyInstance(Instance inst) throws Exception { // Filter instance if (!m_checksTurnedOff) { m_Missing.input(inst); m_Missing.batchFinished(); inst = m_Missing.output(); } if (m_NominalToBinary != null) { m_NominalToBinary.input(inst); m_NominalToBinary.batchFinished(); inst = m_NominalToBinary.output(); } if (m_Filter != null) { m_Filter.input(inst); m_Filter.batchFinished(); inst = m_Filter.output(); } // classification : double result = m_b; // Is the machine linear? if (m_KernelIsLinear) { // Is weight vector stored in sparse format? if (m_sparseWeights == null) { int n1 = inst.numValues(); for (int p = 0; p < n1; p++) { if (inst.index(p) != m_classIndex) { result += m_weights[inst.index(p)] * inst.valueSparse(p); } } } else { int n1 = inst.numValues(); int n2 = m_sparseWeights.length; for (int p1 = 0, p2 = 0; p1 < n1 && p2 < n2;) { int ind1 = inst.index(p1); int ind2 = m_sparseIndices[p2]; if (ind1 == ind2) { if (ind1 != m_classIndex) { result += inst.valueSparse(p1) * m_sparseWeights[p2]; } p1++; p2++; } else if (ind1 > ind2) { p2++; } else { p1++; } } } } else { for (int i = 0; i < m_alpha.length; i++) { result += (m_alpha[i] - m_alpha_[i]) * m_kernel.eval(-1, i, inst); } } // apply the inverse transformation // (due to the normalization/standardization) return (result - m_Blin) / m_Alin; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); Enumeration enm = super.listOptions(); while (enm.hasMoreElements()) result.addElement(enm.nextElement()); result.addElement(new Option( "\tTurns off all checks - use with caution!\n" + "\tTurning them off assumes that data is purely numeric, doesn't\n" + "\tcontain any missing values, and has a nominal class. Turning them\n" + "\toff also means that no header information will be stored if the\n" + "\tmachine is linear. Finally, it also assumes that no instance has\n" + "\ta weight equal to 0.\n" + "\t(default: checks on)", "no-checks", 0, "-no-checks")); result.addElement(new Option( "\tThe amount up to which deviations are\n" + "\ttolerated (epsilon). (default 1e-3)", "S", 1, "-S <double>")); result.addElement(new Option( "\tThe complexity constant C. (default 1)", "C", 1, "-C <double>")); result.addElement(new Option( "\tWhether to 0=normalize/1=standardize/2=neither. " + "(default 0=normalize)", "N", 1, "-N")); result.addElement(new Option( "\tThe tolerance parameter. " + "(default 1.0e-3)", "T", 1, "-T <double>")); result.addElement(new Option( "\tThe epsilon for round-off error. " + "(default 1.0e-12)", "P", 1, "-P <double>")); result.addElement(new Option( "\tThe Kernel to use.\n" + "\t(default: weka.classifiers.functions.supportVector.PolyKernel)", "K", 1, "-K <classname and parameters>")); result.addElement(new Option( "", "", 0, "\nOptions specific to kernel " + getKernel().getClass().getName() + ":")); enm = ((OptionHandler) getKernel()).listOptions(); while (enm.hasMoreElements()) result.addElement(enm.nextElement()); return result.elements(); } /** * Parses a given list of options. <p/> * <!-- options-end --> <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; String[] tmpOptions; setChecksTurnedOff(Utils.getFlag("no-checks", options)); tmpStr = Utils.getOption('S', options); if (tmpStr.length() != 0) setEpsilon(Double.parseDouble(tmpStr)); else setEpsilon(1e-3); tmpStr = Utils.getOption('C', options); if (tmpStr.length() != 0) setC(Double.parseDouble(tmpStr)); else setC(1.0); tmpStr = Utils.getOption('T', options); if (tmpStr.length() != 0) setToleranceParameter(Double.parseDouble(tmpStr)); else setToleranceParameter(1.0e-3); tmpStr = Utils.getOption('P', options); if (tmpStr.length() != 0) setEps(Double.parseDouble(tmpStr)); else setEps(1.0e-12); tmpStr = Utils.getOption('N', options); if (tmpStr.length() != 0) setFilterType(new SelectedTag(Integer.parseInt(tmpStr), TAGS_FILTER)); else setFilterType(new SelectedTag(FILTER_NORMALIZE, TAGS_FILTER)); tmpStr = Utils.getOption('K', options); tmpOptions = Utils.splitOptions(tmpStr); if (tmpOptions.length != 0) { tmpStr = tmpOptions[0]; tmpOptions[0] = ""; setKernel(Kernel.forName(tmpStr, tmpOptions)); } super.setOptions(options); } /** * Gets the current settings of the classifier. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { int i; Vector result; String[] options; result = new Vector(); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); if (getChecksTurnedOff()) result.add("-no-checks"); result.add("-S"); result.add("" + getEpsilon()); result.add("-C"); result.add("" + getC()); result.add("-T"); result.add("" + getToleranceParameter()); result.add("-P"); result.add("" + getEps()); result.add("-N"); result.add("" + m_filterType); result.add("-K"); result.add("" + getKernel().getClass().getName() + " " + Utils.joinOptions(getKernel().getOptions())); return (String[]) result.toArray(new String[result.size()]); } /** * Disables or enables the checks (which could be time-consuming). Use with * caution! * * @param value if true turns off all checks */ public void setChecksTurnedOff(boolean value) { if (value) turnChecksOff(); else turnChecksOn(); } /** * Returns whether the checks are turned off or not. * * @return true if the checks are turned off */ public boolean getChecksTurnedOff() { return m_checksTurnedOff; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String checksTurnedOffTipText() { return "Turns time-consuming checks off - use with caution."; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String kernelTipText() { return "The kernel to use."; } /** * Gets the kernel to use. * * @return the kernel */ public Kernel getKernel() { return m_kernel; } /** * Sets the kernel to use. * * @param value the kernel */ public void setKernel(Kernel value) { m_kernel = value; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String filterTypeTipText() { return "Determines how/if the data will be transformed."; } /** * Gets how the training data will be transformed. Will be one of * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. * * @return the filtering mode */ public SelectedTag getFilterType() { return new SelectedTag(m_filterType, TAGS_FILTER); } /** * Sets how the training data will be transformed. Should be one of * FILTER_NORMALIZE, FILTER_STANDARDIZE, FILTER_NONE. * * @param newType the new filtering mode */ public void setFilterType(SelectedTag newType) { if (newType.getTags() == TAGS_FILTER) { m_filterType = newType.getSelectedTag().getID(); } } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String cTipText() { return "The complexity parameter C."; } /** * Get the value of C. * * @return Value of C. */ public double getC() { return m_C; } /** * Set the value of C. * * @param v Value to assign to C. */ public void setC(double v) { m_C = v; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String toleranceParameterTipText() { return "The tolerance parameter (shouldn't be changed)."; } /** * Get the value of tolerance parameter. * @return Value of tolerance parameter. */ public double getToleranceParameter() { return m_tol; } /** * Set the value of tolerance parameter. * @param v Value to assign to tolerance parameter. */ public void setToleranceParameter(double v) { m_tol = v; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String epsTipText() { return "The epsilon for round-off error (shouldn't be changed)."; } /** * Get the value of eps. * @return Value of eps. */ public double getEps() { return m_eps; } /** * Set the value of eps. * @param v Value to assign to epsilon. */ public void setEps(double v) { m_eps = v; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String epsilonTipText() { return "The amount up to which deviations are tolerated. " + "Watch out, the value of epsilon is used with the (normalized/standardized) " + "data."; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -