简体   繁体   English

如何制作“ ndarrays的ndarray的元组”?

[英]How can I make a “tuple of an ndarray of ndarrays”?

How can I create a tuple which contains two "numpy.ndarray"s? 如何创建包含两个“ numpy.ndarray”的元组? If I do so, can I update the ndarrays inside the tuple? 如果这样做,我可以在元组中更新ndarray吗?

I working on a python code implementing some neural network, in order to make it work with my own input data. 我使用实现某些神经网络的python代码进行工作,以使其与我自己的输入数据一起使用。 The goal is to read an image file and return an output based on what neural network outputs. 目的是读取图像文件并根据神经网络的输出返回输出。 I have taken the code from an excellent online book on Neural Networks written by Michael Nielsen ( link ). 我从迈克尔·尼尔森(Michael Nielsen)撰写的关于神经网络的出色在线书中摘录了代码( 链接 )。

The original code reads from a famous data-set known as MNIST, stored as a file which is output of python's pickle function. 原始代码从称为MNIST的著名数据集中读取,并存储为文件,该文件是python的pickle函数的输出。 Now, the input data from the dataset is read by a data=Pickle.Load() call. 现在,通过data = Pickle.Load()调用读取数据集中的输入数据。 Pickle loads dataset's data When this line is executed, the data variable is loaded as a "Tuple of length 2" containing two "Numpy ndArrays of length 10000",each ndarray element being another ndarray of length 784 (which are in fact values of a 28x28 image). Pickle加载数据集的数据在执行此行时,数据变量将作为“长度为2的元组”加载,其中包含两个“长度为10000的Numpy ndArrays”,每个ndarray元素都是另一个长度为784的ndarray(实际上是a的值28x28图片)。

Now, the problem is I am trying to override the input so that instead of reading from a pickled file, it reads from a normal image file. 现在,问题是我试图覆盖输入,以便从正常图像文件读取而不是从腌制文件中读取。 The pickled file originally contained N image files. 腌制的文件最初包含N个图像文件。 I am trying to crop N image files from my single image file and restructure my data so that it would be compatible with the original format. 我正在尝试从单个图像文件中裁剪N个图像文件并重组数据,以使其与原始格式兼容。

I successfully read my image file, cropped a window of my image, created the innermost ndarray that contains 784 pixel values (a single image). 我成功读取了图像文件,裁剪了图像窗口,创建了最里面的ndarray,其中包含784个像素值(单个图像)。 But how can I create an ndarray that contains each new image that I crop, and after that put them into a tuple? 但是,如何创建一个包含每个新图像的ndarray,然后将它们放入一个元组中? Is there a way to restructure my data in a single numpy command? 有没有办法在单个numpy命令中重组我的数据?

I'm making a lot of guesses as to what you want, but here's sample creating a tuple of arrays: 我对您想要的内容进行了很多猜测,但是以下示例创建了一个数组元组:

An 'image', raveled to the (784,) shape: 惊吓到(784)形状的“图像”:

In [1027]: img=np.ones((28,28),int)
In [1028]: imgr = img.ravel()
In [1029]: imgr.shape
Out[1029]: (784,)

'stack' a bunch of copies of this image. “堆叠”该图像的一堆副本。 np.concatenate is also handy for joining arrays, but np.array automatically joins them on a new axis. np.concatenate对于连接数组也很方便,但是np.array自动将它们连接到新轴上。

In [1030]: imgs=np.array([imgr.copy(),imgr.copy()*2,imgr.copy()*3])
In [1031]: imgs
Out[1031]: 
array([[1, 1, 1, ..., 1, 1, 1],
       [2, 2, 2, ..., 2, 2, 2],
       [3, 3, 3, ..., 3, 3, 3]])
In [1032]: imgs.shape
Out[1032]: (3, 784)

Making a tuple is trivial (basic Python), just (x, y) syntax. 创建元组很简单(基本的Python),只是(x, y)语法。

In [1033]: tup=(imgs, imgs.copy())
In [1034]: tup
Out[1034]: 
(array([[1, 1, 1, ..., 1, 1, 1],
        [2, 2, 2, ..., 2, 2, 2],
        [3, 3, 3, ..., 3, 3, 3]]), array([[1, 1, 1, ..., 1, 1, 1],
        [2, 2, 2, ..., 2, 2, 2],
        [3, 3, 3, ..., 3, 3, 3]]))

Strictly speaking a tuple is immutable; 严格来说,元组是不可变的。 you can't replace elements as you would a list. 您不能像列表那样替换元素。 But since the elements are arrays, they can be changed 'in place'. 但是由于元素是数组,因此可以“就地”更改它们。

In [1035]: tup[0][:]*=2
In [1036]: tup
Out[1036]: 
(array([[2, 2, 2, ..., 2, 2, 2],
        [4, 4, 4, ..., 4, 4, 4],
        [6, 6, 6, ..., 6, 6, 6]]), array([[1, 1, 1, ..., 1, 1, 1],
        [2, 2, 2, ..., 2, 2, 2],
        [3, 3, 3, ..., 3, 3, 3]]))

So here I've scaled the first array by 2, leaving the other as is 所以在这里我将第一个数组按比例缩放了2

to scale one of the images of the 1st array, I could use: 缩放第一个数组的图像之一,我可以使用:

In [1038]: tup[0][1,:] += 5
In [1039]: tup
Out[1039]: 
(array([[2, 2, 2, ..., 2, 2, 2],
        [9, 9, 9, ..., 9, 9, 9],
        [6, 6, 6, ..., 6, 6, 6]]), array([[1, 1, 1, ..., 1, 1, 1],
        [2, 2, 2, ..., 2, 2, 2],
        [3, 3, 3, ..., 3, 3, 3]]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM