2
$\begingroup$

I have a classic User-Item dataset where each row (i.e., (user, item)) indicates the action of a user clicking/selecting an item. Now, the dataset only provides positive samples and does not specifically indicate whether a user has disliked an item. In order to create a balanced dataset, I would like to create random negative samples (for instance randomly pick a set of items which the user has never clicked). Of course, I can achieve this by writing a program; however, I found this library in python Scikits called imbalanced-learn, which seems to provide various sampling techniques. Can someone provide a small code-snippet that can use this library (or some other python library) to achieve this?.

$\endgroup$
1
  • 1
    $\begingroup$ For an ecommerce setting, you might also look at pageviews. A pageview with click is a positive sample, and pageview without a click could be seen as negative. $\endgroup$
    – The Lyrist
    Jun 13, 2018 at 15:40

2 Answers 2

1
$\begingroup$

If I understood correctly, you want to invent new negative samples to have a balanced dataset, because your current dataset only have positive samples.

However, the library you mentioned only re-samples a dataset where all classes are present. The sample creation process is by definition data-specific, as there can be impossible attribute combinations, etc.

If you want to blindly create combination of attribute values, you can use the stuff in module random:

  • To create integer attributes:
    def random_integer(min_val, max_val):
        return randint.randint(0, 9))
    
  • To create real-valued attributes:
    def random_float(min_val, max_val):
        return random.uniform(min_val, max_val)
    
  • To create discrete attributes:
    def random_discrete(value_list):
        return random.choice(value_list)
    

And then, with a loop, generate your negative samples in a loop:

    def generate_negative_sample():
        return {'attr1': random_discrete(['a', 'b', 'c']),
                'attr2': random_float(-10., 10.),
                'attr3': random_integer(0, 4)}

    random_samples = [generate_negative_sample() for _ in range(1000)]
$\endgroup$
0
$\begingroup$

Simple Version

# generate 2d classification dataset

X, y = make_blobs(n_samples=100, centers=3, n_features=2)

Moons Classification Problem (swirl like data)

# generate 2d classification dataset

X, y = make_moons(n_samples=100, noise=0.1)

The make_circles generates a binary classification problem with datasets that fall into concentric circles and few more are also there.

References

Scikit documentation

Imbalanced learn

$\endgroup$
1
  • $\begingroup$ Thanks, but I don't think it addresses my question. I don't want to generate any synthetically balanced/unbalanced dataset. I just want to modify an existing user-item dataset to "make it balanced". $\endgroup$
    – Rkz
    Mar 9, 2018 at 0:39

Your Answer

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

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