Generating combinations and permutations is usually a tricky task in programming.

In Python 🐍, using the itertools library this becomes much easier, less memory intensive and faster!

Let me tell you what I learned with them

[4.45 min]

1/13🧡
Let's suppose you want to create all possible cards of a regular deck. What you have to do is to join:

β€’ All cards ranks: A, 2 to 10, J, Q and K
β€’ The 4 suits: β™₯️♣️♦️♠️

How to generate all cards?

2/13🧡
The operation that can solve this is a cartesian product:

ranks = ['A'] + list(range(2, 11)) + ['J', 'Q', 'K']
suits = ['β™₯️', '♣️', '♦️', '♠️']

all_cards = it.product(suits, ranks)
>>> ('A', 'β™₯️'),('A', '♣️'),('A', '♦️'),('A', '♠️'),...
len(all_cards)
>>> 52

3/13🧡
Before using a deck of cards, usually you shuffle it. One question that we can ask is: How many possible shuffles exist?

This is the same as asking: how many deck permutations exist?

4/13🧡
To generate permutations, itertools have the permutations function:

all_possible_shuffles = it.permutations(all_cards)

Attention! This is an iterable! If you do list(all_possible_shuffles), do you know what would be the length of this list?

5/13🧡
If you try running this on public Colab:

num_permutation = len(list(all_possible_shuffles))

It will crash after using all RAM!!! 🀯

All possible shuffles of a 52 cards deck is equal to 52! (52 factorial)

This number is close to 8 * 10^67
or 8 followed by 67 zeros!

6/13🧡
Here you can clearly see the power of iterables.

You can use a collection that doesn't fit in memory because it's lazily evaluated!

7/13🧡
Now that we've shuffled the deck, you have to draw K cards from it. My question is, how many possible hands can you draw?

The question, in Math language, is: how many combinations of K cards can you draw from a set of 52 cards? πŸ€”

8/13🧡
Itertools also can help you with that.
Let's suppose K = 10:

all_combinations = it.combinations(all_cards, 10)

Again this value is very big!!!

9/13🧡
The combinations formula is:
c = n! / (k! * (n-k)!)
c = 52! / (10! * 42!)
c ~ 1.5 * 10^10

Again, huge number! This will pressure the memory!
As you can see, knowing the available tools to help you can make a big difference!

10/13🧡
The last question is, if you change the previous problem to:
how many possible hands can you draw if when you draw a card, you write it down, put it back on the deck, shuffle and draw again. πŸ€”

This is done using the combination_with_replacement method.

11/13🧡
As you can imagine, this is much bigger than the number of combinations!
Something close to 1.44 * 10Λ†17

you can read more about this here: en.wikipedia.org/wiki/Combinati…

12/13🧡
These functions are the hardest to understand on the itertools lib but it's worth knowing they exist!

I hope I made it easier to understand them

for the full documentation you can find it here: docs.python.org/3/library/iter…

13/13🧡

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Luiz GUStavo πŸ’‰πŸ’‰πŸŽ‰

Luiz GUStavo πŸ’‰πŸ’‰πŸŽ‰ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @gusthema

18 Jun
This week I've been posting about the itertools Python🐍 module.

If you want to improve your coding skills, one way is adding new tools to your toolbox

Itertools enables you to solve problems that would otherwise be absurdly hard to solve.

[2min]

1/7🧡
After you've learned the basic of Python, I'd suggest you go deeper in the collections manipulation:

β€’ Slicing
β€’ Comprehension
β€’ Generators
β€’ Iterators
β€’ Itertools
β€’ map/filter/zip

I've posted about all this content in the past, I can revisit if you'd like

2/7🧡
This week I've explained all functions on the itertools module

Starting by the basic ones:

3/7🧡
Read 7 tweets
15 Jun
Following up from my previous thread, let's continue taking a look at some additional itertools methods

Some of them, as you will see, have very similar built-in versions but the key here is: itertools works on iterables and generators that are lazy evaluated collections

1/14🧡
One example is the method islice. It does slicing but for iterables (potentially endless collections). The main difference is that it doesn't accept negative indexes like regular slicing.

numbers = range(10)
items = it.islice(numbers, 2, 4)
>>> [2, 3]

2/14🧡
zip_longest vs zip
both do the same thing: aggregates elements from multiple iterators.

The difference is that zip_longest aggregates until the longest iterator ends while zip stops on the shortest one.

It will fill the missing values with any value you want

3/14🧡
Read 14 tweets
9 Jun
Quick⚑️ Python🐍 trick:
How do you merge two dictionaries?

[55 sec]🀯

1/6🧡
For Python 3.5 and up you can do:

d1 = {"a":1, "b":2}
d2 = {"c":3, "d":4}
d3 = {**d1, **d2}
d3 == {"a": 1, "b": 2, "c":3, "d":4}

Why?

2/6🧡
The ** operator expands the collection, so, you can think it as this:

d1 = {"a":1, "b":2}
d2 = {"c":3, "d":4}
d3 = {**d1, **d2} -> {"a":1, "b":2, "c":3, "d":4}
d3 == {"a": 1, "b": 2, "c":3, "d":4}

3/6🧡
Read 6 tweets
8 Jun
I was telling a friend that one cool feature from Python is list slice notation

So instead of just posting the link I decided to do a brief explanation.

[5 min]

Python's regular array indexing as in multiple languages is: a[index]

a = [0, 1, 2, 3, 4]
a[0] == 0

1/12🧡
Python has negative indexing too:

a = [0, 1, 2, 3, 4]
a[-1] == 4
a[-2] == 3

2/12🧡
Python also enables creating sub-lists from a list, or a slice:

-> a[start_index:last_index]

a = [0, 1, 2, 3, 4]
a[1:3] == [1, 2]

start_index is inclusive
last_index is exclusive

3/12🧡
Read 12 tweets
30 May
I've been trying the new TensorFlow Decision Forest (TF-DF) library today and it's very good!

Not only the ease of use but also all the available metadata, documentation and integrations you get!

Let me show you some of the cool things I've learned so far...

[5 min]

1/11🧡
TensorFlow Decision Forests have implemented 3 algorithms:

β€’ CART
β€’ Random Forest
β€’ Gradient Boosted Trees

You can get this list with tfdf.keras.get_all_models()

All of them enable Classification, Regression and Ranking Tasks

2/11🧡
CART or Classification and Regression Trees is a simple decision tree. 🌳

The process divides the dataset in two parts
The first is used to grow the tree while the second is used to prune the tree

This is a good basic algorithm to learn and understand Decision Trees

3/11🧡
Read 11 tweets
29 May
Machine learning goes beyond Deep Learning and Neural Networks

Sometimes a simpler technique might give you better results and be easier to understand

A very versatile algorithm is the Decision Forest
🌴🌲🌳?

What is it and how does it work?
Let me tell you..

[7 min]

1/10🧡
Before understanding a Forest, let's start by what's a Tree

Imagine you have a table of data of characteristics of Felines. With features like size, weight, color, habitat and a column with the labels like lion, tiger, house cat, lynx and so on.

2/10🧡
With some time, you could write a code based on if/else statements that could, for each a row in the table, decide which feline it is

This is exactly what a Decision Tree does
During its training it creates the if/elses

en.wikipedia.org/wiki/Decision_…

3/10🧡
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(