📄 paceregression.java
字号:
double[][] result = new double[numInstances] [numAttributes]; for (int i = 0; i < numInstances; i++) { Instance inst = data.instance(i); result[i][0] = 1.0; // the class value (lies on index middle) is left out for (int j = 0; j < middle; j++) { result[i][j + 1] = inst.value(j); } for (int j = middle + 1; j < numAttributes; j++) { result[i][j] = inst.value(j); } } return result; } /** * Classifies the given instance using the linear regression function. * * @param instance the test instance * @return the classification * @exception Exception if classification can't be done successfully */ public double classifyInstance(Instance instance) throws Exception { if (m_Coefficients == null) { throw new Exception("Pace Regression: No model built yet."); } // check for missing data and throw exception if some are found if (checkForMissing(instance, m_Model)) { throw new NoSupportForMissingValuesException("Can't handle missing values!"); } // Calculate the dependent variable from the regression model return regressionPrediction(instance, m_Coefficients); } /** * Outputs the linear regression model as a string. */ public String toString() { if (m_Coefficients == null) { return "Pace Regression: No model built yet."; } // try { StringBuffer text = new StringBuffer(); text.append("\nPace Regression Model\n\n"); text.append(m_Model.classAttribute().name()+" =\n\n"); int index = 0; text.append(Utils.doubleToString(m_Coefficients[0], 12, 4) ); for (int i = 1; i < m_Coefficients.length; i++) { // jump over the class attribute if (index == m_ClassIndex) index++; if (m_Coefficients[i] != 0.0) { // output a coefficient if unequal zero text.append(" +\n"); text.append(Utils.doubleToString(m_Coefficients[i], 12, 4) + " * "); text.append(m_Model.attribute(index).name()); } index ++; } return text.toString(); } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(2); newVector.addElement(new Option("\tProduce debugging output.\n" + "\t(default no debugging output)", "D", 0, "-D")); newVector.addElement(new Option("\tThe estimator can be one of the following:\n" + "\t\teb\tEmpirical Bayes(default)\n" + "\t\tnested\tOptimal nested model\n" + "\t\tsubset\tOptimal subset\n" + "\t\tpace2\tPACE2\n" + "\t\tpace4\tPACE4\n" + "\t\tpace6\tPACE6\n\n" + "\t\tols\tOrdinary least squares\n" + "\t\taic\tAIC\n" + "\t\tbic\tBIC\n" + "\t\tric\tRIC\n" + "\t\tolsc\tOLSC", "E", 0, "-E <estimator>")); newVector.addElement(new Option("\tThreshold value for the OLSC estimator", "S", 0, "-S <threshold value>")); return newVector.elements(); } /** * Parses a given list of options. <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 { setDebug(Utils.getFlag('D', options)); String estimator = Utils.getOption('E', options); if ( estimator.equals("ols") ) paceEstimator = olsEstimator; else if ( estimator.equals("olsc") ) paceEstimator = olscEstimator; else if( estimator.equals("eb") || estimator.equals("") ) paceEstimator = ebEstimator; else if ( estimator.equals("nested") ) paceEstimator = nestedEstimator; else if ( estimator.equals("subset") ) paceEstimator = subsetEstimator; else if ( estimator.equals("pace2") ) paceEstimator = pace2Estimator; else if ( estimator.equals("pace4") ) paceEstimator = pace4Estimator; else if ( estimator.equals("pace6") ) paceEstimator = pace6Estimator; else if ( estimator.equals("aic") ) paceEstimator = aicEstimator; else if ( estimator.equals("bic") ) paceEstimator = bicEstimator; else if ( estimator.equals("ric") ) paceEstimator = ricEstimator; else throw new WekaException("unknown estimator " + estimator + " for -E option" ); String string = Utils.getOption('S', options); if( ! string.equals("") ) olscThreshold = Double.parseDouble( string ); } /** * Returns the coefficients for this linear model. */ public double[] coefficients() { double[] coefficients = new double[m_Coefficients.length]; for (int i = 0; i < coefficients.length; i++) { coefficients[i] = m_Coefficients[i]; } return coefficients; } /** * Gets the current settings of the classifier. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] options = new String [6]; int current = 0; if (getDebug()) { options[current++] = "-D"; } options[current++] = "-E"; switch (paceEstimator) { case olsEstimator: options[current++] = "ols"; break; case olscEstimator: options[current++] = "olsc"; options[current++] = "-S"; options[current++] = "" + olscThreshold; break; case ebEstimator: options[current++] = "eb"; break; case nestedEstimator: options[current++] = "nested"; break; case subsetEstimator: options[current++] = "subset"; break; case pace2Estimator: options[current++] = "pace2"; break; case pace4Estimator: options[current++] = "pace4"; break; case pace6Estimator: options[current++] = "pace6"; break; case aicEstimator: options[current++] = "aic"; break; case bicEstimator: options[current++] = "bic"; break; case ricEstimator: options[current++] = "ric"; break; } while (current < options.length) { options[current++] = ""; } return options; } /** * Get the number of coefficients used in the model * * @return the number of coefficients */ public int numParameters() { return m_Coefficients.length-1; } /** * Controls whether debugging output will be printed * * @param debug true if debugging output should be printed */ public void setDebug(boolean debug) { m_Debug = debug; } /** * Controls whether debugging output will be printed * * @param debug true if debugging output should be printed */ public boolean getDebug() { return m_Debug; } /** * Gets the estimator * * @return the estimator */ public SelectedTag getEstimator() { return new SelectedTag(paceEstimator, TAGS_ESTIMATOR); } /** * Sets the estimator. * * @param estimator the new estimator */ public void setEstimator(SelectedTag estimator) { if (estimator.getTags() == TAGS_ESTIMATOR) { paceEstimator = estimator.getSelectedTag().getID(); } } /** * Set threshold for the olsc estimator * * @param threshold the threshold for the olsc estimator */ public void setThreshold(double newThreshold) { olscThreshold = newThreshold; } /** * Gets the threshold for olsc estimator * * @return the threshold */ public double getThreshold() { return olscThreshold; } /** * Calculate the dependent value for a given instance for a * given regression model. * * @param transformedInstance the input instance * @param selectedAttributes an array of flags indicating which * attributes are included in the regression model * @param coefficients an array of coefficients for the regression * model * @return the regression value for the instance. * @exception Exception if the class attribute of the input instance * is not assigned */ private double regressionPrediction(Instance transformedInstance, double [] coefficients) throws Exception { int column = 0; double result = coefficients[column]; for (int j = 0; j < transformedInstance.numAttributes(); j++) { if (m_ClassIndex != j) { column++; result += coefficients[column] * transformedInstance.value(j); } } return result; } /** * Generates a linear regression function predictor. * * @param String the options */ public static void main(String argv[]) { Classifier scheme; try { scheme = new PaceRegression(); System.out.println(Evaluation.evaluateModel(scheme, argv)); } catch (Exception e) { e.printStackTrace(); // System.out.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -