Если вам нужен точный счет для КАЖДОГО целого числа : вот способ сделать это для игрушечного примера с 10 нулями, 5 единицами и 5 двойками и формой (4,5)
. Вы можете изменить числа, их количество и форму по своему усмотрению):
#create integers in the counts you want and stack them into single 1-D array
A = np.hstack((np.zeros(10),np.ones(5),np.ones(5)*2))
#randomly shuffle them
np.random.shuffle(A)
#reshape to your desire
A = A.reshape(4,5)
Пример вывода:
[[2. 0. 1. 1. 2.]
[1. 1. 0. 0. 2.]
[1. 2. 0. 0. 0.]
[0. 0. 0. 0. 2.]]
Если вы хотите точный подсчет ТОЛЬКО ДЛЯ 0s : Согласно комментарию @FBruzzesi, вот способ сделать это для игрушечного примера из 10 нулей и 10 единиц / двоек и формы (4,5)
. Вы можете изменить числа, их количество и форму по своему усмотрению):
#create integers in the counts you want and stack them into single 1-D array
A = np.hstack([np.zeros(10),np.random.randint(1,3,size=10)])
#randomly shuffle them
np.random.shuffle(A)
#reshape to your desire
A = A.reshape(4,5)
Пример вывода:
[[1. 2. 1. 2. 2.]
[0. 0. 0. 0. 1.]
[0. 1. 2. 0. 0.]
[0. 0. 0. 1. 1.]]