相关疑难解决方法(0)

按列索引在Numpy数组的每一行中选择一个元素

有没有更好的方法从"input_array"和"select_id"获取"output_array"?

我们可以摆脱range( input_array.shape[0] )?

>>> input_array = numpy.array( [ [3,14], [12, 5], [75, 50] ] )
>>> select_id = [0, 1, 1]
>>> print input_array
[[ 3 14]
 [12  5]
 [75 50]]

>>> output_array = input_array[  range( input_array.shape[0] ), select_id ]
>>> print output_array
[ 3  5 50]
Run Code Online (Sandbox Code Playgroud)

select numpy

29
推荐指数
2
解决办法
1万
查看次数

如何为numpy数组中的每个类别随机选择1个样本(整数编码)

我使用整数编码来表示numpy数组中的类别。但是,我无法弄清楚如何为每个类别抽取 1 个随机样本并返回索引值。

例如,我有一个数组,如:

np.array([2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 1, 1, 1, 0, 1, 0, 0, 2, 2, 1])
Run Code Online (Sandbox Code Playgroud)

我将如何随机抽取 0、1 和 2 并返回每个样本的索引?

python numpy

1
推荐指数
1
解决办法
489
查看次数

标签 统计

numpy ×2

python ×1

select ×1