Transcript:
Broadcasting in the last video, I mentioned that this is another technique that can reduce the execution time of Python code. Let’s see how Python broadcasting works in this video. Let’s look at an example. This matrix is what, carbohydrates, protein and fat have per 100 grams of four different foods. Shows calories. For example, carbohydrates in 100 grams of apples. Give you 56 calories. Much less protein and fat. The protein in 100 grams of beef gives you 104 calories. Fat gives you 135 calories. Our purpose is to provide the carbohydrates proteins and fats in the four foods. Our purpose is to provide the carbohydrates proteins and fats in each of the four foods. Let’’s say we’’re looking for the percentage of calories. Let’’s say we’’re looking for the percentage of calories. For example, if you add up all the numbers in this column, For example, if you add up all the numbers in this column, You can see that. 100 grams of apples have 59 calories. You can see that. 100 grams of apples have 59 calories. You can see that. 100 grams of apples have 59 calories. If you calculate the percentage The percentage of calories that carbohydrates give in apples is. The percentage of calories that carbohydrates give in Apples is 56/59, about 94.9% 56/59, about 94.9% 56/59, about 94.9% Most of the calories in apples come from carbohydrates. Most of the calories in apples come from carbohydrates. Contrary to the beef, most of the calories come from protein and fat. The calculation we want to do here is to get the sum of the numbers in the four columns of the matrix, 100 grams of apple’s beef, eggs and potatoes, 100 grams of apples beef, eggs and potatoes. After that divide, the entire matrix Finds the percentage of calories from carbohydrates, protein and fat in four foods Finds the percentage of calories from carbohydrates, protein and fat in four foods. Can I do this without using the for statement? Can I do this without using the for statement? Let’s see how to do it. What I’m going to show you is this (3 4) matrix that I’ll call A With just one line of Python code? We’re going to get the sum of each column. You’ll get four numbers. The total number of calories in four different foods, 100g of each food. In the second line of Python, we will divide each of the four columns by the sum of each column. If the explanation wasn’t clear, Hope you can understand by looking at the Python code. It’s a Jupyter Notebook. I have already written the code that creates the matrix. A you just saw. If you run it by pressing shift + enter matrix, A is printed. Let’s write two lines of Python code. The first line is Cal = Asum(axis=0). It means I’m going to add heat. I’ll explain more in a moment. Let’s print cal. Adding heat as I just said. The total number of calories in an apple is 59 Adding heat as I just said. The total number of calories in an apple is 59 There are 239 calories in beef as well as eggs and potatoes. To calculate the percentage percentage = 100 * A/calreshape(14). You can write percentage = 100 * A/calreshape(14). You can write percentage = 100 * A/calreshape(14). You can write percentage = 100 * A/calreshape(14). You can write percentage = 100 * A/calreshape(14). You can write Percentage = 100 * A/calreshape(14). It’s a percentage. So you have to multiply it by 100 Let’s print the percentage and run. Let’s print the percentage and run. In the command, we just added matrix. A Divided into a Matrix (1 4) Gives you a percentage matrix. Like I had calculated by hand before Apple’s 94.9% of calories Come from carbohydrates. Back to the slide, Let’s look at the two lines of code. I wrote in the Jupyter Notebook again. If I add a little more explanation. The parameter axis = 0 tells Python to add vertically. The horizontal axis is axis 1. We will add it horizontally. This command is an example of Python broadcasting. This command is an example of Python broadcasting. Dividing the (3 4) matrix. A by the (1 4) matrix Dividing the (3 4) matrix. A by the (1 4) matrix Dividing the (3 4) matrix. A by the (1 4) matrix Dividing the (3 4) matrix. A by the (1 4) matrix. In fact, after the first line of code is executed. The variable Cal is already the (1 4) matrix. You don’t need to use the reshape function. When I write Python code if I am not sure about the dimensions of the matrix. When I write Python code If I am not sure about the dimensions of the matrix, Use the reshape function to make sure you have a matrix of the dimensions you need. Use the reshape function to make sure you have a matrix of the dimensions you need. The reshape function takes a constant time. So the call is very cheap. The reshape function takes a constant time. So the call is very cheap. Don’t hesitate to use the reshape function to make sure the dimensions of the matrix. Don’t hesitate to use the reshape function to make sure the dimensions of the matrix. Let me explain in more detail how this operation works. Here we divided the (3 4) matrix into the (1 4) matrix. Here we divided the (3 4) matrix into the (1 4) matrix. How can we divide a (3 4) matrix into a (1 4) matrix or vector? Let’s look at some more examples of broadcasting (4 1) If we add a constant to the vector Python automatically converts this number into a (4 1) vector Python automatically converts this number into a (4 1) vector Python automatically converts this number into a (4 1) vector. If you add those two vectors, you get the vector on the right. It’’s all the elements plus 100 In the previous video, I already broadcast to Constant B once. This broadcasting works for both row and column vectors. This broadcasting works for both row and column vectors. In logistic regression, we saw a similar shape when we add B to a vector. Let’s look at another example. Let’s say we have a (2 3) matrix. Add the (1 n) matrix to this. In general, there is an (m n) matrix. In general, there is an (m n) matrix. In general, there is an (m n) matrix. Add the (1 n) matrix to that matrix. Add the (1 n) matrix to that matrix. Add the (1 n) matrix to that matrix. Add the (1 N) matrix to that matrix Python copies this matrix M Times and turns it into a (m n) matrix. Here, we will copy the (1 3) matrix twice and make it into the (2 3) matrix Here. We will copy the (1 3) matrix twice and make it into the (2 3) matrix. If we add these two matrices, we get the one on the right. We put 100 in the first column and 200. And 300 in the other two columns. It’’s the same on the previous slide, But It’’s just division, instead of addition, It’s the same on the previous slide, but it’s just division. Instead Of addition, Let’s look at the last example. If we add (m n) matrix and (m 1) vector or matrix. If we add (m n) matrix and (m 1) vector or matrix. If we add (M N) matrix and (m 1) vector or matrix Copy this matrix horizontally N times and make it (m n) matrix. Copy this matrix horizontally N times and make it (m n) matrix. You can think of it as copying it three times horizontally and adding it. The result will be like this. Add 100 to the first row. In the second row we add 200. Let’s take a look at a more general principle of Python broadcasting. If you add subtract, multiply or divide (1 n) matrices from (m n) matrices. If you add subtract, multiply or divide (1 n) matrices from (m n) matrices. If you add subtract, multiply or divide (1 n) matrices from (m n) matrices. Copy this matrix M Times to make it (m n) matrix. Copy this matrix M Times to make it (m n) matrix. It performs element-wise calculation. It performs element-wise calculation. If you operate the (M N) matrix with the (m 1) matrix Copy this matrix N Times to make it (m n) matrix. Copy this matrix N Times to make it (m n) matrix. It performs element-wise calculation. Another form of broadcasting (m 1) when there is a matrix or column vector. Real numbers, addition, subtraction, multiplication or division. Real numbers, addition, subtraction, multiplication or division. It can also be called a (1 1) matrix. In that case, copy this mistake m times. In that case, copy this mistake. M Time’s (m 1) Make a matrix! Add element-by-element. As in this example, Works similarly for row vectors. Works similarly for row vectors. Works similarly for row vectors. More general broadcasting can do more. If you are interested, read the NumPy Broadcasting documentation. If you are interested, read the NumPy Broadcasting documentation. If you are interested, read the NumPy Broadcasting documentation. There is a more general definition of broadcasting. There is a more general definition of broadcasting. This slide wrote down the most important broadcasting to implement a neural network. I’ll tell you briefly before we finish. Among those familiar with Matlab or Octave, If you have used the bsxfun function of neural network programming, Bsxfun is similar, but does different things. It is used for a similar purpose to Python broadcasting. I’m talking to a MATLAB and Octave expert. So it’s okay if you don’t understand. You don’t need to know when implementing neural networks in Python. I’ve been dealing with Python broadcasting. When you do your programming homework broadcasting speeds up your code. I hope you shorten the code. There is one thing I would like to share before doing a programming example. There is one thing I would like to share before doing a programming example. There is one thing I would like to share before doing a programming example. This is advice to reduce errors in Python code. I hope it can help and I’ll cover it in the next video.