0

I am wondering if it is possible to do a weighted sample on a population where the sample can not contain certain people but I would still like to consider the excluded people in the population assessment for weights. Is this possible? Below is my current weighted sample code:

def get_weighted_sample(df,n):
    def get_class_prob(x):
        weight_x = int(np.rint(n * len(x[x.Concat != 0]) / len(df[df.Concat != 0])))
        sampled_x = x.sample(weight_x).reset_index(drop=True)
        return (sampled_x)
        # we are grouping by the target class we use for the proportions

    weighted_sample = df.groupby('Product').apply(get_class_prob)
    print(weighted_sample["Product"].value_counts())
    return (weighted_sample)


sample = get_weighted_sample(df,10)
sample

Did some research and not finding any answers so far.

1
  • just to add, my data frame would just be a list of people, and concat is variable that combines unique attributes of the people like age, gender, region. The code above samples so that the each attribute is representative in the population and in the sample in the same way.
    – Ashley
    Dec 13, 2022 at 20:44

1 Answer 1

0

was able to solve using the following for sampled_x: sampled_x=df[df['Exclude']==0].sample(weight_x,replace=False).reset_index(drop=True)

where exclude is a flag in the dataset that indicates which rows to ignore

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.