Alright, so we have a giant matrix A and another smaller 2x2 matrix B and in
this video we're going to use and np.where to change and manipulate the
matrices now we want. Now, np.where can take in up to three parameters: the
array of booleans, an array of values to keep, and a replacement array. So, I'm
gonna show you how this works with a few different examples. Alright, so let's get
going with our examples. First off we're gonna do the matrix B. We'll type np.
where and we'll give it a mask where B is greater than zero and we'll hit enter
there. And this may not seem like anything interesting at first but let's
take a look at B one more time. Alright, so what this gave back was actually the
indices where the value is indeed true. So B greater than zero... So, any index
where B is greater than zero... You'll notice this four right here is
indexed at zero so row 0 and column 0 and that is indeed greater than zero and
this one at row 1 column 0 is greater than zero so that's why it gave us back
0, 0 and a 1, 0. Alright let's do a similar thing with
A this time. We'll do less than zero so this is obviously a much bigger array so
there's a lot of indices. You can see that the indices line up with where the
negative numbers are. And moving right along... alright we'll do np.where of
B greater than zero. To keep the values of B and replace any values where it's
not true with -1. Alright so B greater than 0. So the only places
where a B is greater than zero is the 4 and 1 and so you'll see that
those stayed the same the 4 and the 1 are the same because
we put B as the keep... -1 we put as the replacement ,so anywhere where it
was less than zero so it didn't meet that condition is now filled in with a
-1. Alright, here's A once again let's do np.
where A is less than zero. We'll keep A and we'll replace it with value 100 so
every time it's less than zero it stayed just like the original A and any time
it's greater than zero (so it didn't match the condition) it was replaced with
the number 100. Now the replacement doesn't have to be a
single number, you can actually give it an array of numbers. Let's go ahead and
move along to that. So we'll do np.where, we'll give it
B greater than zero we'll keep B if that's the case and
we'll replace it with a different and new array. So B greater than zero... we
keep B if that's the case and if not we replace with this array on the end just
full of zeros and I didn't have to put all zeros in that case... So that negative
2 and -1 are now replaced with the zeros in that second array and
to illustrate this one more time we'll do np.where A is greater than zero. A
is going to be what we keep if that's the case in our replacement will be a
random array that's the same size as our original A so if a is greater than zero
then we keep the original A and if it's not greater than zero we replace with a
random number. So, remember all those negatives at the beginning? Now we have a
point seven zero eight a point four zero six and so on but the positive numbers
are still the same. So there you have it, that is how you can use numpy's where
function to manipulate arrays how you desire. Please be sure to check out some
of my other Python videos and don't forget to hit subscribe. :D