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🧡
If the merged dicts have repeated keys, the later will update the earlier values:

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

4/6🧡
This expansion can also be seen on functions

def foo(a, b, c):
return a + b + c

d1 = {"b":1, "c":2}
foo(0, **d1) -> foo(0, 1, 2) -> 3

πŸ’‘This is a very common pattern when you want pass all the named parameters to a function from a dictionary.

5/6🧡
In Python 3.9, there's a new way of doing a dict merge (PEP-584) python.org/dev/peps/pep-0…

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

The | is much easier to read but 3.9 is still not the default version so it can be risky to use

6/6🧡

β€’ β€’ β€’

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

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
28 May
#GoogleIO2021 was last week and it's a lot of content to read/watch!

I'll post summaries of all the ML talks and help you be up to date

Let's start with my talk with @kempy about how #TFHub and how people are using it on the real world



[5 min]

1/7🧡
Having access to high quality models enables developers to solve their problems!

This makes ML more accessible to everyone and helps the society with it, like:

β€’ Defend forests from illegal hunting
β€’ Studying & protecting whales
β€’ Blocking spam
β€’ Helping farmers

2/7🧡
TensorFlow Hub has models for all the ML domains such as Image, Text, Audio and Video

For image, this tutorial can get you started with Transfer Learning. It does some cool tricks with the data and the final model is ready for on-device deployment

tensorflow.org/hub/tutorials/…

3/7🧡
Read 7 tweets
18 May
Today it's my 6 year Google anniversary!

I started as an android developer πŸ“±πŸ€– and today I'm an ML developer πŸ§ πŸ€–

I learned a lot of technical stuff (A LOT!), but there's much more than that.

Let me tell you some of the things I learned...

1/11🧡
Being the big fish 🐳 on a small pound is cool, but you may run out of air.

Starting at this job meant becoming a tiny fish 🐠 in a big pound!

And here is where the growth opportunities are!

2/11🧡
Working with smarter people than you is sometimes very hard for your ego!
There's a lot of impostor syndrome in the beginning.

But that's a big chance to learn what these smarter people do that you can adapt and grow as bigger fish 🦈.

3/11🧡
Read 12 tweets
6 Apr
Sometimes you need to build a Machine Learning model that cannot be expressed with the Sequential API

For these moments, when you need a more complex model, with multiple inputs and outputs or with residual connections, that's when you need the Functional API!

[2.46 min]

1/8🧡
The Functional API is more flexible than the Sequential API.

The easiest way to understand is to visualize the same model created using the Sequential and Functional API

2/8🧡
You can think of the Functional API as a way to create a Directed Acyclic Graph (DAG) of layers while the Sequential API can only create a stack of layers.

Functional is also known as Symbolic or Declarative API

3/8🧡
Read 9 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!

:(