Skip to main content
Data Science Wizardry Blog by Attila Vajda

Broadcasting in NumPy, juggling meaning.

I am working on the "Python One Liners" book, because I find it effective in improving my coding skills. After having read about NumPy multidimensional array slicing, the next topic is broadcasting. I am enjoying a coffee and listening to world music, African psychedelic rock. Now it is John Lennon with "Imagine".

It is satisfying to help/collaborate on Discord.

a.shape

shape is a tuple with as many elements as there are dimensions in this array. The values represent the number of elements on each level or dimension.

microthings to do: multidimensional 10 min, search in codebase, ask for quiz, make sentences with conjunctions, one liners, puzzles, unscramble code

I don't understand the following when I read it:

"Broadcasting automatically fixes element-wise operations of NumPy arrays with different shapes. For example, the multiplication operator * usually performs element-wise multiplication when applied to NumPy arrays."

Excerpt from "Python One-Liners" [^1]

I would like to understand it, so I feed it to the computer agent with the following:

Can you please give 2 jumbled one-liners to unscramble,
- a quiz
- 1 related sentence with its word order jumbled
- 3 appositives of related concepts without the actual names of the concepts
- 2 related one-liners in bash, 2 in Python, for data science
- a related mindfulness meditation
- a brief scientific writing template for this research

Here is a jumbled sentence related to broadcasting:

orF hecwh itwh npduroces ,yanmpsibrodc sutop currepocso cilycdiretacm gasev, lsfae dna asir- dpesoh dftuyepsrgio.

🧐 - I find it difficult to untangle these words, let alone untangling the sentence structure.

For, with, produces, Now, I ahve reason to revisit the book and reread the phrase to solve the puzzle! 😌🧠

Puzzling with the cat πŸˆβ€β¬›:

In performs multiplication usually broadcasting element-wise when applied to NumPy the operator * arrays.

In broadcasting, the operator * usually performs element-wise multiplication when applied to NumPy arrays.

a = np.array([1, 2, 3, 4], dtype=np.int16)

NumPy gives you control over the datatype of arrays, because you can set the datatype of the array at its definition. NumPy arrays are homogeneous, so elements set in one type can be easily converted to another type.

β˜•οΈ

employees[0,::2] = employees[0,::2] * 1.1

What are three ways of writing this one-liner? I asked the computer agent, because I don't understand the meaning of this line, or the concept of broadcasting. Seeing different ways of writing a piece of code usually provides some insight into its meaning.

Wow:

employees[0, ::2] *= 1.1

Using list slicing in Python can achieve the same output, but it is a less concise approach:

employees[0][::2] = [x * 1.1 for x in employees[0][::2]]

We can also glimpse what the reason might have been for the transition from the form [0][::2] to [0, ::2]. I can also see now, that in Python consequent square brackets signify dimensions.

DataFrames can be used for acquiring equivalent outcome, this example supplies opportunity for learning Pandas:

import pandas as pd
pd.DataFrame(employees).iloc[0, ::2] *= 1

Panda's iloc is 'Purely integer-location based indexing for selection by position.' I struggle to understand this definition πŸŽ‰. Oh, iloc seems very useful! It is a good addition to the one-liner superpower toolkit. This empowering toolkit already contains, among other tricks, list comprehension, any() and all(), map(), slice assignment, lambda, zip(), slicing and file input.[^1]

Reference: [^1]: Mayer, C. (2020). Python One-Liners. No Starch Press.