📄 gaussianprocesses.java
字号:
m_Filter.input(inst); m_Filter.batchFinished(); inst = m_Filter.output(); } // Build K vector weka.core.matrix.Matrix k = new weka.core.matrix.Matrix(m_data.numInstances(),1); for (int i = 0; i < m_data.numInstances(); i++) k.set(i,0,m_kernel.eval(-1,i,inst)); double result = k.transpose().times(m_t).get(0,0)+m_avg_target; return result; } /** * Predicts a confidence interval for the given instance and confidence level. * * @param inst the instance to make the prediction for * @param confidenceLevel the percentage of cases the interval should cover * @return a 1*2 array that contains the boundaries of the interval * @throws Exception if interval could not be estimated * successfully */ public double[][] predictInterval(Instance inst, double confidenceLevel) 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(); } // Build K vector (and Kappa) weka.core.matrix.Matrix k = new weka.core.matrix.Matrix(m_data.numInstances(),1); for (int i = 0; i < m_data.numInstances(); i++) k.set(i,0,m_kernel.eval(-1,i,inst)); double kappa = m_kernel.eval(-1,-1,inst) + m_delta*m_delta; double estimate = k.transpose().times(m_t).get(0,0)+m_avg_target; double sigma = Math.sqrt(kappa - k.transpose().times(m_C).times(k).get(0,0)); confidenceLevel = 1.0 - ((1.0 - confidenceLevel)/2.0); double z = Statistics.normalInverse(confidenceLevel); double[][] interval = new double[1][2]; interval[0][0] = estimate - z * sigma; interval[0][1] = estimate + z * sigma; return interval; } /** * Gives the variance of the prediction at the given instance * * @param inst the instance to get the variance for * @return tha variance * @throws Exception if computation fails */ public double getStandardDeviation(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_Alin = 1.0; m_Blin = 0.0; m_NominalToBinary.batchFinished(); inst = m_NominalToBinary.output(); } if (m_Filter != null) { m_Filter.input(inst); m_Filter.batchFinished(); inst = m_Filter.output(); } weka.core.matrix.Matrix k = new weka.core.matrix.Matrix(m_data.numInstances(),1); for (int i = 0; i < m_data.numInstances(); i++) k.set(i,0,m_kernel.eval(-1,i,inst)); double kappa = m_kernel.eval(-1,-1,inst) + m_delta*m_delta; double var = kappa - k.transpose().times(m_C).times(k).get(0,0); if (var < 0) System.out.println("Aiaiai: variance is negative (" + var + ")!!!"); double sigma = Math.sqrt(var); return sigma; } /** * 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( "\tLevel of Gaussian Noise." + " (default 0.1)", "L", 1, "-L <double>")); result.addElement(new Option( "\tWhether to 0=normalize/1=standardize/2=neither. " + "(default 0=normalize)", "N", 1, "-N")); 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-start --> * Valid options are: <p/> * * <pre> -D * If set, classifier is run in debug mode and * may output additional info to the console</pre> * * <pre> -L <double> * Level of Gaussian Noise. (default 0.1)</pre> * * <pre> -N * Whether to 0=normalize/1=standardize/2=neither. (default 0=normalize)</pre> * * <pre> -K <classname and parameters> * The Kernel to use. * (default: weka.classifiers.functions.supportVector.PolyKernel)</pre> * * <pre> * Options specific to kernel weka.classifiers.functions.supportVector.RBFKernel: * </pre> * * <pre> -D * Enables debugging output (if available) to be printed. * (default: off)</pre> * * <pre> -no-checks * Turns off all checks - use with caution! * (default: checks on)</pre> * * <pre> -C <num> * The size of the cache (a prime number). * (default: 250007)</pre> * * <pre> -G <num> * The Gamma parameter. * (default: 0.01)</pre> * <!-- 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; tmpStr = Utils.getOption('L', options); if (tmpStr.length() != 0) setNoise(Double.parseDouble(tmpStr)); else setNoise(0.1); 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]); result.add("-L"); result.add("" + getNoise()); result.add("-N"); result.add("" + m_filterType); result.add("-K"); result.add("" + m_kernel.getClass().getName() + " " + Utils.joinOptions(m_kernel.getOptions())); return (String[]) result.toArray(new String[result.size()]); } /** * 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 new 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.2200Instances * * @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 noiseTipText() { return "The level of Gaussian Noise (added to the diagonal of the Covariance Matrix)."; } /** * Get the value of noise. * * @return Value of noise. */ public double getNoise() { return m_delta; } /** * Set the level of Gaussian Noise. * * @param v Value to assign to noise. */ public void setNoise(double v) { m_delta = v; } /** * Prints out the classifier. * * @return a description of the classifier as a string */ public String toString() { StringBuffer text = new StringBuffer(); if (m_t == null) return "Gaussian Processes: No model built yet."; try { text.append("Gaussian Processes\n\n"); text.append("Kernel used:\n " + m_kernel.toString() + "\n\n"); text.append("Average Target Value : " + m_avg_target + "\n"); text.append("Inverted Covariance Matrix:\n"); double min = m_C.get(0,0); double max = m_C.get(0,0); for (int i = 0; i < m_data.numInstances(); i++) for (int j = 0; j < m_data.numInstances(); j++) { if (m_C.get(i,j) < min) min = m_C.get(i,j); else if (m_C.get(i,j) > max) max = m_C.get(i,j); } text.append(" Lowest Value = " + min + "\n"); text.append(" Highest Value = " + max + "\n"); text.append("Inverted Covariance Matrix * Target-value Vector:\n"); min = m_t.get(0,0); max = m_t.get(0,0); for (int i = 0; i < m_data.numInstances(); i++) { if (m_t.get(i,0) < min) min = m_t.get(i,0); else if (m_t.get(i,0) > max) max = m_t.get(i,0); } text.append(" Lowest Value = " + min + "\n"); text.append(" Highest Value = " + max + "\n \n"); } catch (Exception e) { return "Can't print the classifier."; } return text.toString(); } /** * Main method for testing this class. * * @param argv the commandline parameters */ public static void main(String[] argv) { Classifier scheme; try { scheme = new GaussianProcesses(); System.out.println(Evaluation.evaluateModel(scheme, argv)); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -