简体   繁体   English

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

[英]Map 1d numpy array to 2d array based on element-wise function

I have a numpy array, it has shape=(10000,) . 我有一个numpy数组,它有shape=(10000,) Here are the first 5 entries: 以下是前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. 但这似乎不起作用。

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: 使用整数索引,根据index值从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: 这是一个可以使用choose的情况,但它比数字索引的数组形状更挑剔:

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个转换为布尔值的选项, where

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM