本文整理汇总了Java中weka.core.Utils.sum方法的典型用法代码示例。如果您正苦于以下问题:Java Utils.sum方法的具体用法?Java Utils.sum怎么用?Java Utils.sum使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Utils
的用法示例。
在下文中一共展示了Utils.sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: chooseRandomIndexBasedOnProportions
import weka.core.Utils; //导入方法依赖的package包/类
/**
* returns a random index based on the given proportions
*
* @param proportionArray the proportions
* @param random the random number generator to use
* @return the random index
*/
protected int chooseRandomIndexBasedOnProportions(double[] proportionArray,
Random random) {
double probSum;
double val;
int index;
double sum;
probSum = Utils.sum(proportionArray);
val = random.nextDouble() * probSum;
index = 0;
sum = 0.0;
while ((sum <= val) && (index < proportionArray.length)) {
sum += proportionArray[index++];
}
return index - 1;
}
示例2: leafNumInstances
import weka.core.Utils; //导入方法依赖的package包/类
protected int leafNumInstances()
{
if (m_Attribute != -1)
throw new Error("not a leaf");
double sum = 0;
if (m_ClassDistribution != null)
sum = Utils.sum(m_ClassDistribution);
return (int) sum;
}
示例3: leafString
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Outputs a leaf.
*
* @return the leaf as string
* @throws Exception if generation fails
*/
protected String leafString() throws Exception {
double sum = 0, maxCount = 0;
int maxIndex = 0;
double classMean = 0;
double avgError = 0;
if (m_ClassDistribution != null) {
if (m_Info.classAttribute().isNominal()) {
sum = Utils.sum(m_ClassDistribution);
maxIndex = Utils.maxIndex(m_ClassDistribution);
maxCount = m_ClassDistribution[maxIndex];
} else {
classMean = m_ClassDistribution[0];
if (m_Distribution[1] > 0) {
avgError = m_Distribution[0] / m_Distribution[1];
}
}
}
if (m_Info.classAttribute().isNumeric()) {
return " : " + Utils.doubleToString(classMean, 2) + " ("
+ Utils.doubleToString(m_Distribution[1], 2) + "/"
+ Utils.doubleToString(avgError, 2) + ")";
}
return " : " + m_Info.classAttribute().value(maxIndex) + " ("
+ Utils.doubleToString(sum, 2) + "/"
+ Utils.doubleToString(sum - maxCount, 2) + ")";
}
示例4: getSquaredError
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Gets the squared error for all clusters.
*
* @return the squared error, NaN if fast distance calculation is used
* @see #m_FastDistanceCalc
*/
public double getSquaredError() {
if (m_FastDistanceCalc) {
return Double.NaN;
} else {
return Utils.sum(m_squaredErrors);
}
}
示例5: clusterInstance
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Classifies a given instance. Either this or distributionForInstance() needs
* to be implemented by subclasses.
*
* @param instance the instance to be assigned to a cluster
* @return the number of the assigned cluster as an integer
* @exception Exception if instance could not be clustered successfully
*/
@Override
public int clusterInstance(Instance instance) throws Exception {
double[] dist = distributionForInstance(instance);
if (dist == null) {
throw new Exception("Null distribution predicted");
}
if (Utils.sum(dist) <= 0) {
throw new Exception("Unable to cluster instance");
}
return Utils.maxIndex(dist);
}
示例6: distributionForInstance
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Classifies a given instance using the selected combination rule.
*
* @param instance the instance to be classified
* @return the distribution
* @throws Exception if instance could not be classified successfully
*/
@Override
public double[] distributionForInstance(Instance instance) throws Exception {
double[] result = new double[instance.numClasses()];
switch (m_CombinationRule) {
case AVERAGE_RULE:
result = distributionForInstanceAverage(instance);
break;
case PRODUCT_RULE:
result = distributionForInstanceProduct(instance);
break;
case MAJORITY_VOTING_RULE:
result = distributionForInstanceMajorityVoting(instance);
break;
case MIN_RULE:
result = distributionForInstanceMin(instance);
break;
case MAX_RULE:
result = distributionForInstanceMax(instance);
break;
case MEDIAN_RULE:
result[0] = classifyInstance(instance);
break;
default:
throw new IllegalStateException("Unknown combination rule '"
+ m_CombinationRule + "'!");
}
if (!instance.classAttribute().isNumeric() && (Utils.sum(result) > 0)) {
Utils.normalize(result);
}
return result;
}
示例7: add
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Adds counts to given bag.
*/
public final void add(int bagIndex, double[] counts) {
double sum = Utils.sum(counts);
for (int i = 0; i < counts.length; i++) {
m_perClassPerBag[bagIndex][i] += counts[i];
}
m_perBag[bagIndex] = m_perBag[bagIndex] + sum;
for (int i = 0; i < counts.length; i++) {
m_perClass[i] = m_perClass[i] + counts[i];
}
totaL = totaL + sum;
}
示例8: KononenkosMDL
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Test using Kononenko's MDL criterion.
*
* @param priorCounts
* @param bestCounts
* @param numInstances
* @param numCutPoints
* @return true if the split is acceptable
*/
private boolean KononenkosMDL(double[] priorCounts, double[][] bestCounts,
double numInstances, int numCutPoints) {
double distPrior, instPrior, distAfter = 0, sum, instAfter = 0;
double before, after;
int numClassesTotal;
// Number of classes occuring in the set
numClassesTotal = 0;
for (double priorCount : priorCounts) {
if (priorCount > 0) {
numClassesTotal++;
}
}
// Encode distribution prior to split
distPrior = SpecialFunctions.log2Binomial(numInstances + numClassesTotal
- 1, numClassesTotal - 1);
// Encode instances prior to split.
instPrior = SpecialFunctions.log2Multinomial(numInstances, priorCounts);
before = instPrior + distPrior;
// Encode distributions and instances after split.
for (double[] bestCount : bestCounts) {
sum = Utils.sum(bestCount);
distAfter += SpecialFunctions.log2Binomial(sum + numClassesTotal - 1,
numClassesTotal - 1);
instAfter += SpecialFunctions.log2Multinomial(sum, bestCount);
}
// Coding cost after split
after = Utils.log2(numCutPoints) + distAfter + instAfter;
// Check if split is to be accepted
return (before > after);
}
示例9: computeAverageClassValues
import weka.core.Utils; //导入方法依赖的package包/类
/** Computes average class values for each attribute and value */
private void computeAverageClassValues() {
double totalCounts, sum;
Instance instance;
double[] counts;
double[][] avgClassValues = new double[getInputFormat().numAttributes()][0];
m_Indices = new int[getInputFormat().numAttributes()][0];
for (int j = 0; j < getInputFormat().numAttributes(); j++) {
Attribute att = getInputFormat().attribute(j);
if (att.isNominal()) {
avgClassValues[j] = new double[att.numValues()];
counts = new double[att.numValues()];
for (int i = 0; i < getInputFormat().numInstances(); i++) {
instance = getInputFormat().instance(i);
if (!instance.classIsMissing() && (!instance.isMissing(j))) {
counts[(int) instance.value(j)] += instance.weight();
avgClassValues[j][(int) instance.value(j)] += instance.weight()
* instance.classValue();
}
}
sum = Utils.sum(avgClassValues[j]);
totalCounts = Utils.sum(counts);
if (Utils.gr(totalCounts, 0)) {
for (int k = 0; k < att.numValues(); k++) {
if (Utils.gr(counts[k], 0)) {
avgClassValues[j][k] /= counts[k];
} else {
avgClassValues[j][k] = sum / totalCounts;
}
}
}
m_Indices[j] = Utils.sort(avgClassValues[j]);
}
}
}
示例10: process
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Processes the given data.
*
* @param instances the data to process
* @return the modified data
* @throws Exception in case the processing goes wrong
*/
@Override
protected Instances process(Instances instances) throws Exception {
// Only change first batch of data
if (isFirstBatchDone()) {
return new Instances(instances);
}
// Generate the output and return it
Instances result = new Instances(instances, instances.numInstances());
double[] sumOfWeightsPerClass = new double[instances.numClasses()];
for (int i = 0; i < instances.numInstances(); i++) {
Instance inst = instances.instance(i);
sumOfWeightsPerClass[(int)inst.classValue()] += inst.weight();
}
double sumOfWeights = Utils.sum(sumOfWeightsPerClass);
// Rescale weights
double factor = sumOfWeights / (double)instances.numClasses();
for (int i = 0; i < instances.numInstances(); i++) {
result.add(instances.instance(i)); // This will make a copy
Instance newInst = result.instance(i);
copyValues(newInst, false, instances, outputFormatPeek());
newInst.setWeight(factor * newInst.weight() /
sumOfWeightsPerClass[(int)newInst.classValue()]);
}
return result;
}
示例11: distributionForInstance
import weka.core.Utils; //导入方法依赖的package包/类
/**
* 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 an error occurred during the prediction
*/
public double [] distributionForInstance(Instance instance) throws Exception {
double transProb = 0.0, temp = 0.0;
double [] classProbability = new double[m_NumClasses];
double [] predictedValue = new double[1];
// initialization ...
for (int i=0; i<classProbability.length; i++) {
classProbability[i] = 0.0;
}
predictedValue[0] = 0.0;
if (m_InitFlag == ON) {
// need to compute them only once and will be used for all instances.
// We are doing this because the evaluation module controls the calls.
if (m_BlendMethod == B_ENTROPY) {
generateRandomClassColomns();
}
m_Cache = new KStarCache[m_NumAttributes];
for (int i=0; i<m_NumAttributes;i++) {
m_Cache[i] = new KStarCache();
}
m_InitFlag = OFF;
// System.out.println("Computing...");
}
// init done.
Instance trainInstance;
Enumeration<Instance> enu = m_Train.enumerateInstances();
while ( enu.hasMoreElements() ) {
trainInstance = (Instance)enu.nextElement();
transProb = instanceTransformationProbability(instance, trainInstance);
switch ( m_ClassType )
{
case Attribute.NOMINAL:
classProbability[(int)trainInstance.classValue()] += transProb;
break;
case Attribute.NUMERIC:
predictedValue[0] += transProb * trainInstance.classValue();
temp += transProb;
break;
}
}
if (m_ClassType == Attribute.NOMINAL) {
double sum = Utils.sum(classProbability);
if (sum <= 0.0)
for (int i=0; i<classProbability.length; i++)
classProbability[i] = (double) 1/ (double) m_NumClasses;
else Utils.normalize(classProbability, sum);
return classProbability;
}
else {
predictedValue[0] = (temp != 0) ? predictedValue[0] / temp : 0.0;
return predictedValue;
}
}
示例12: doPrintClassification
import weka.core.Utils; //导入方法依赖的package包/类
protected void doPrintClassification(double[] dist, Instance inst, int index) throws Exception {
int prec = m_NumDecimals;
Instance withMissing = (Instance)inst.copy();
withMissing.setDataset(inst.dataset());
double predValue = 0;
if (Utils.sum(dist) == 0) {
predValue = Utils.missingValue();
} else {
if (inst.classAttribute().isNominal()) {
predValue = Utils.maxIndex(dist);
} else {
predValue = dist[0];
}
}
// index
append("<tr>");
append("<td>" + (index+1) + "</td>");
if (inst.dataset().classAttribute().isNumeric()) {
// actual
if (inst.classIsMissing())
append("<td align=\"right\">" + "?" + "</td>");
else
append("<td align=\"right\">" + Utils.doubleToString(inst.classValue(), prec) + "</td>");
// predicted
if (Utils.isMissingValue(predValue))
append("<td align=\"right\">" + "?" + "</td>");
else
append("<td align=\"right\">" + Utils.doubleToString(predValue, prec) + "</td>");
// error
if (Utils.isMissingValue(predValue) || inst.classIsMissing())
append("<td align=\"right\">" + "?" + "</td>");
else
append("<td align=\"right\">" + Utils.doubleToString(predValue - inst.classValue(), prec) + "</td>");
} else {
// actual
append("<td>" + ((int) inst.classValue()+1) + ":" + sanitize(inst.toString(inst.classIndex())) + "</td>");
// predicted
if (Utils.isMissingValue(predValue))
append("<td>" + "?" + "</td>");
else
append("<td>" + ((int) predValue+1) + ":" + sanitize(inst.dataset().classAttribute().value((int)predValue)) + "</td>");
// error?
if (!Utils.isMissingValue(predValue) && !inst.classIsMissing() && ((int) predValue+1 != (int) inst.classValue()+1))
append("<td>" + "+" + "</td>");
else
append("<td>" + " " + "</td>");
// prediction/distribution
if (m_OutputDistribution) {
if (Utils.isMissingValue(predValue)) {
append("<td>" + "?" + "</td>");
}
else {
append("<td align=\"right\">");
for (int n = 0; n < dist.length; n++) {
if (n > 0)
append("</td><td align=\"right\">");
if (n == (int) predValue)
append("*");
append(Utils.doubleToString(dist[n], prec));
}
append("</td>");
}
}
else {
if (Utils.isMissingValue(predValue))
append("<td align=\"right\">" + "?" + "</td>");
else
append("<td align=\"right\">" + Utils.doubleToString(dist[(int)predValue], prec) + "</td>");
}
}
// attributes
append(attributeValuesString(withMissing) + "</tr>\n");
}
示例13: backfitHoldOutSet
import weka.core.Utils; //导入方法依赖的package包/类
/**
* Backfits data from holdout set.
*
* @throws Exception if insertion fails
*/
protected void backfitHoldOutSet() throws Exception {
// Insert instance into hold-out class distribution
if (m_Info.classAttribute().isNominal()) {
// Nominal case
if (m_ClassProbs == null) {
m_ClassProbs = new double[m_Info.numClasses()];
}
System.arraycopy(m_Distribution, 0, m_ClassProbs, 0,
m_Info.numClasses());
for (int i = 0; i < m_HoldOutDist.length; i++) {
m_ClassProbs[i] += m_HoldOutDist[i];
}
if (Utils.sum(m_ClassProbs) > 0) {
doSmoothing();
Utils.normalize(m_ClassProbs);
} else {
m_ClassProbs = null;
}
} else {
// Numeric case
double sumOfWeightsTrainAndHoldout = m_Distribution[1]
+ m_HoldOutDist[0];
if (sumOfWeightsTrainAndHoldout <= 0) {
return;
}
if (m_ClassProbs == null) {
m_ClassProbs = new double[1];
} else {
m_ClassProbs[0] *= m_Distribution[1];
}
m_ClassProbs[0] += m_HoldOutDist[1];
m_ClassProbs[0] /= sumOfWeightsTrainAndHoldout;
}
// The process is recursive
if (m_Attribute != -1) {
for (Tree m_Successor : m_Successors) {
m_Successor.backfitHoldOutSet();
}
}
}
示例14: calculateRegionProbs
import weka.core.Utils; //导入方法依赖的package包/类
private double[] calculateRegionProbs(int j, int i) throws Exception {
double[] sumOfProbsForRegion = new double[m_trainingData.classAttribute()
.numValues()];
for (int u = 0; u < m_numOfSamplesPerRegion; u++) {
double[] sumOfProbsForLocation = new double[m_trainingData
.classAttribute().numValues()];
m_weightingAttsValues[m_xAttribute] = getRandomX(j);
m_weightingAttsValues[m_yAttribute] = getRandomY(m_panelHeight - i - 1);
m_dataGenerator.setWeightingValues(m_weightingAttsValues);
double[] weights = m_dataGenerator.getWeights();
double sumOfWeights = Utils.sum(weights);
int[] indices = Utils.sort(weights);
// Prune 1% of weight mass
int[] newIndices = new int[indices.length];
double sumSoFar = 0;
double criticalMass = 0.99 * sumOfWeights;
int index = weights.length - 1;
int counter = 0;
for (int z = weights.length - 1; z >= 0; z--) {
newIndices[index--] = indices[z];
sumSoFar += weights[indices[z]];
counter++;
if (sumSoFar > criticalMass) {
break;
}
}
indices = new int[counter];
System.arraycopy(newIndices, index + 1, indices, 0, counter);
for (int z = 0; z < m_numOfSamplesPerGenerator; z++) {
m_dataGenerator.setWeightingValues(m_weightingAttsValues);
double[][] values = m_dataGenerator.generateInstances(indices);
for (int q = 0; q < values.length; q++) {
if (values[q] != null) {
System.arraycopy(values[q], 0, m_vals, 0, m_vals.length);
m_vals[m_xAttribute] = m_weightingAttsValues[m_xAttribute];
m_vals[m_yAttribute] = m_weightingAttsValues[m_yAttribute];
// classify the instance
m_dist = m_classifier.distributionForInstance(m_predInst);
for (int k = 0; k < sumOfProbsForLocation.length; k++) {
sumOfProbsForLocation[k] += (m_dist[k] * weights[q]);
}
}
}
}
for (int k = 0; k < sumOfProbsForRegion.length; k++) {
sumOfProbsForRegion[k] += (sumOfProbsForLocation[k] * sumOfWeights);
}
}
// average
Utils.normalize(sumOfProbsForRegion);
// cache
double[] tempDist = new double[sumOfProbsForRegion.length];
System.arraycopy(sumOfProbsForRegion, 0, tempDist, 0,
sumOfProbsForRegion.length);
return tempDist;
}
开发者ID:mydzigear,项目名称:repo.kmeanspp.silhouette_score,代码行数:72,代码来源:RemoteBoundaryVisualizerSubTask.java
示例15: calculateRegionProbs
import weka.core.Utils; //导入方法依赖的package包/类
private double[] calculateRegionProbs(int j, int i) throws Exception {
double[] sumOfProbsForRegion = new double[m_trainingData.classAttribute()
.numValues()];
for (int u = 0; u < m_numOfSamplesPerRegion; u++) {
double[] sumOfProbsForLocation = new double[m_trainingData
.classAttribute().numValues()];
m_weightingAttsValues[m_xAttribute] = getRandomX(j);
m_weightingAttsValues[m_yAttribute] = getRandomY(m_panelHeight - i - 1);
m_dataGenerator.setWeightingValues(m_weightingAttsValues);
double[] weights = m_dataGenerator.getWeights();
double sumOfWeights = Utils.sum(weights);
int[] indices = Utils.sort(weights);
// Prune 1% of weight mass
int[] newIndices = new int[indices.length];
double sumSoFar = 0;
double criticalMass = 0.99 * sumOfWeights;
int index = weights.length - 1;
int counter = 0;
for (int z = weights.length - 1; z >= 0; z--) {
newIndices[index--] = indices[z];
sumSoFar += weights[indices[z]];
counter++;
if (sumSoFar > criticalMass) {
break;
}
}
indices = new int[counter];
System.arraycopy(newIndices, index + 1, indices, 0, counter);
for (int z = 0; z < m_numOfSamplesPerGenerator; z++) {
m_dataGenerator.setWeightingValues(m_weightingAttsValues);
double[][] values = m_dataGenerator.generateInstances(indices);
for (int q = 0; q < values.length; q++) {
if (values[q] != null) {
System.arraycopy(values[q], 0, m_vals, 0, m_vals.length);
m_vals[m_xAttribute] = m_weightingAttsValues[m_xAttribute];
m_vals[m_yAttribute] = m_weightingAttsValues[m_yAttribute];
// classify the instance
m_dist = m_classifier.distributionForInstance(m_predInst);
for (int k = 0; k < sumOfProbsForLocation.length; k++) {
sumOfProbsForLocation[k] += (m_dist[k] * weights[q]);
}
}
}
}
for (int k = 0; k < sumOfProbsForRegion.length; k++) {
sumOfProbsForRegion[k] += (sumOfProbsForLocation[k] * sumOfWeights);
}
}
// average
Utils.normalize(sumOfProbsForRegion);
// cache
if ((i < m_panelHeight) && (j < m_panelWidth)) {
m_probabilityCache[i][j] = new double[sumOfProbsForRegion.length];
System.arraycopy(sumOfProbsForRegion, 0, m_probabilityCache[i][j], 0,
sumOfProbsForRegion.length);
}
return sumOfProbsForRegion;
}