Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为numpy数组的每一行采样唯一的列索引_Python_Python 2.7_Numpy_Random - Fatal编程技术网

Python 为numpy数组的每一行采样唯一的列索引

Python 为numpy数组的每一行采样唯一的列索引,python,python-2.7,numpy,random,Python,Python 2.7,Numpy,Random,我想为numpy数组的每一行生成固定数量的随机列索引(不替换) A = np.array([[3, 5, 2, 3, 3], [1, 3, 3, 4, 5], [3, 5, 4, 2, 1], [1, 2, 3, 5, 3]]) 如果我将所需的列号固定为2,我希望 np.array([[1,3], [0,4], [1,4], [2,3]]) 我正在寻找一个基于非循环Numpy的解决方案。我用

我想为numpy数组的每一行生成固定数量的随机列索引(不替换)

A = np.array([[3, 5, 2, 3, 3],
       [1, 3, 3, 4, 5],
       [3, 5, 4, 2, 1],
       [1, 2, 3, 5, 3]])
如果我将所需的列号固定为2,我希望

np.array([[1,3],
          [0,4],
          [1,4],
          [2,3]])
我正在寻找一个基于非循环Numpy的解决方案。我用choice尝试过,但用replacement=False我得到了错误

ValueError:在以下情况下,无法获取比总体更大的样本: “replace=False”


您可以按如下方式使用
random.choice()

def random_indices(arr, n):
    x, y = arr.shape
    return np.random.choice(np.arange(y), (x, n))
    # or return np.random.randint(low=0, high=y, size=(x, n))
演示:

作为一种实验方法,这里有一种方法,在99%的时间内,将给出唯一的指数:

In [60]: def random_ind(arr, n):
    ...:     x, y = arr.shape
    ...:     ind = np.random.randint(low=0, high=y, size=(x * 2, n))
    ...:     _, index = np.unique(ind.dot(np.random.rand(ind.shape[1])), return_index=True)
    ...:     return ind[index][:4]
    ...: 
    ...: 
    ...: 

In [61]: random_ind(A, 2)
Out[61]: 
array([[0, 1],
       [1, 0],
       [1, 1],
       [1, 4]])

In [62]: random_ind(A, 2)
Out[62]: 
array([[1, 0],
       [2, 0],
       [2, 1],
       [3, 1]])

In [64]: random_ind(A, 3)
Out[64]: 
array([[0, 0, 0],
       [1, 1, 2],
       [0, 4, 1],
       [2, 3, 1]])

In [65]: random_ind(A, 4)
Out[65]: 
array([[0, 4, 0, 3],
       [1, 0, 1, 4],
       [0, 4, 1, 2],
       [3, 0, 1, 0]])
如果没有4个唯一项,此函数将在第行返回
索引器
返回ind[index][:4]
,在这种情况下,您可以重复此函数以确保获得所需的结果。

像这样吗

B = np.random.randint(5, size=(len(A), 2))

以下是一种矢量化方法,其灵感来自-

样本运行-

In [146]: A
Out[146]: 
array([[3, 5, 2, 3, 3],
       [1, 3, 3, 4, 5],
       [3, 5, 4, 2, 1],
       [1, 2, 3, 5, 3]])

In [147]: random_unique_indexes_per_row(A, N=2)
Out[147]: 
array([[4, 0],
       [0, 1],
       [3, 2],
       [2, 0]])
In [148]: random_unique_indexes_per_row(A, N=3)
Out[148]: 
array([[2, 0, 1],
       [3, 4, 2],
       [3, 2, 1],
       [4, 3, 0]])

我无法将您想要的结果与原始数组关联。什么代码产生了
选择
错误?很明显,你不能从6个人中选择10件不更换的物品。您是否尝试从第1行中随机选择2个项目,从第2行中随机选择另一个项目,依此类推?@hpaulj如果我选择random.randint(a.shape[1],size=(a.shape[0],2)),则为每行选择2个随机列索引,我将获得具有重复项的行。如果replace=False,我会得到error.OP想要随机索引,但行似乎应该是唯一的。欢迎使用堆栈溢出!请不要只回答源代码。试着提供一个关于你的解决方案如何工作的很好的描述。请参阅:。谢谢,但OP似乎想要
而不需要替换
@Divakar似乎是这样,但是我给出了一个解决方案,用于唯一的行,而不是每行0\u 0中的唯一项。
def random_unique_indexes_per_row(A, N=2):
    m,n = A.shape
    return np.random.rand(m,n).argsort(1)[:,:N]
In [146]: A
Out[146]: 
array([[3, 5, 2, 3, 3],
       [1, 3, 3, 4, 5],
       [3, 5, 4, 2, 1],
       [1, 2, 3, 5, 3]])

In [147]: random_unique_indexes_per_row(A, N=2)
Out[147]: 
array([[4, 0],
       [0, 1],
       [3, 2],
       [2, 0]])
In [148]: random_unique_indexes_per_row(A, N=3)
Out[148]: 
array([[2, 0, 1],
       [3, 4, 2],
       [3, 2, 1],
       [4, 3, 0]])