[英]Create numpy array with proper step and step range
Hi all I need to create specific array contains random integers from certain range with step. 大家好,我需要创建一个特定的数组,其中包含带有步距的特定范围内的随机整数。
Output need to by like this: 输出需要像这样:
[[0 3 2 1]
[1 2 0 3]
[2 0 3 1]
...
[10 7 8 9]]
First digit in each row need to be a certain value [0,1,2,...]
next 3 digits in the row need to be random from range (0,3)
for first row (without 0), (0,3)
for the second (without 1), (6,10)
for last one (without 10). 每行的第一位数字必须是某个值[0,1,2,...]
该行的下3位数字必须是第一行的范围(0,3)
(无0), (0,3)
第二个(不包含1),最后一个(不包含10)为(6,10)
)。 Generally (x, y) without fixed digit where x = fixed number - 4 but not less than the fixed number, y = fixed number + 4 but not more than the fixed number . 通常(x,y)没有固定数字,其中x =固定数字-4但不小于固定数字,y =固定数字+ 4但不大于固定数字。
Range of random numbers need to be 4 digits without repeats in certain direction. 随机数范围必须是4位,并且不能在特定方向上重复。
Another visualization: fixed numbers = [0,1,2,3,..] random range = (0,3) + offset 另一个可视化:固定数字= [0,1,2,3,..]随机范围=(0,3)+偏移
array range - without
[[0, random numbers in range] (0,3) - 0
[1, random numbers in range] (0,3) - 1
[2, random numbers in range] (0,3) - 2
...
[6, random numbers in range] (2,6) - 6
[7, random numbers in range] (3,7) - 7
...
[n, random numbers in range]] (n-4,n) - n
After some manipulations i wrote below code, which is pretty fast but i think that it can by done much better and faster. 经过一些操作后,我在下面的代码下编写了代码,该代码非常快,但是我认为它可以做得越来越好。
def winning_matrix():
######## create beginning 4 vector array ########
# create vector with counts to merge
add_vector_beginning = np.arange(4)[:, None]
# create zeros array for data input
add_array = np.zeros(shape=(4, 3))
# fill up add array
add_array[0] = np.array([1, 2, 3])
add_array[1] = np.array([0, 2, 3])
add_array[2] = np.array([0, 1, 3])
add_array[3] = np.array([0, 1, 2])
# convert array values to integers
add_array = add_array.astype(int)
# mix array values
mix_array = np.zeros(shape=(4, 3))
for i in range(4):
mix_array[i] = np.random.choice(add_array[i], replace=False, size=3)
# convert array values to integers
mix_array_int = mix_array.astype(int)
# merge array with proper vector
add_matrix = np.column_stack((add_vector_beginning, mix_array_int))
#### create main winning array #######
# create add vector with proper count values
add_vector = np.arange(vectors_number - 4)[:, None] + 4
# create offset for main array
offset = np.arange(vectors_number - 4)[:, None] + 1
# create random array
winning_matrix_random = np.random.rand(vectors_number - 4, 3)
# convert random array values in array into positions intigers
winning_convert = winning_matrix_random.argsort(1)
# add offset to array
winning_matrix_raw = winning_convert + offset
# merge add vector with winning array
winning_matrix = np.column_stack((add_vector, winning_matrix_raw))
#### merge two created arrays into one #########
for i in range(4):
winning_matrix = np.insert(winning_matrix, i, add_matrix[i], axis=0)
return winning_matrix
Any suggestions how to speed up? 有什么建议如何加快?
Looks like the choices for each line are independent, depending on a n
parameter (or two). 看起来每行的选择是独立的,取决于n
参数(或两个)。 If so I'd focus on writing a function that generates one row given n
. 如果是这样,我将专注于编写在给定n
生成一行的函数。
For that row, generate a range, eg x = np.arange(m, m+4)
, remove n
, then do a np.random.choise(x, size=3, replace=False)
. 对于该行,生成一个范围,例如x = np.arange(m, m+4)
,删除n
,然后执行np.random.choise(x, size=3, replace=False)
。 I haven't read your description with enough care to get all the details. 我没有足够仔细地阅读您的描述以获取所有详细信息。
Then just run this function for each row, accumulating the results in a list. 然后,只需为每一行运行此函数,并将结果累积在列表中即可。
def arow(n,m,k=4):
x = list(range(m,m+k))
i = x.pop(x.index(n))
y = np.random.choice(x, replace=False, size=3)
y = [i]+y.tolist()
return y
In [34]: arow(0,0,4)
Out[34]: [0, 1, 2, 3]
In [35]: arow(1,0,4)
Out[35]: [1, 2, 3, 0]
In [39]: arow(10,7,4)
Out[39]: [10, 8, 7, 9]
In [47]: k=4
...: alist = []
...: for n in range(11):
...: m = max(0,n-k+1)
...: r = arow(n,m,k)
...: alist.append(r)
...: np.array(alist)
Out[47]:
array([[ 0, 2, 3, 1],
[ 1, 0, 3, 2],
[ 2, 3, 1, 0],
[ 3, 1, 2, 0],
[ 4, 3, 2, 1],
[ 5, 4, 3, 2],
[ 6, 3, 4, 5],
[ 7, 6, 4, 5],
[ 8, 7, 6, 5],
[ 9, 8, 6, 7],
[10, 8, 9, 7]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.