按顺序指定数量为1和0的Numpy数组

The*_*rer 2 python numpy

预期输出:[1,1,1,1,0,0]

我的方法:

out = [1]*len(positive)
out.extend([0]*len(negative))
out = np.array(out)
Run Code Online (Sandbox Code Playgroud)

我觉得应该有一个更好的方法来生成像这样的numpy数组,而不是这个三步过程.

Bra*_*mon 6

你可以使用numpy.repeat():

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

第一个参数是类似数组的元素将被重复.第二个参数指定元素的重复次数.