预期输出:[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数组,而不是这个三步过程.
你可以使用numpy.repeat()
:
>>> np.repeat([1, 0], [4, 2])
array([1, 1, 1, 1, 0, 0])
Run Code Online (Sandbox Code Playgroud)
第一个参数是类似数组的元素将被重复.第二个参数指定元素的重复次数.