In [1]:
import numpy as np

np.random.choice

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())


[ 0.1  0.   0.6  0.3]
[ 0.1  0.   0.6  0.3]

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)


[98, 0, 611, 291]

In [5]:
v = np.array(v)
print(v / float(ntest))


[ 0.098  0.     0.611  0.291]

np.random.permutation


In [6]:
m1 = np.random.permutation(5)
print(m1)


[3 0 2 4 1]

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)


[1 2 3 4 5]
[4 5 1 2 3]
[3, 4, 0, 1, 2]

np.random.permutation #2


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)


[[  2.    0.2]
 [  4.    0.4]
 [  8.   -0.8]
 [ 10.    1. ]
 [ 20.   -2. ]]
(5, 2)

In [10]:
permutation = np.random.permutation(X.shape[0])

In [11]:
X_shuffle=  X[permutation]
print(X_shuffle)


[[  4.    0.4]
 [  2.    0.2]
 [  8.   -0.8]
 [ 10.    1. ]
 [ 20.   -2. ]]
  • 다른 방법

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)


[[  2.    0.2]
 [  4.    0.4]
 [  8.   -0.8]
 [ 10.    1. ]
 [ 20.   -2. ]]

In [13]:
np.take(X, np.random.permutation(X.shape[0]), axis=0, out=X)


Out[13]:
array([[  2. ,   0.2],
       [  4. ,   0.4],
       [ 20. ,  -2. ],
       [ 10. ,   1. ],
       [  8. ,  -0.8]])
  • 다른 방법

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)


(array([[  2. ,   0.2],
       [  4. ,   0.4],
       [  8. ,  -0.8],
       [ 10. ,   1. ],
       [ 20. ,  -2. ]]), (5, 2))

In [15]:
np.take(Y, np.random.permutation(Y.shape[0]), axis=0, out=Y)
# https://stackoverflow.com/a/35647011
print(Y)


[[  8.   -0.8]
 [  4.    0.4]
 [ 20.   -2. ]
 [  2.    0.2]
 [ 10.    1. ]]

또 다른 방법


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)


[[  2.    0.2]
 [  4.    0.4]
 [  8.   -0.8]
 [ 10.    1. ]
 [ 20.   -2. ]]
[[2.0, 0.20000000000000001], [4.0, 0.40000000000000002], [8.0, -0.80000000000000004], [10.0, 1.0], [20.0, -2.0]]

In [18]:
import random
random.shuffle(Z1)
print(Z1)
print(np.array(Z1))


[[10.0, 1.0], [8.0, -0.80000000000000004], [2.0, 0.20000000000000001], [20.0, -2.0], [4.0, 0.40000000000000002]]
[[ 10.    1. ]
 [  8.   -0.8]
 [  2.    0.2]
 [ 20.   -2. ]
 [  4.    0.4]]