Skip to main content
Mathematics LibreTexts

44.4: Advanced Python Indexing

  • Page ID
    70569
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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.

    from IPython.display import YouTubeVideo
    YouTubeVideo("XSyiafkKerQ",width=640,height=360, cc_load_policy=True)
    %matplotlib inline
    import matplotlib.pylab as plt
    import numpy as np
    import imageio
    
    #from urllib.request import urlopen, urlretrieve
    #from scipy.misc 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);
    im[20,20,:] = 255
    plt.imshow(im)
    cropped = im[0:50,0:50,:]
    plt.imshow(cropped)
    cropped = im[50:,350:610,:]
    plt.imshow(cropped)
    red = im[:,:,0]
    plt.imshow(red)
    plt.colorbar()
    #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)
    green_only = np.zeros(im.shape).astype(int)
    green_only[:,:,1] = im[:,:,1]
    
    plt.imshow(green_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.

    no_blue = im.copy()
    #####Start your code here #####
    
    #####End of 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 44.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; a detailed edit history is available upon request.

    • Was this article helpful?