Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

42.4: Advanced Python Indexing

( \newcommand{\kernel}{\mathrm{null}\,}\)

This one is a little long and reviews some of the information from the last video. However, I really like using images as a way to talk about array and matrix indexing in Numpy.

Direct Link to the Youtube video.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

from IPython.display import YouTubeVideo
YouTubeVideo("XSyiafkKerQ",width=640,height=360, cc_load_policy=True)
from IPython.display import YouTubeVideo
YouTubeVideo("XSyiafkKerQ",width=640,height=360, cc_load_policy=True)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import imageio

#from urllib.request import urlopen, urlretrieve
from imageio import imsave

url = 'https://res.cloudinary.com/miles-extranet-dev/image/upload/ar_16:9,c_fill,w_1000,g_face,q_50/Michigan/migration_photos/G21696/G21696-msubeaumonttower01.jpg'
im = imageio.imread(url)

im[10,10,0] = 255
im[10,10,1] = 255
im[10,10,2] = 255

#Show the image
plt.imshow(im);
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import imageio

#from urllib.request import urlopen, urlretrieve
from imageio import imsave

url = 'https://res.cloudinary.com/miles-extranet-dev/image/upload/ar_16:9,c_fill,w_1000,g_face,q_50/Michigan/migration_photos/G21696/G21696-msubeaumonttower01.jpg'
im = imageio.imread(url)

im[10,10,0] = 255
im[10,10,1] = 255
im[10,10,2] = 255

#Show the image
plt.imshow(im);

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

im[20,20,:] = 255
plt.imshow(im)
im[20,20,:] = 255
plt.imshow(im)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

cropped = im[0:50,0:50,:]
plt.imshow(cropped)
cropped = im[0:50,0:50,:]
plt.imshow(cropped)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

cropped = im[50:,350:610,:]
plt.imshow(cropped)
cropped = im[50:,350:610,:]
plt.imshow(cropped)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

red = im[:,:,0]
plt.imshow(red)
plt.colorbar()
red = im[:,:,0]
plt.imshow(red)
plt.colorbar()

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Note python changed slightly since the making of the video.  
# We added the astype funciton to ensure that values are between 0-255
red_only = np.zeros(im.shape).astype(int)
red_only[:,:,0] = red

plt.imshow(red_only)
#Note python changed slightly since the making of the video.  
# We added the astype funciton to ensure that values are between 0-255
red_only = np.zeros(im.shape).astype(int)
red_only[:,:,0] = red

plt.imshow(red_only)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

green_only = np.zeros(im.shape).astype(int)
green_only[:,:,1] = im[:,:,1]

plt.imshow(green_only)
green_only = np.zeros(im.shape).astype(int)
green_only[:,:,1] = im[:,:,1]

plt.imshow(green_only)

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

blue_only = np.zeros(im.shape).astype(int)
blue_only[:,:,2] = im[:,:,2]

plt.imshow(blue_only)
blue_only = np.zeros(im.shape).astype(int)
blue_only[:,:,2] = im[:,:,2]

plt.imshow(blue_only)
Do This

Modify the following code to set all of the values in the blue channel to zero using only one simple line of indexing code.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

no_blue = im.copy()

#put your code here

plt.imshow(no_blue)
no_blue = im.copy()

#put your code here

plt.imshow(no_blue)
Question

What was the command you use to set all of the values of blue inside no_blue to zero?


This page titled 42.4: Advanced Python Indexing is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?