为numpy.array的每个元素添加一个维度

时间:2021-10-04 21:22:34

I'm trying to transform each element of a numpy array into an array itself (say, to interpret a greyscale image as a color image). In other words:

我正在尝试将numpy数组的每个元素转换为数组本身(例如,将灰度图像解释为彩色图像)。换一种说法:

>>> my_ar = numpy.array((0,5,10))
[0, 5, 10]
>>> transformed = my_fun(my_ar)  # In reality, my_fun() would do something more useful
array([
      [ 0,  0, 0], 
      [ 5, 10, 15], 
      [10, 20, 30]])
>>> transformed.shape
(3, 3)

I've tried:

def my_fun_e(val):
    return numpy.array((val, val*2, val*3))

my_fun = numpy.frompyfunc(my_fun_e, 1, 3)

but get:

my_fun(my_ar)
(array([[0 0 0], [ 5 10 15], [10 20 30]], dtype=object), array([None, None, None], dtype=object), array([None, None, None], dtype=object))

and I've tried:

我试过了:

my_fun = numpy.frompyfunc(my_fun_e, 1, 1)

but get:

>>> my_fun(my_ar)
array([[0 0 0], [ 5 10 15], [10 20 30]], dtype=object)

This is close, but not quite right -- I get an array of objects, not an array of ints.

这很接近,但不是很正确 - 我得到一个对象数组,而不是一个int数组。

Update 3! OK. I've realized that my example was too simple beforehand -- I don't just want to replicate my data in a third dimension, I'd like to transform it at the same time. Maybe this is clearer?

更新3!好。我已经意识到我的例子事先太简单了 - 我不只是想在第三维复制我的数据,我想同时转换它。也许这更清楚了?

4 个解决方案

#1


2  

Use map to apply your transformation function to each element in my_ar:

使用map将转换函数应用于my_ar中的每个元素:

import numpy

my_ar = numpy.array((0,5,10))
print my_ar

transformed = numpy.array(map(lambda x:numpy.array((x,x*2,x*3)), my_ar))
print transformed

print transformed.shape

#2


7  

Does numpy.dstack do what you want? The first two indexes are the same as the original array, and the new third index is "depth".

numpy.dstack能做你想要的吗?前两个索引与原始数组相同,新的第三个索引是“深度”。

>>> import numpy as N
>>> a = N.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> b = N.dstack((a,a,a))
>>> b
array([[[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]],

       [[4, 4, 4],
        [5, 5, 5],
        [6, 6, 6]],

       [[7, 7, 7],
        [8, 8, 8],
        [9, 9, 9]]])
>>> b[1,1]
array([5, 5, 5])

#3


1  

I propose:

 numpy.resize(my_ar, (3,3)).transpose()

You can of course adapt the shape (my_ar.shape[0],)*2 or whatever

您当然可以调整形状(my_ar.shape [0],)* 2等等

#4


1  

Does this do what you want:

这样做你想要的:

tile(my_ar, (1,1,3))

#1


2  

Use map to apply your transformation function to each element in my_ar:

使用map将转换函数应用于my_ar中的每个元素:

import numpy

my_ar = numpy.array((0,5,10))
print my_ar

transformed = numpy.array(map(lambda x:numpy.array((x,x*2,x*3)), my_ar))
print transformed

print transformed.shape

#2


7  

Does numpy.dstack do what you want? The first two indexes are the same as the original array, and the new third index is "depth".

numpy.dstack能做你想要的吗?前两个索引与原始数组相同,新的第三个索引是“深度”。

>>> import numpy as N
>>> a = N.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> b = N.dstack((a,a,a))
>>> b
array([[[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]],

       [[4, 4, 4],
        [5, 5, 5],
        [6, 6, 6]],

       [[7, 7, 7],
        [8, 8, 8],
        [9, 9, 9]]])
>>> b[1,1]
array([5, 5, 5])

#3


1  

I propose:

 numpy.resize(my_ar, (3,3)).transpose()

You can of course adapt the shape (my_ar.shape[0],)*2 or whatever

您当然可以调整形状(my_ar.shape [0],)* 2等等

#4


1  

Does this do what you want:

这样做你想要的:

tile(my_ar, (1,1,3))