How to resample an array in python
I am new in python and I have a question about array/matrix. Below is the matrix I got.
A =
[[85 77 83 ..., 59 58 59]
[80 83 80 ..., 57 60 58]
[75 76 81 ..., 59 58 60]]
I want to re-sample(I don't know if this is the right word) the matrix so it becomes
B =
[[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]
[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]
[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 开发者_开发知识库58 58 58 58 59 59 59 59]
[ 85 85 85 85 77 77 77 77 83 83 83 83 ....... 59 59 59 59 58 58 58 58 59 59 59 59]
[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]
[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]
[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]
[ 80 80 80 80 83 83 83 83 80 80 80 80 ....... 57 57 57 57 60 60 60 60 58 58 58 58]
[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]
[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]
[ 75 75 75 75 76 76 76 76 81 81 81 81 ....... 59 59 59 59 58 58 58 58 60 60 60 60]]
I searched online and looked at many posts, but still I have no clue how to do this. So please teach me how to do this, and I am greatly appreciated.
Definitely use the info from Scipy interpolation how to resize/resample 3x3 matrix to 5x5? from the comments.
But I thought I'd mess around and here's what I got:
Possibly the worst looking method of all time:
>>> import pprint
>>> a = [[85, 77, 99],
... [11, 22, 33],
... [44, 55, 66]]
>>>
>>> def transform(n,matrix):
... return [item for sublist in [[[item for sublist in [[element]*n for element in row] for item in sublist] for _ in range(n)] for row in matrix] for item in sublist]
...
>>> pprint.pprint(transform(3,a))
[[85, 85, 85, 77, 77, 77, 99, 99, 99],
[85, 85, 85, 77, 77, 77, 99, 99, 99],
[85, 85, 85, 77, 77, 77, 99, 99, 99],
[11, 11, 11, 22, 22, 22, 33, 33, 33],
[11, 11, 11, 22, 22, 22, 33, 33, 33],
[11, 11, 11, 22, 22, 22, 33, 33, 33],
[44, 44, 44, 55, 55, 55, 66, 66, 66],
[44, 44, 44, 55, 55, 55, 66, 66, 66],
[44, 44, 44, 55, 55, 55, 66, 66, 66]]
>>> pprint.pprint(transform(4,a))
[[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
[85, 85, 85, 85, 77, 77, 77, 77, 99, 99, 99, 99],
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
[11, 11, 11, 11, 22, 22, 22, 22, 33, 33, 33, 33],
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66],
[44, 44, 44, 44, 55, 55, 55, 55, 66, 66, 66, 66]]
>>> pprint.pprint(transform(5,a))
[[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
[85, 85, 85, 85, 85, 77, 77, 77, 77, 77, 99, 99, 99, 99, 99],
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
[11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 33, 33, 33, 33, 33],
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66],
[44, 44, 44, 44, 44, 55, 55, 55, 55, 55, 66, 66, 66, 66, 66]]
>>>
The following implementation recursively resamples (duplicates) a matrix or a list (any iterable container type) containing numbers (any non-iterable objects). It is fast and much simpler to comprehend than other alternatives. It can handle arbitrarily-nested lists. Every sublist is properly deep-copied.
import itertools
def resample(obj, n):
try:
return list(itertools.chain.from_iterable((resample(row, n) for c in xrange(n)) for row in obj))
except TypeError:
return obj
Usage:
>>> l = [1, 2, 3, 4]
>>> resample(l, 4)
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
>>> m = [[1, 2, 3, 4], [5, 6, 7, 8]]
>>> resample(m, 4)
[[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8],
[5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8]]
I didn't exactly understand your algorithm or what you want to do, but:
a=[[1,2],[3,4]]
# grow horizontally, 5 times
b=[[c for d in zip(x,x,x,x,x) for c in d] for x in a]
# grow vertically, 5 times
c= [z[:] for x in ((y[:],y[:],y[:],y[:],y[:]) for y in b) for z in x]
Note that it works with arrays of anything as it uses only the base language primitives
import numpy as np
import scipy as sp
# your matrix. Let's say A(3,3) with random values from 0 to 20
A = sp.random.randint(20,size=(3,3))
# Resize as you want (m x n)
m =5
n =5
New_A = sp.kron(A, sp.ones(m,n))
print New_A
Even it is a bit late for the answer I preferred to give some comments on it!
精彩评论