基于逐元素函数将1d numpy数组映射到2d数组

时间:2021-10-23 21:24:14

I have a numpy array, it has shape=(10000,). Here are the first 5 entries:

我有一个numpy数组,它有shape =(10000,)。以下是前5个条目:

labels = data[:, 0]
print(labels.shape)
print(labels[0:5])

# prints 
# (100000,)
# [1. 1. 1. 0. 1.]

Every entry is either 0 or 1. I would like to map this to a 2d array, by an element wise operation which maps

每个条目都是0或1.我想通过映射的元素操作将其映射到2d数组

0 -> [1, 0]
1 -> [0, 1]

How do I do this? I tried

我该怎么做呢?我试过了

labels = np.apply_along_axis(lambda x: [1, 0] if x[0] == 0 else [0, 1], 0, data[:, 0])

but that did not seem to work.

但这似乎不起作用。

3 个解决方案

#1


2  

In [435]: ref = np.array([[1,0],[0,1]])
In [436]: index = np.array([1.,1.,1.,0.,1.])

Indexing with floats gives an error in recent versions:

使用浮点数进行索引会在最近的版本中出错:

In [437]: ref[index,:]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-437-d50c95668d6c> in <module>()
----> 1 ref[index,:]

IndexError: arrays used as indices must be of integer (or boolean) type

Index with integers, select rows from ref depending on the index value:

使用整数索引,根据索引值从ref中选择行:

In [438]: ref[index.astype(int),:]
Out[438]: 
array([[0, 1],
       [0, 1],
       [0, 1],
       [1, 0],
       [0, 1]])

This is a case where choose could be used, but it's pickier about array shapes than the above indexing:

这是一个可以使用选择的情况,但它比上面的索引更挑剔数组形状:

In [440]: np.choose(index.astype(int)[:,None],[[1,0],[0,1]])
Out[440]: 
array([[0, 1],
       [0, 1],
       [0, 1],
       [1, 0],
       [0, 1]])

or with only 2 choices that convert to boolean, where:

或只有2个转换为布尔值的选项,其中:

In [443]: np.where(index.astype(bool)[:,None],[0,1],[1,0])
Out[443]: 
array([[0, 1],
       [0, 1],
       [0, 1],
       [1, 0],
       [0, 1]])

#2


1  

You could try the following

您可以尝试以下方法

labels = np.array([1,1,1,0,1])
np.eye(np.max(labels) + 1)[labels]

which gives:

array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 1.,  0.],
       [ 0.,  1.]])

#3


0  

This method does xor on the original array and stack two arrays together.

此方法对原始数组执行xor并将两个数组堆叠在一起。

labels = np.random.randint(0,2, 10000)
# array([0, 0, 1, ..., 1, 1, 0])
np.vstack([(~labels.astype(bool)).astype(int), labels])
array([[1, 1, 0, ..., 0, 0, 1],
       [0, 0, 1, ..., 1, 1, 0]])

#1


2  

In [435]: ref = np.array([[1,0],[0,1]])
In [436]: index = np.array([1.,1.,1.,0.,1.])

Indexing with floats gives an error in recent versions:

使用浮点数进行索引会在最近的版本中出错:

In [437]: ref[index,:]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-437-d50c95668d6c> in <module>()
----> 1 ref[index,:]

IndexError: arrays used as indices must be of integer (or boolean) type

Index with integers, select rows from ref depending on the index value:

使用整数索引,根据索引值从ref中选择行:

In [438]: ref[index.astype(int),:]
Out[438]: 
array([[0, 1],
       [0, 1],
       [0, 1],
       [1, 0],
       [0, 1]])

This is a case where choose could be used, but it's pickier about array shapes than the above indexing:

这是一个可以使用选择的情况,但它比上面的索引更挑剔数组形状:

In [440]: np.choose(index.astype(int)[:,None],[[1,0],[0,1]])
Out[440]: 
array([[0, 1],
       [0, 1],
       [0, 1],
       [1, 0],
       [0, 1]])

or with only 2 choices that convert to boolean, where:

或只有2个转换为布尔值的选项,其中:

In [443]: np.where(index.astype(bool)[:,None],[0,1],[1,0])
Out[443]: 
array([[0, 1],
       [0, 1],
       [0, 1],
       [1, 0],
       [0, 1]])

#2


1  

You could try the following

您可以尝试以下方法

labels = np.array([1,1,1,0,1])
np.eye(np.max(labels) + 1)[labels]

which gives:

array([[ 0.,  1.],
       [ 0.,  1.],
       [ 0.,  1.],
       [ 1.,  0.],
       [ 0.,  1.]])

#3


0  

This method does xor on the original array and stack two arrays together.

此方法对原始数组执行xor并将两个数组堆叠在一起。

labels = np.random.randint(0,2, 10000)
# array([0, 0, 1, ..., 1, 1, 0])
np.vstack([(~labels.astype(bool)).astype(int), labels])
array([[1, 1, 0, ..., 0, 0, 1],
       [0, 0, 1, ..., 1, 1, 0]])