[Numpy-discussion] fancy view question
Stéfan van der Walt
stefan@sun.ac...
Tue Feb 17 08:42:21 CST 2009
2009/2/17 Stéfan van der Walt <stefan@sun.ac.za>:
> 2009/2/17 Vincent Schut <schut@sarvision.nl>:
>> Hi list,
>>
>> would it be possible to create a view on an array, such that this view
>> is twice as large (in some dimensions) and in fact does a nearest
>> neighbour 'zoom' on the original array? E.g. using some fancy
>> slicing/striding tricks?
>>
>> an example:
>>
>> a = [[1, 2],
>> [3, 4]]
>>
>> then I'd like a view on a such that this view is:
>>
>> [[1, 1, 2, 2],
>> [1, 1, 2, 2],
>> [3, 3, 4, 4],
>> [3, 3, 4, 4]]
>
> np.lib.stride_tricks.as_strided(x, (2, 2, 2, 2), (8, 0, 4, 0)).reshape((4, 4))
Or, more generally:
import numpy as np
def zoom(x, factor=2):
rows, cols = x.shape
row_stride, col_stride = x.strides
view = np.lib.stride_tricks.as_strided(x,
(rows, factor, cols, factor),
(row_stride, 0, col_stride, 0))
return view.reshape((rows*factor, cols*factor))
a = np.array([[1, 2, 3],
[4, 5, 6]])
print zoom(a, 2)
Cheers
Stéfan
More information about the Numpy-discussion
mailing list