Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 分解numpy代码_Python_Python 3.x_Numpy_Scipy - Fatal编程技术网

Python 分解numpy代码

Python 分解numpy代码,python,python-3.x,numpy,scipy,Python,Python 3.x,Numpy,Scipy,我一直在仔细阅读文档,并重新阅读/运行下面的代码,以便准确了解发生了什么。尽管如此,我的知识仍有差距。我想把代码和注释一起呈现给大家,这意味着我的知识缺口,希望你们中的一些人愿意填补 下面是我的请求朋友: 1) 帮助我填补知识空白 2) 以非技术性和简单的形式一步一步地解释这里发生的事情 import numpy import scipy.misc import matplotlib.pyplot lena = scipy.misc.lena() ''' Generates an arti

我一直在仔细阅读文档,并重新阅读/运行下面的代码,以便准确了解发生了什么。尽管如此,我的知识仍有差距。我想把代码和注释一起呈现给大家,这意味着我的知识缺口,希望你们中的一些人愿意填补

下面是我的请求朋友:
1) 帮助我填补知识空白
2) 以非技术性和简单的形式一步一步地解释这里发生的事情

import numpy
import scipy.misc
import matplotlib.pyplot

lena = scipy.misc.lena()


''' Generates an artificial range within the framework of the original array (Which is an image)
This artificial range will be paired with another one and used to 'climb'
Through the original array and make changes'''

def get_indices(size):
    arr = numpy.arange(size)
    #This sets every fourth element to False? How?
    return arr % 4 == 0

lena1 = lena.copy()
xindices = get_indices(lena.shape[0])
yindices = get_indices(lena.shape[1])




'''I am unsure of HOW the below code is executing. I know something is being
Set to zero, but what? And how can I verify it?'''

lena[xindices, yindices] = 0

#What does the argument 211 do exactly?
matplotlib.pyplot.subplot(211)
matplotlib.pyplot.imshow(lena1)


matplotlib.pyplot.show()

谢谢朋友们

在代码执行时,使用Python调试器对逐步检查代码总是很有用的。在您选择的任何地方写下以下内容:

import pdb; pdb.set_trace()
执行将停止,您可以检查任何变量,使用任何定义的函数,并逐行前进

这里是代码的注释版本。函数上的注释被转换为一个docstring,其中包含一个可以执行的doctest

import numpy
import scipy.misc
import matplotlib.pyplot

# Get classic image processing example image, Lena, at 8-bit grayscale
# bit-depth, 512 x 512 size.
lena = scipy.misc.lena()
# lena is now a Numpy array of integers, between 245 and 25, of 512 rows and
# 512 columns.


def get_indices(size):
    """
    Returns each fourth index in a Numpy vector of the passed in size.
    Specifically, return a vector of booleans, where all indices are set to
    False except those of every fourth element. This vector can be used to
    index another Numpy array and select *only* those elements. Example use:

        >>> import numpy as np
        >>> vector = np.array([0, 1, 2, 3, 4])
        >>> get_indices(vector.size)
        array([ True, False, False, False,  True], ...)

    """
    arr = numpy.arange(size)
    return arr % 4 == 0

# Keep a copy of the original image
lena1 = lena.copy()

# Use the defined function to get every fourth index, first in the x direction,
# then in the y direction
xindices = get_indices(lena.shape[0])
yindices = get_indices(lena.shape[1])


# Set every pixel that equals true in the vectors further up to 0. This
# selects **each fourth pixel on the diagonal** (from up left to bottom right).
lena[xindices, yindices] = 0

# Create a Matplotlib plot, with 2 subplots, and selects the one on the 1st
# colum, 1st row. The layout for all subplots is determined from all calls to
# subplot, i.e. if you later call `subplot(212)` you will get a vertical layout
# in one column and two rows; but if you call `subplot(221)` you will get a
# horizontal layout in two columns and one row.
matplotlib.pyplot.subplot(211)
# Show the unaltered image on the first subplot
matplotlib.pyplot.imshow(lena1)
# You could plot the modified original image in the second subplot, and compare
# to the unmodified copy by issuing:
#matplotlib.pyplot.subplot(212)
#matplotlib.pyplot.imshow(lena)

matplotlib.pyplot.show()

您是否试图了解
scipy.misc.lena
返回的内容?你被困在哪里或为什么?是的,我理解。对我来说,最令人困惑的部分是“lena[xindices,yindices]=0”
a[i,j]
只是对
a[i][j]
的一种简单说法,即索引多维数组。其中一些问题在你的另一个问题中得到了回答