使用我的代码我遇到的问题是numpy.choose方法不接受所有参数,因为它受NPY_MAXARGS(=32)的限制.是否有可用的替代方案,允许任意数量的参数数组或至少多于32那个数组numpy.choose?
choices = [np.arange(0,100)]*100
selection = [0] * 100
np.choose(selection, choices)
>> ValueError: Need between 2 and (32) array objects (inclusive).
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激... :)
索引可以作为列表给出.假设它selections具有与以下相同的长度choices:
b = numpy.array(choices)
result = b[range(len(selections)), selections]
Run Code Online (Sandbox Code Playgroud)
将在选择中给出索引指定的选项中的值.看到它的实际效果:
numpy.random.seed(1)
b = numpy.random.randint(0,100,(5,10))
>>> array([[37, 12, 72, 9, 75, 5, 79, 64, 16, 1],
[76, 71, 6, 25, 50, 20, 18, 84, 11, 28],
[29, 14, 50, 68, 87, 87, 94, 96, 86, 13],
[ 9, 7, 63, 61, 22, 57, 1, 0, 60, 81],
[ 8, 88, 13, 47, 72, 30, 71, 3, 70, 21]])
selections = numpy.random.randint(0,10,5)
>>> array([1, 9, 3, 4, 8])
result = b[range(len(selections)),selections]
>>>> array([12, 28, 68, 22, 70])
Run Code Online (Sandbox Code Playgroud)