简体   繁体   中英

How to pass multiple parameters to python function

I have a numpy array with 300 entries. Within each entry is another numpy array [2048 x 2048].

Each entry is a "tiff" entry (in matrix form) which corresponds to the pixel position in a detector. Now, what I want to do is centralize this so that I have an [2048 x 2048] array with each entry having 300 entries corresponding to the pixels from the 300 frames.

I think I have found a way using the zip function. But, each time I get back either a [300 x 2048 x 2048] or [2048 x 300 x 2048].

I want a [2048 x 2048 x 300]. I'm trying to do this in a rather economical and pythonic way beyond simply reloading into a new array and reindexing.

T_prime = zip(([list(t) for t in zip(*Tiffs)])) 

Where Tiffs is the array as described above.

In numpy we often add dimmensions to an array instead of using nested arrays (which is the norm with lists for examples). Once you have all your data in a single array, it's easy to operate on it. In your case it looks like you're looking to transpose the array. An example:

import numpy as np
example_data = np.empty(30, dtype=object)
for i in range(30):
    example_data[i] = np.zeros((100, 101))

structured = np.array(list(example_data))

print structured.shape
# (30, 100, 101)
print structured.transpose([1, 2, 0]).shape
# (100, 101, 30)

you can use this way

result =  map(lambda x: zip(*x) ,zip(*Tiffs))

and here is full example

list1 = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]
list2 =  [[5,6,7,8],[5,6,7,8],[5,6,7,8]]
listoflists = [list1,list2]

result =  map(lambda x: zip(*x) ,zip(*listoflists))
print result

which will result in

[[(1, 5), (2, 6), (3, 7), (4, 8)], [(1, 5), (2, 6), (3, 7), (4, 8)], [(1, 5), (2, 6), (3, 7), (4, 8)]]

Would you describe this as an array of 3 items, where each is a 2x4 array?

In [300]: Tiffs=np.arange(24).reshape(3,2,4)

In [301]: Tiffs
Out[301]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [302]: Tiffs.shape
Out[302]: (3, 2, 4)

If so how about doing a selective transpose?

In [304]: Tiffs.transpose(1,2,0)
Out[304]: 
array([[[ 0,  8, 16],
        [ 1,  9, 17],
        [ 2, 10, 18],
        [ 3, 11, 19]],

       [[ 4, 12, 20],
        [ 5, 13, 21],
        [ 6, 14, 22],
        [ 7, 15, 23]]])

In [305]: _.shape
Out[305]: (2, 4, 3)

It's still a 3d array, but could be viewed a (2x4) with 3 items each.

Another possibility is that it is really an array of objects, where each object is a 2d array, but I think you'd have had to put some extra effort into constructing it.

In [319]: Tiffs
Out[319]: 
array([array([[0, 1, 2, 3],
       [4, 5, 6, 7]]),
       array([[ 8,  9, 10, 11],
       [12, 13, 14, 15]]),
       array([[16, 17, 18, 19],
       [20, 21, 22, 23]])], dtype=object)

Transposing this is trickier because it really is (3,) array of (2,4) arrays, and axis swapping doesn't cross that object boundary. Something with zip is probably required. zip can be used to transpose nested lists, but your zip expression are a bit confusing.

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