/** * Normalizes the doubles in the array by their sum. * * @param doubles the array of double * @exception IllegalArgumentException if sum is Zero or NaN */ public static void normalize(double[] doubles) { double sum = 0; for (double d : doubles) { sum += d; } normalize(doubles, sum); }
/** * Normalizes the doubles in the array by their sum. * * @param doubles the array of double * @exception IllegalArgumentException if sum is Zero or NaN */ public static void normalize(double[] doubles) { double sum = 0; for (double d : doubles) { sum += d; } normalize(doubles, sum); }
/** * Computes probabilities from F scores * * @param Fs the F scores * @return the computed probabilities */ private double[] probs(double[] Fs) { double maxF = -Double.MAX_VALUE; for (int i = 0; i < Fs.length; i++) { if (Fs[i] > maxF) { maxF = Fs[i]; } } double sum = 0; double[] probs = new double[Fs.length]; for (int i = 0; i < Fs.length; i++) { probs[i] = Math.exp(Fs[i] - maxF); sum += probs[i]; } Utils.normalize(probs, sum); return probs; }
/** * Computes the p-values (probabilities for the classes) from the F-values of * the logistic model. * * @param Fs the F-values * @return the p-values */ protected double[] probs(double[] Fs) { double maxF = -Double.MAX_VALUE; for (double element : Fs) { if (element > maxF) { maxF = element; } } double sum = 0; double[] probs = new double[Fs.length]; for (int i = 0; i < Fs.length; i++) { probs[i] = Math.exp(Fs[i] - maxF); sum += probs[i]; } Utils.normalize(probs, sum); return probs; }
/** * Computes probabilities from F scores * * @param Fs the F scores * @return the computed probabilities */ private double[] probs(double[] Fs) { double maxF = -Double.MAX_VALUE; for (int i = 0; i < Fs.length; i++) { if (Fs[i] > maxF) { maxF = Fs[i]; } } double sum = 0; double[] probs = new double[Fs.length]; for (int i = 0; i < Fs.length; i++) { probs[i] = Math.exp(Fs[i] - maxF); sum += probs[i]; } Utils.normalize(probs, sum); return probs; }
/** * Converts an array containing the natural logarithms of probabilities stored * in a vector back into probabilities. The probabilities are assumed to sum * to one. * * @param a an array holding the natural logarithms of the probabilities * @return the converted array */ public static double[] logs2probs(double[] a) { double max = a[maxIndex(a)]; double sum = 0.0; double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = Math.exp(a[i] - max); sum += result[i]; } normalize(result, sum); return result; }
/** * Converts an array containing the natural logarithms of probabilities stored * in a vector back into probabilities. The probabilities are assumed to sum * to one. * * @param a an array holding the natural logarithms of the probabilities * @return the converted array */ public static double[] logs2probs(double[] a) { double max = a[maxIndex(a)]; double sum = 0.0; double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = Math.exp(a[i] - max); sum += result[i]; } normalize(result, sum); return result; }
@Override public double[] getVotesForInstance(Instance inst) { double[] Pr = new double[inst.numClasses()]; for (int i = 0; i < this.experts.size(); i++) { double[] pr = this.experts.get(i).getVotesForInstance(inst); int yHat = Utils.maxIndex(pr); Pr[yHat] += this.weights.get(i); } // for Utils.normalize(Pr); return Pr; }
/** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if there is a problem generating the prediction */ public double [] distributionForInstance(Instance instance) throws Exception { double[] probOfClassGivenDoc = new double[m_numClasses]; //calculate the array of log(Pr[D|C]) double[] logDocGivenClass = new double[m_numClasses]; for (int h = 0; h < m_numClasses; h++) { logDocGivenClass[h] = probOfDocGivenClass(instance, h); } double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)]; for (int i = 0; i < m_numClasses; i++) { probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max) * m_probOfClass[i]; } Utils.normalize(probOfClassGivenDoc); return probOfClassGivenDoc; }
/** * Calculates the class membership probabilities for the given test * instance. * * @param instance the instance to be classified * @return predicted class probability distribution * @throws Exception if there is a problem generating the prediction */ public double [] distributionForInstance(Instance instance) throws Exception { double[] probOfClassGivenDoc = new double[m_numClasses]; //calculate the array of log(Pr[D|C]) double[] logDocGivenClass = new double[m_numClasses]; for (int h = 0; h < m_numClasses; h++) { logDocGivenClass[h] = probOfDocGivenClass(instance, h); } double max = logDocGivenClass[Utils.maxIndex(logDocGivenClass)]; for (int i = 0; i < m_numClasses; i++) { probOfClassGivenDoc[i] = Math.exp(logDocGivenClass[i] - max) * m_probOfClass[i]; } Utils.normalize(probOfClassGivenDoc); return probOfClassGivenDoc; }
/** * Returns the class distribution for an instance. */ @Override public double[] distributionForInstance(Instance inst) throws Exception { // Does the metric produce thresholds that need to be applied? if (m_thresholds != null) { double[] dist = m_IterativeClassifier.distributionForInstance(inst); double[] newDist = new double[dist.length]; for (int i = 0; i < dist.length; i++) { if (dist[i] >= m_thresholds[i]) { newDist[i] = 1.0; } } Utils.normalize(newDist); // Could have multiple 1.0 entries return newDist; } else { return m_IterativeClassifier.distributionForInstance(inst); } }
/** * Returns the class distribution for an instance. */ @Override public double[] distributionForInstance(Instance inst) throws Exception { // Does the metric produce thresholds that need to be applied? if (m_thresholds != null) { double[] dist = m_IterativeClassifier.distributionForInstance(inst); double[] newDist = new double[dist.length]; for (int i = 0; i < dist.length; i++) { if (dist[i] >= m_thresholds[i]) { newDist[i] = 1.0; } } Utils.normalize(newDist); // Could have multiple 1.0 entries return newDist; } else { return m_IterativeClassifier.distributionForInstance(inst); } }
/** * Return a class probability distribution computed from the frequency counts * at this node * * @param inst the instance to get a prediction for * @param classAtt the class attribute * @return a class probability distribution * @throws Exception if a problem occurs */ public double[] getDistribution(Instance inst, Attribute classAtt) throws Exception { double[] dist = new double[classAtt.numValues()]; for (int i = 0; i < classAtt.numValues(); i++) { WeightMass w = m_classDistribution.get(classAtt.value(i)); if (w != null) { dist[i] = w.m_weight; } else { dist[i] = 1.0; } } Utils.normalize(dist); return dist; }
/** * Return a class probability distribution computed from the frequency counts * at this node * * @param inst the instance to get a prediction for * @param classAtt the class attribute * @return a class probability distribution * @throws Exception if a problem occurs */ public double[] getDistribution(Instance inst, Attribute classAtt) throws Exception { double[] dist = new double[classAtt.numValues()]; for (int i = 0; i < classAtt.numValues(); i++) { WeightMass w = m_classDistribution.get(classAtt.value(i)); if (w != null) { dist[i] = w.m_weight; } else { dist[i] = 1.0; } } Utils.normalize(dist); return dist; }
@Override public double[] convertDistribution(double r[], int c) { double newr[] = new double[c]; for(int i = 0; i < r.length; i++) { double d[] = MLUtils.fromBitString(m_InstancesTemplate.classAttribute().value(i)); for(int j = 0; j < d.length; j++) { newr[j] += (d[j] * r[i]); } } try { Utils.normalize(newr); } catch(Exception e) { newr = new double[c]; } return newr; }
/** * calculate prior probabilites for the clusters * * @param inst the instances * @throws Exception if priors can't be calculated **/ private void estimate_priors(Instances inst) throws Exception { for (int i = 0; i < m_num_clusters; i++) { m_priorsPrev[i] = m_priors[i]; m_priors[i] = 0.0; } for (int i = 0; i < inst.numInstances(); i++) { for (int j = 0; j < m_num_clusters; j++) { m_priors[j] += inst.instance(i).weight() * m_weights[i][j]; } } Utils.normalize(m_priors); }
/** * calculate prior probabilites for the clusters * * @param inst the instances * @throws Exception if priors can't be calculated **/ private void estimate_priors(Instances inst) throws Exception { for (int i = 0; i < m_num_clusters; i++) { m_priorsPrev[i] = m_priors[i]; m_priors[i] = 0.0; } for (int i = 0; i < inst.numInstances(); i++) { for (int j = 0; j < m_num_clusters; j++) { m_priors[j] += inst.instance(i).weight() * m_weights[i][j]; } } Utils.normalize(m_priors); }
@Override public double[] distributionForInstance(Instance instance) throws Exception { if (m_canopies == null || m_canopies.size() == 0) { throw new Exception("No canopies available to cluster with!"); } double[] d = new double[numberOfClusters()]; if (m_missingValuesReplacer != null) { m_missingValuesReplacer.input(instance); instance = m_missingValuesReplacer.output(); } for (int i = 0; i < m_canopies.numInstances(); i++) { double distance = m_distanceFunction.distance(instance, m_canopies.instance(i)); d[i] = 1.0 / (1.0 + distance); } Utils.normalize(d); return d; }
@Override public double[] distributionForInstance(Instance instance) throws Exception { if (m_canopies == null || m_canopies.size() == 0) { throw new Exception("No canopies available to cluster with!"); } double[] d = new double[numberOfClusters()]; if (m_missingValuesReplacer != null) { m_missingValuesReplacer.input(instance); instance = m_missingValuesReplacer.output(); } for (int i = 0; i < m_canopies.numInstances(); i++) { double distance = m_distanceFunction.distance(instance, m_canopies.instance(i)); d[i] = 1.0 / (1.0 + distance); } Utils.normalize(d); return d; }
/** * Returns class probabilities for an instance. * * @param instance the instance to compute the distribution for * @return the class probabilities * @throws Exception if distribution can't be computed successfully */ @Override public double[] distributionForInstance(Instance inst) throws Exception { Attribute classAtt = inst.classAttribute(); double[] pred = new double[classAtt.numValues()]; if (m_root != null) { LeafNode l = m_root.leafForInstance(inst, null, null); HNode actualNode = l.m_theNode; if (actualNode == null) { actualNode = l.m_parentNode; } pred = actualNode.getDistribution(inst, classAtt); } else { // all class values equally likely for (int i = 0; i < classAtt.numValues(); i++) { pred[i] = 1; } Utils.normalize(pred); } // Utils.normalize(pred); return pred; }