有一些numpy.map吗? [英] Is there some numpy.map?
问题描述
我可能在这里缺少明显的东西,但是我缺少了函数numpy.map
.那将与Python的map
函数相同,但是会将输出收集在numpy
数组中.例如,我可能有一个图像生成器genImage(i)
,该图像生成器基于单个输入生成2D图像(大小为(m, n)
),并且我想将range(k)
输入到生成器函数中并获得一个(k, m, n)
数组
I may be missing something obvious here, but I am missing a function numpy.map
. What that would be is the same as Python's map
function, but collect the output in a numpy
array. For example, I could have an image generator genImage(i)
that generates a 2D image (of size (m, n)
) based on a single input, and I would like to input range(k)
to my generator function and obtain a (k, m, n)
array.
当前,我会使用numpy.array(list(map(genImage, range(k)))
,但是我觉得这种转换成列表的效率很低(我的最终数组大小约为50 GB).因此,我正在寻找与numpy.fromiter
类似的numpy.map(genImage, range(k))
,但要查找迭代器的多维输出.
Currently, I would use numpy.array(list(map(genImage, range(k)))
, but I feel that this conversion into a list is rather inefficient (my final array is about 50 GB in size). I am thus looking for numpy.map(genImage, range(k))
, which is similar to numpy.fromiter
, but for multidimensional outputs of the iterator.
(我尝试过np.array(map(...))
,但是它返回带有地图的一个元素数组,因为它是唯一的条目-这就是为什么:为什么需要将地图类型转换为列表以将其分配给熊猫系列?)
(I have tried np.array(map(...))
, but that returns a one-element array with the map as it's only entry - here is why: Why is it required to typecast a map into a list to assign it to a pandas series?)
是否有更好的方法来实现我想要的?我正在寻找一种理想的方式,可以与joblib
一起使用.
Is there a better way to achieve what I want? I am looking to a way that ideally, I could use with joblib
.
推荐答案
If I got you right you need column_stack that would work like this:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
a = np.column_stack((range(3), a))
a
[[0 1 2]
[1 3 4]
[2 5 6]]
这篇关于有一些numpy.map吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!