/** * Computes class distribution for instance using decision tree. * * @param instance * the instance for which distribution is to be computed * @return the class distribution for the given instance */ public double[] distributionForInstance(Instance instance) throws NoSupportForMissingValuesException { if (m_Attribute == null) { return m_Distribution; } else if (instance.isMissing(m_Attribute)) { double[] d = new double[0]; for (int i = 0; i < m_Successors.length; ++i) { double[] dd = m_Successors[i].distributionForInstance(instance); if (d.length == 0 && dd.length > 0) d = new double[dd.length]; for (int j = 0; j < d.length; ++j) { d[j] += dd[j]; } } for (int j = 0; j < d.length; ++j) { d[j] /= m_Successors.length; } return d; } else { return m_Successors[(int) instance.value(m_Attribute)] .distributionForInstance(instance); } }
public double[] distributionForInstance(Instance instance) throws NoSupportForMissingValuesException{ double sum = 0; double [] distribution = new double[numClasses]; for(int i = 0; i < NUM_TREES; ++i){ double [] treeDist = trees[i].distributionForInstance(instance); for(int j = 0; j <numClasses; ++j){ distribution[j]+= treeDist[j] * weights[i]; } sum += weights[i]; } Assert._assert(sum != 0, "Sum of Weights is zero"); for(int i = 0; i < numClasses; ++i) distribution[i] /= sum; return distribution; }
/** * Classifies the given instance using the linear regression function. * * @param instance the test instance * @return the classification * @throws 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); }
public double classProbability(Instance instance, double targetClass){ Assert._assert(targetClass >= 0); Assert._assert(targetClass < numClasses); try { return distributionForInstance(instance)[(int) targetClass]; } catch (NoSupportForMissingValuesException e) { e.printStackTrace(); } return Double.NaN; }
public double classVariance(Instance instance, double targetClass){ Assert._assert(targetClass >= 0); Assert._assert(targetClass < numClasses); try { return varianceForInstance(instance)[(int) targetClass]; } catch (NoSupportForMissingValuesException e) { e.printStackTrace(); } return Double.NaN; }
public double[] varianceForInstance(Instance instance) throws NoSupportForMissingValuesException{ //should this be weighted? double [][] classProbabilities = new double[NUM_TREES + 1][]; classProbabilities[NUM_TREES] = new double[numClasses]; double [] variance = new double[numClasses]; for(int i = 0; i < NUM_TREES; ++i){ double [] treeDist = trees[i].distributionForInstance(instance); classProbabilities[i] = treeDist; for(int j = 0; j <numClasses; ++j){ classProbabilities[NUM_TREES][j] += treeDist[j]; } } for(int j = 0; j <numClasses; ++j) classProbabilities[NUM_TREES][j] /= NUM_TREES; for(int i = 0; i < NUM_TREES; ++i){ for(int j = 0; j <numClasses; ++j){ double diff = Math.abs(classProbabilities[i][j] - classProbabilities[NUM_TREES][j]); variance[j] += diff * diff; } } for(int j = 0; j <numClasses; ++j) variance[j] /= NUM_TREES; return variance; }
/** * Classifies a given test instance using the decision tree. * * @param instance the instance to be classified * @return the classification * @throws NoSupportForMissingValuesException if instance has missing values */ public double classifyInstance(Instance instance) throws NoSupportForMissingValuesException { if (instance.hasMissingValue()) { throw new NoSupportForMissingValuesException("Id3: no missing values, " + "please."); } if (m_Attribute == null) { return m_ClassValue; } else { return m_Successors[(int) instance.value(m_Attribute)]. classifyInstance(instance); } }
/** * Computes class distribution for instance using decision tree. * * @param instance the instance for which distribution is to be computed * @return the class distribution for the given instance * @throws NoSupportForMissingValuesException if instance has missing values */ public double[] distributionForInstance(Instance instance) throws NoSupportForMissingValuesException { if (instance.hasMissingValue()) { throw new NoSupportForMissingValuesException("Id3: no missing values, " + "please."); } if (m_Attribute == null) { return m_Distribution; } else { return m_Successors[(int) instance.value(m_Attribute)]. distributionForInstance(instance); } }