Saltar al contenido principal
LibreTexts Español

42.4: Indexación avanzada de Python

  • Page ID
    115504
  • \( \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}}\)

    Este es un poco largo y revisa parte de la información del último video. Sin embargo, me gusta mucho usar imágenes como una forma de hablar sobre la indexación de matrices y matrices en Numpy.

    Enlace directo al video de Youtube.

    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 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);
    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)
    Hacer esto

    Modifica el siguiente código para establecer todos los valores del canal azul en cero usando solo una línea simple de código de indexación.

    no_blue = im.copy()
    
    #put your code here
    
    plt.imshow(no_blue)
    Pregunta

    ¿Cuál fue el comando que usas para establecer a cero todos los valores de blue inside no_blue?


    This page titled 42.4: Indexación avanzada de Python 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.