Читать книгу Algorithms For Dummies - John Paul Mueller, John Mueller Paul, Luca Massaron - Страница 90

Shuffling combinations

Оглавление

In some cases, you don't need an entire dataset; all you really need are a few of the members in combinations of a specific length. For example, you might have a dataset containing four numbers and want only two-number combinations from it. (The ability to obtain parts of a dataset is a key function for generating a fully connected graph, which is described in Part 3 of the book.) The following code shows how to obtain such combinations:

from itertools import combinations a = np.array([1,2,3,4]) for comb in combinations(a, 2): print(comb)

which produces this output:

(1, 2)(1, 3)(1, 4)(2, 3)(2, 4)(3, 4)

The output contains all the possible two-number combinations of a. Note that this example uses the itertools combinations() function (the permutations() function appears in the previous section). Of course, you might not need all those combinations; perhaps a random subset of them would work better. In this case, you can rely on the random.sample() function to come to your aid, as shown here:

import random pool = [] for comb in combinations(a, 2): pool.append(comb) print(random.sample(pool, 3))

The precise combinations you see as output will vary, such as [(1, 2), (2, 3), (1, 4)]. However, the idea is that you've limited your dataset in two ways. First, you’re not using all the data elements all the time, and second, you’re not using all the possible combinations of those data elements. The effect is to create a relatively random-looking set of data elements that you can use as input to an algorithm. Python provides a whole host of randomizing methods that you can see at https://docs.python.org/3/library/random.html. Many of the later examples in this book also rely on randomization to help obtain the correct output from algorithms.

Algorithms For Dummies

Подняться наверх