Learn how broadcasting allows use to combine arrays of different shapes and even combine arrays and scalars (simple numbers). You can see how easy it is to scale all values of an array.
[Instructor] We touched broadcasting area, but now let's take a closer look. Let's create a small vector. Array equal NumPy arange of three. And then we can do array plus four. Through adding a scaler to a vector, a NumPy does what makes sense for us. It adds this scaler to every element of the array. We can do the same with other math operations, we can do array divided by seven, and we can do array to the power of two.
Broadcasting works for other dimensions as well. Let's create a matrix of three by three, so matrix equals np.arange of nine, and then reshape with three on three. And then, let's also create a vector of three. And now, we're going to do the matrix plus the vec. NumPy is matching the shapes and extending vector to be the same size of the matrix. Vector itself does not change. We can even do one vector, which is np.arange of three, and then another vector, which is np.arange of three, but we'll reshape it to be three rows and one column.
Let's take a look at vector two, and if you look at the shape, we see that it has three rows and one column. Now add 'em up. V1 plus v2. This time NumPy will extend both vectors to a three by three matrix. V1 will be extended down, like before, and v2 will be extended to a row of zeros, a row of ones, and a row of twos. NumPys try hard to find a good match, but sometimes it's not possible. Let's try, for example np.arange of three plus np.arange of four.
And we'd get an error. This was a deliberate error, and I'll do several more of these throughout the course to illustrate some points. Note that if you're re-running a notebook with arrows in it, you need to click on cell, one or below, until you're done. As we can see, the first example is adding a scaler to a vector, and then the scaler is added to every element. The second example is adding a vector to a matrix, and then the vector is stretched down to fill the gaps, so it can be added to the matrix. And the last example is what we did.
We have two vectors, with different shapes, and each of them is being stretched to match the other dimensions. And the result is the matrix.
- Working with Jupyter notebooks
- Using code cells
- Extensions to the Python language
- Markdown cells
- Editing notebooks
- NumPy basics
- Broadcasting, array operations, and ufuncs
- Folium and Geo
- Machine learning with scikit-learn
- Plotting with matplotlib and bokeh
- Branching into Numba, Cython, deep learning, and NLP
Skill Level Intermediate
NumPy Data Science Essential Trainingwith Charles Kelly3h 54m Intermediate
1. Scientific Python Overview
2. The Jupyter Notebook
3. NumPy Basics
Manage environments5m 11s
6. Folium and Geo
7. NY Taxi Data
10. Other Packages
11. Development Process
Next steps1m 33s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.