Python 移除数组的随机子集
我有一个数组Python 移除数组的随机子集,python,numpy,multidimensional-array,random,Python,Numpy,Multidimensional Array,Random,我有一个数组a=array([[x1,x2,…,xn],[y1,y2,…,yn]])soa.shape=(2,n)。有没有一种快速的方法可以获取此数组的随机子集,即r=array([[xi1,xi2,…,xip],[yi1,yi2,…,yip]])形式的数组?您可以使用随机。选择来屏蔽列并索引: idx = np.random.choice([True,False], replace=True, size=a.shape[1], p=[0.7,0.3
a=array([[x1,x2,…,xn],[y1,y2,…,yn]])
soa.shape=(2,n)
。有没有一种快速的方法可以获取此数组的随机子集,即r=array([[xi1,xi2,…,xip],[yi1,yi2,…,yip]])形式的数组
?您可以使用随机。选择来屏蔽列并索引:
idx = np.random.choice([True,False], replace=True, size=a.shape[1],
p=[0.7,0.3] # keep approx. 70% of columns
)
a[:, idx]
您可以使用random.choice
屏蔽列并索引:
idx = np.random.choice([True,False], replace=True, size=a.shape[1],
p=[0.7,0.3] # keep approx. 70% of columns
)
a[:, idx]
首先获取一些随机索引:
i=np.random.randint(2, size=a.shape[1])
然后只保留以下尺寸:
b=a[:,np.where(i==1)]
例如:
>>> a
array([[1. , 2. , 3. , 4. , 5. , 6. , 7. ],
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]])
>>> i=np.random.randint(2, size=a.shape[1])
>>> i
array([0, 0, 1, 1, 1, 1, 0])
>>> a[:,np.where(i==1)]
array([[[3. , 4. , 5. , 6. ]],
[[0.3, 0.4, 0.5, 0.6]]])
首先获取一些随机索引:
i=np.random.randint(2, size=a.shape[1])
然后只保留以下尺寸:
b=a[:,np.where(i==1)]
例如:
>>> a
array([[1. , 2. , 3. , 4. , 5. , 6. , 7. ],
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]])
>>> i=np.random.randint(2, size=a.shape[1])
>>> i
array([0, 0, 1, 1, 1, 1, 0])
>>> a[:,np.where(i==1)]
array([[[3. , 4. , 5. , 6. ]],
[[0.3, 0.4, 0.5, 0.6]]])
你能不能也包括一些你试过的代码?我试过例如a0=np.random.choice(a[0],size=p,replace=False)a1=np.array([a[1][a0.tolist().index(value)]表示a0中的值)a=np.vstack((a0,a1))
你能不能也包括一些你试过的代码?我试过例如a0=np random.choice(a[0],size=p,replace=False)a1=np.array([a[1][a0.tolist().index(value)]表示a0中的值)a=np.vstack((a0,a1))