Algorithms For Dummies



Download 7,18 Mb.
Pdf ko'rish
bet203/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   199   200   201   202   203   204   205   206   ...   651
Bog'liq
Algorithms

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.  




102

 

   


  PART 1 

 Getting Started

(The ability to obtain parts of a dataset is a key function for generating a fully con-

nected 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)

  

(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 combina-

tions; 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:

pool = []

  

for comb in combinations(a, 2):



    pool.append(comb)

  

  



random.sample(pool, 3)

[(1, 4), (3, 4), (1, 2)]

The precise combinations you see as output will vary. However, the idea is that 

you’ve limited your dataset in two ways. First, you’re not using all the data ele-

ments 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.

Another  variation  of  this  theme  is  to  create  a  complete  list  but  randomize  the 

order of the elements. The act of randomizing the list order is shuffling, and you 

use the 


random.shuffle

 function to do it. In fact, 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.



CHAPTER 5


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   199   200   201   202   203   204   205   206   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish