In [1]:
import numpy as np
np.random.seed(0)
p = np.array([0.1, 0.0, 0.7, 0.2])
index = np.random.choice([0, 1, 2, 3], p = p.ravel())
This means that you will pick the index
according to the distribution:
$P(index = 0) = 0.1, P(index = 1) = 0.0, P(index = 2) = 0.7, P(index = 3) = 0.2$.
In [2]:
np.random.seed(0)
p = np.array([0.1, 0.0, 0.6, 0.3])
print(p)
print(p.ravel())
In [3]:
v =[0,0,0,0]
ntest = 1000
for i in range(ntest):
idx = np.random.choice([0, 1, 2, 3], p = p.ravel())
v[idx] += 1
#print(i, idx)
In [4]:
print(v)
In [5]:
v = np.array(v)
print(v / float(ntest))
In [6]:
m1 = np.random.permutation(5)
print(m1)
In [7]:
X = np.array([1,2,3,4,5])
m = X.shape[0]
permutation = list(np.random.permutation(m))
shuffled_X = X[permutation]
In [8]:
print(X)
print(shuffled_X)
print(permutation)
In [9]:
x1 = [2, 4, 8, 10, 20]
x2 = [0.2, 0.4, -0.8, 1.0, -2.0]
X = np.transpose(np.vstack((x1, x2)))
print(X)
print(X.shape)
In [10]:
permutation = np.random.permutation(X.shape[0])
In [11]:
X_shuffle= X[permutation]
print(X_shuffle)
In [12]:
x1 = [2, 4, 8, 10, 20]
x2 = [0.2, 0.4, -0.8, 1.0, -2.0]
X = np.transpose(np.vstack((x1, x2)))
print(X)
In [13]:
np.take(X, np.random.permutation(X.shape[0]), axis=0, out=X)
Out[13]:
In [14]:
y1 = [2, 4, 8, 10, 20]
y2 = [0.2, 0.4, -0.8, 1.0, -2.0]
Y = np.transpose(np.vstack((y1, y2)))
print(Y, Y.shape)
In [15]:
np.take(Y, np.random.permutation(Y.shape[0]), axis=0, out=Y)
# https://stackoverflow.com/a/35647011
print(Y)
또 다른 방법
In [16]:
def tolist(a):
try:
return list(tolist(i) for i in a)
except TypeError:
return a
In [17]:
z1 = [2, 4, 8, 10, 20]
z2 = [0.2, 0.4, -0.8, 1.0, -2.0]
Z = np.transpose(np.vstack((z1, z2)))
print(Z)
Z1 = tolist(Z)
print(Z1)
In [18]:
import random
random.shuffle(Z1)
print(Z1)
print(np.array(Z1))