我创建了一个函数“cross2”来完成一个矩阵,从前面的两列中随机抽取两个数字来完成后面的两列。但当一个数字被采样时,另一个被采样的数字在另一行
#I create the matrix "tab2"
data2=np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]])
data22=data2.transpose()
tab2=np.zeros((6,8),float)
tab2[:,0:2]=data22
def cross2 (a):#a is the column of the data
for i in range (0,6):#i is the row of the data
number=np.arange(0,6,1)
c=np.random.choice(number,1,replace=True)
tab2[i,a]=tab2[c,a-2]# select a number by a random choice from second previous column
toChooseFrom1 = np.concatenate((tab2[:c,(a-2)], tab2[(c+1):,(a-2)]))
toChooseFrom2 = np.concatenate((tab2[:c,(a-1)], tab2[(c+1):,(a-1)]))
tochoose=np.concatenate([toChooseFrom1,toChooseFrom2])
np.random.shuffle(tochoose)
tab2[i,a+1]=np.random.choice(tochoose,1,replace=True)
#loop to complete the matrix tab2 with the function "cross2"
for a in range (2,8):
if a % 2 ==0:
cross2(a)
我认为这个函数很好,但是如果可能的话,我想用更少的行做同样的事情,因为我的脚本很长
目前没有回答
相关问题 更多 >
编程相关推荐