简体   繁体   中英

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

How can I create a tuple which contains two "numpy.ndarray"s? If I do so, can I update the ndarrays inside the tuple?

I working on a python code implementing some neural network, in order to make it work with my own input data. 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 ).

The original code reads from a famous data-set known as MNIST, stored as a file which is output of python's pickle function. Now, the input data from the dataset is read by a data=Pickle.Load() call. 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).

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. 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.

I successfully read my image file, cropped a window of my image, created the innermost ndarray that contains 784 pixel values (a single image). But how can I create an ndarray that contains each new image that I crop, and after that put them into a tuple? Is there a way to restructure my data in a single numpy command?

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:

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.

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.

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

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]]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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