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

发布于 2024-11-03 02:16:59 字数 1303 浏览 5 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狂之美人 2024-11-10 02:16:59

绝对使用 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]]
>>> 

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]]
>>> 
拧巴小姐 2024-11-10 02:16:59

以下实现以递归方式对包含数字(任何不可迭代对象)的矩阵或列表(任何可迭代容器类型)进行重新采样(重复)。与其他替代方案相比,它速度更快且更容易理解。它可以处理任意嵌套的列表。每个子列表都被正确地深度复制。

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

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]]
猫九 2024-11-10 02:16:59

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

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]

请注意,它适用于任何数组,因为它仅使用基本语言原语

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

标点 2024-11-10 02:16:59
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

即使现在回答有点晚了,我还是想对此发表一些评论!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文