小编FJ_*_*asi的帖子

将多维NumPy数组旋转图像90度

我有一个形状为(7,4,100,100)的numpy数组,这意味着我有7个深度为4的100x100图像。我想将这些图像旋转90度。我试过了:

rotated= numpy.rot90(array, 1)
Run Code Online (Sandbox Code Playgroud)

但是它将数组的形状更改为(4,7,100,100),这是不希望的。有什么解决办法吗?

python numpy image

4
推荐指数
2
解决办法
1万
查看次数

在python中缓存函数的最后k个结果

我想编写一个接受单参数函数 f 和整数 k 的函数,并返回一个与 f 行为相同的函数,除了它缓存 f 的最后 k 个结果。

例如,如果 memoize 是我们想要的函数,并且让 mem_f = memoize(f, 2),那么:

    mem_f(arg1) -> f(arg1) is computed and cached  
    mem_f(arg1) -> f(arg1) is returned from cache  
    mem_f(arg2) -> f(arg2) is computed and cached  
    mem_f(arg3) -> f(arg3) is computed and cached, and f(arg1) is evicted
Run Code Online (Sandbox Code Playgroud)

我所做的是:

def memoize(f,k):
    cache = dict()

    def mem_f(*args):
        if args in cache:
            return cache[args]
        result = f(*args)
        cache[args]= result
        return result 
    return mem_f
Run Code Online (Sandbox Code Playgroud)

该函数从缓存中返回结果,如果缓存中没有,则计算并缓存。但是,我不清楚如何仅缓存 f 的最后 k 个结果?我是新手,任何帮助将不胜感激。

python caching function memoization python-3.x

3
推荐指数
1
解决办法
715
查看次数

合并多个numpy数组

我有 3 个 numpy 数组,用于存储形状 (4,100,100) 的图像数据。

arr1= np.load(r'C:\Users\x\Desktop\py\output\a1.npy')
arr2= np.load(r'C:\Users\x\Desktop\py\output\a2.npy')
arr3= np.load(r'C:\Users\x\Desktop\py\output\a3.npy')
Run Code Online (Sandbox Code Playgroud)

我想将所有 3 个数组合并为 1 个数组。我已经尝试过这种方式:

merg_arr = np.zeros((len(arr1)+len(arr2)+len(arr3), 4,100,100), dtype=input_img.dtype)
Run Code Online (Sandbox Code Playgroud)

现在这制作了一个所需长度的数组,但我不知道如何复制这个数组中的所有数据。可能正在使用循环?

python arrays numpy

2
推荐指数
1
解决办法
7429
查看次数

标签 统计

python ×3

numpy ×2

arrays ×1

caching ×1

function ×1

image ×1

memoization ×1

python-3.x ×1