From the course: Data Analysis with Python and Pandas

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Broadcasting

Broadcasting

- [Instructor] Okay, so now that we've gotten familiar with the concept of vectorization, let's take a quick look at the concept of broadcasting. Broadcasting is what allows us to perform vectorized operations with arrays of different sizes where NumPy will expand the smaller array to quote unquote fit the larger one. And so the simplest example of this is looking at single values, also known as scalars which can be broadcast into arrays of any dimension. So let's say we have this three by three array. My first column has all ones, my second column has all twos, and my third column has all threes. If I add one to this array, one gets added to every element in this array. How does this code actually work behind the scenes? So we have our three by three array here. We're then going to add one. And what happens on the back end is that we fill out another three by three matrix and perform this matrix operation where we have this three by three matrix with 1, 2, 3's. And then we fill out a…

Contents