如何在Python中对数组进行重新采样

3

我是一个新手,想请教一个有关python数组/矩阵的问题。 下面是我得到的矩阵。

A =

[[85 77 83 ..., 59 58 59]

[80 83 80 ..., 57 60 58]

[75 76 81 ..., 59 58 60]]

我想重新采样(我不知道这是否是正确的词),使其变成

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

我在网上搜索了很多帖子,但仍然不知道如何做到这一点。所以请教一下如何做到这一点,非常感谢。


我认为如果你向矩阵中的每一行/元素解释你想要做什么,你会发现你已经描述了实现该操作的算法,并可以在Python中自己实现它。 - Assaf Lavie
这会有所帮助:https://dev59.com/M2035IYBdhLWcg3wKsua - carl
4个回答

3

一定要使用来自Stackoverflow评论中的Scipy插值如何将3x3矩阵调整为5x5?的信息。

但我想试试,这是我的结果:

可能是有史以来最丑陋的方法:

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

2
以下实现递归地重新采样(复制)包含数字(任何非可迭代对象)的矩阵或列表(任何可迭代容器类型)。它快速且比其他替代方案更易于理解。它可以处理任意嵌套的列表。每个子列表都被正确地深度复制。
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

使用方法:

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

0

我对你的算法或者你想要做什么并不完全理解,但是:

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]

请注意,它可以使用任何数组,因为它仅使用基本语言原语。

0
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

虽然回答有点晚,但我还是想发表一些评论!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接