/** * evalutes the expression for the current iteration * * @param value the current iteration value (from 'min' to 'max' with * stepsize 'step') * @param isX true if X is to be evaluated otherwise Y * @return the generated value, NaN if the evaluation fails */ protected double evaluate(double value, boolean isX) { double result; HashMap symbols; String expr; double base; double min; double max; double step; if (isX) { expr = getXExpression(); base = getXBase(); min = getXMin(); max = getXMax(); step = getXStep(); } else { expr = getYExpression(); base = getYBase(); min = getYMin(); max = getYMax(); step = getYStep(); } try { symbols = new HashMap(); symbols.put("BASE", new Double(base)); symbols.put("FROM", new Double(min)); symbols.put("TO", new Double(max)); symbols.put("STEP", new Double(step)); symbols.put("I", new Double(value)); result = MathematicalExpression.evaluate(expr, symbols); } catch (Exception e) { result = Double.NaN; } return result; }
/** * initializes the filter with the given dataset, i.e., the kernel gets * built. Needs to be called before the first call of Filter.useFilter or * batchFinished(), if not the -F option (or setInitFile(File) is used). * * @param instances the data to initialize with * @throws Exception if building of kernel fails */ public void initFilter(Instances instances) throws Exception { HashMap symbols; // determine kernel factor symbols = new HashMap(); symbols.put("A", new Double(instances.numAttributes())); symbols.put("N", new Double(instances.numInstances())); m_KernelFactor = MathematicalExpression.evaluate(getKernelFactorExpression(), symbols); // init filters if (!m_checksTurnedOff) { m_Missing = new ReplaceMissingValues(); m_Missing.setInputFormat(instances); instances = Filter.useFilter(instances, m_Missing); } else { m_Missing = null; } if (getKernel().getCapabilities().handles(Capability.NUMERIC_ATTRIBUTES)) { boolean onlyNumeric = true; if (!m_checksTurnedOff) { for (int i = 0; i < instances.numAttributes(); i++) { if (i != instances.classIndex()) { if (!instances.attribute(i).isNumeric()) { onlyNumeric = false; break; } } } } if (!onlyNumeric) { m_NominalToBinary = new NominalToBinary(); m_NominalToBinary.setInputFormat(instances); instances = Filter.useFilter(instances, m_NominalToBinary); } else { m_NominalToBinary = null; } } else { m_NominalToBinary = null; } if ((m_Filter != null) && (m_Filter.getClass() != AllFilter.class)) { m_ActualFilter = Filter.makeCopy(m_Filter); m_ActualFilter.setInputFormat(instances); instances = Filter.useFilter(instances, m_ActualFilter); } else { m_ActualFilter = null; } m_NumTrainInstances = instances.numInstances(); // set factor for kernel m_ActualKernel = Kernel.makeCopy(m_Kernel); if (m_ActualKernel instanceof RBFKernel) ((RBFKernel) m_ActualKernel).setGamma( m_KernelFactor * ((RBFKernel) m_ActualKernel).getGamma()); // build kernel m_ActualKernel.buildKernel(instances); m_Initialized = true; }
/** * initializes the filter with the given dataset, i.e., the kernel gets built. * Needs to be called before the first call of Filter.useFilter or * batchFinished(), if not the -F option (or setInitFile(File) is used). * * @param instances the data to initialize with * @throws Exception if building of kernel fails */ public void initFilter(Instances instances) throws Exception { HashMap<String, Double> symbols; // determine kernel factor symbols = new HashMap<String, Double>(); symbols.put("A", new Double(instances.numAttributes())); symbols.put("N", new Double(instances.numInstances())); m_KernelFactor = MathematicalExpression.evaluate( getKernelFactorExpression(), symbols); // init filters if (!m_checksTurnedOff) { m_Missing = new ReplaceMissingValues(); m_Missing.setInputFormat(instances); instances = Filter.useFilter(instances, m_Missing); } else { m_Missing = null; } if (getKernel().getCapabilities().handles(Capability.NUMERIC_ATTRIBUTES)) { boolean onlyNumeric = true; if (!m_checksTurnedOff) { for (int i = 0; i < instances.numAttributes(); i++) { if (i != instances.classIndex()) { if (!instances.attribute(i).isNumeric()) { onlyNumeric = false; break; } } } } if (!onlyNumeric) { m_NominalToBinary = new NominalToBinary(); m_NominalToBinary.setInputFormat(instances); instances = Filter.useFilter(instances, m_NominalToBinary); } else { m_NominalToBinary = null; } } else { m_NominalToBinary = null; } if ((m_Filter != null) && (m_Filter.getClass() != AllFilter.class)) { m_ActualFilter = Filter.makeCopy(m_Filter); m_ActualFilter.setInputFormat(instances); instances = Filter.useFilter(instances, m_ActualFilter); } else { m_ActualFilter = null; } m_NumTrainInstances = instances.numInstances(); // set factor for kernel m_ActualKernel = Kernel.makeCopy(m_Kernel); if (m_ActualKernel instanceof RBFKernel) { ((RBFKernel) m_ActualKernel).setGamma(m_KernelFactor * ((RBFKernel) m_ActualKernel).getGamma()); } // build kernel m_ActualKernel.buildKernel(instances); m_Initialized = true; }