Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:24:37+00:00 2026-06-15T13:24:37+00:00

I am reading the weka implementation on re-sampling an array based on a given

  • 0

I am reading the weka implementation on re-sampling an array based on a given weight vector. After reading through the code, I am not sure what’s the algorithm underlying this implementation. In addition, I am quite confusing on the usage of these two lines of code:

  Utils.normalize(probabilities, sumProbs / sumOfWeights);

and

// Make sure that rounding errors don't mess things up
probabilities[numInstances() - 1] = sumOfWeights;

I do not know what they are used for. The following is the code copied from Weka

Instances weka::core::Instances::resampleWithWeights(Random random,double[] weights )       
{

if (weights.length != numInstances()) {
  throw new IllegalArgumentException("weights.length != numInstances.");
}
Instances newData = new Instances(this, numInstances());
if (numInstances() == 0) {
  return newData;
}
double[] probabilities = new double[numInstances()];
double sumProbs = 0, sumOfWeights = Utils.sum(weights);
for (int i = 0; i < numInstances(); i++) {
  sumProbs += random.nextDouble();
  probabilities[i] = sumProbs;
}
Utils.normalize(probabilities, sumProbs / sumOfWeights);

// Make sure that rounding errors don't mess things up
probabilities[numInstances() - 1] = sumOfWeights;
int k = 0; int l = 0;
sumProbs = 0;
while ((k < numInstances() && (l < numInstances()))) {
  if (weights[l] < 0) {
  throw new IllegalArgumentException("Weights have to be positive.");
  }
  sumProbs += weights[l];
  while ((k < numInstances()) &&
       (probabilities[k] <= sumProbs)) { 
  newData.add(instance(l));
  newData.instance(k).setWeight(1);
  k++;
  }
  l++;
}
return newData;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:24:38+00:00Added an answer on June 15, 2026 at 1:24 pm

    The first code fragment:

    Utils.normalize(probabilities, sumProbs / sumOfWeights);
    

    just divides each element of probabilities by the second argument. This converts probabilities from an array that has maximum element of sumProbs to one that has a maximum element of sumOfWeights. The second piece of code:

    probabilities[numInstances() - 1] = sumOfWeights;
    

    just ensures that the last (maximum) element actually is sumOfWeights and wasn’t thrown off by some sort of rounding error.

    EDIT Here’s the theory about how the entire method works. The first half (up to the declaration of k and l) generates probabilities as a vector of (not independent) random numbers that are increasing and the last of which is the sum of weights. This is a random partition of the interval [0, sumOfWeights]. Now the weights themselves are a partition of the same interval. Implicitly, each existing instance is assigned to one each element of the weight-based partition.

    The second half of the method simply steps along the weights partition (using index l). It samples the lth instance as many times as the random partition falls in the indicated weight partition. I realize that this explanation is a little awkwardly worded. Perhaps a picture of what’s going on will help:

    0                                                   sumOfWeights
    ↓                                                       ↓
    
    |     *   *         *       *               * *     *   * ← Random partition
    |    ^      ^           ^      ^     ^     ^         ^  ^ ← Weights partition
    
       0     2        1        1       0     0       3     1  ← # of samples
    

    The second half of the method simply counts how many random partition boundaries (denoted by *) are in each weight interval (bounded by ^). A little consideration should convince you that this is a valid method of randomly sampling with replacement according to the given weights.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through more SICP and I'm stuck on exercise 1.3.8 . My code works
I'm trying to create a new string Attribute using Weka's Java API... Reading through
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading over some example Objective C code just now. @property (nonatomic, strong) IBOutlet UILabel
Reading or writing a C code, I often have difficulties translating the numbers from
Reading through the C specs I found this function: double remquo(double x, double y,
Reading about the problem of creating a read only primitive vector in C# (basically,
Reading through Thinking in Java i stuck in ex:6 of Inner Classes chapter. Exercise
Reading through Peter Norvig's Solving Every Sudoku Puzzle essay , I've encountered a few
Reading for hours, I am pretty sure I understand how blocks in Jade work.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.