为每行生成特定数量的1,但仅限x =零

问题描述 投票:1回答:2

假设我有一个像这样的数组

a = np.array([[0, 1, 1, 1, 0, 0, 0, 0, 1, 0],
             [0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
             [1, 0, 0, 0, 0, 1, 0, 0, 1, 0]])

我希望每一行都有一个特定的数量 - 比方说,每行5个。所以在第一行我需要添加1,第二行需要3个,第三行需要2.我需要在x = 0的地方随机生成那些。

我该怎么做呢?

python numpy random
2个回答
1
投票

这有点棘手,但这是一个完全矢量化的解决方案:

import numpy as np

def add_ones_up_to(data, n):
    # Count number of ones to add to each row
    c = np.maximum(n - np.count_nonzero(data, axis=-1), 0)
    # Make row-shuffling indices
    shuffle = np.argsort(np.random.random(data.shape), axis=-1)
    # Row-shuffled data
    data_shuffled = np.take_along_axis(data, shuffle, axis=-1)
    # Sorting indices for shuffled data (indices of zeros will be first)
    sorter = np.argsort(np.abs(data_shuffled), axis=-1)
    # Sorted row-shuffled data
    data_sort = np.take_along_axis(data_shuffled, sorter, axis=-1)
    # Mask for number of ones to add
    m = c[..., np.newaxis] > np.arange(data.shape[-1])
    # Replace values with ones or previous value depending on mask
    data_sort = np.where(m, 1, data_sort)
    # Undo sorting and shuffling
    reorderer = np.empty_like(sorter)
    np.put_along_axis(reorderer, sorter, np.arange(reorderer.shape[-1]), axis=-1)
    np.put_along_axis(reorderer, shuffle, reorderer.copy(), axis=-1)
    return np.take_along_axis(data_sort, reorderer, axis=-1)

np.random.seed(100)
data = np.array([[0, 1, 1, 1, 0, 0, 0, 0, 1, 0],
                 [0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
                 [1, 0, 0, 0, 0, 1, 0, 0, 1, 0]])
n = 5
print(add_ones_up_to(data, n))
# [[0 1 1 1 1 0 0 0 1 0]
#  [0 1 1 1 0 1 0 1 0 0]
#  [1 0 0 0 0 1 1 0 1 1]]

1
投票
import numpy as np

a = np.array([[0, 1, 1, 1, 0, 0, 0, 0, 1, 0],
             [0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
             [1, 0, 0, 0, 0, 1, 0, 0, 1, 0]])

ones = 5

to_add = ones - np.count_nonzero(a, axis=1)

for i in range(a.shape[0]):

    idx = np.random.choice(np.flatnonzero(a[i, :] == 0), size=to_add[i], replace=False)

    a[i, idx] = 1

对于每一行,您计算非零数以计算要添加的数量。您可以从a为零的索引集中选择多个索引并将其设置为1。

© www.soinside.com 2019 - 2024. All rights reserved.