티스토리 뷰

beginner/파이썬 기초

NumPy_랜덤-2

johh 2019. 2. 13. 13:11
2019.02.13-1

연습문제

  1. 체스판 형태로 배열을 만들려고 한다. a= np.arange(81).reshape(9,9) chess = a%2 위의 결과를 출력하여 확인하시오.

  2. 위의 결과를 plt.imshow() 함수를 써서 표시하시오.

  3. 100x100 형태의 표준정규분포로 랜덤 배열을 생성하여, plt.imshow() 함수로 표시하시오.

In [2]:
import numpy as np
a = np.arange(81).reshape(9,9)
chess = a%2
In [3]:
chess
Out[3]:
array([[0, 1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0, 1],
       [0, 1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0, 1],
       [0, 1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0, 1],
       [0, 1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0, 1],
       [0, 1, 0, 1, 0, 1, 0, 1, 0]], dtype=int32)
In [6]:
import matplotlib.pyplot as plt
plt.imshow(chess)
plt.colorbar()
Out[6]:
<matplotlib.colorbar.Colorbar at 0x61d742c278>
In [11]:
an = np.random.randn(100,100)
np.shape(an)
Out[11]:
(100, 100)
In [12]:
plt.imshow(an)
plt.colorbar()
Out[12]:
<matplotlib.colorbar.Colorbar at 0x61d7441780>

Numpy 랜덤생성 함수 2

normal()

  • 평균과 표준편차를 지정하여 랜덤 숫자 선택
In [14]:
a = np.random.normal(10,5,size=10000)   # 0.00235 = 2.35e-3, 235 = 2.35e+2
a[:100]
Out[14]:
array([ 9.50022853,  6.20826113, 17.95520265, 10.772043  ,  1.97647997,
       13.55152136,  7.85331915, 11.36036251,  6.3681703 ,  6.34359503,
        0.57446817,  9.0043639 ,  9.52948626, 17.97907259, 10.98491136,
        6.0126934 ,  8.41435832, 17.12694737, 11.02073687,  7.84408695,
        3.47269819,  9.629412  ,  8.40603016, 16.4667242 , 10.65017429,
       -2.27382985, 11.40336818, 16.31434768,  5.79626416, 12.94935604,
        3.91411756, 13.44000963,  3.0810663 ,  9.29359041, 11.6844497 ,
       10.2524948 ,  3.1894318 , 10.56360424, 10.77670253,  5.20298247,
        6.4574972 ,  3.30574417, 15.07953682, 11.76723741,  8.87071117,
       13.34776276, 13.02940055,  5.78978882,  7.68180477, -0.54484328,
       10.85867234,  9.45777042,  0.65813456, 12.0466619 , 22.69089738,
       11.09432302, 11.78725787,  9.71531808,  0.11460781,  9.60442571,
       14.23674082,  3.70223833,  5.50583633, 20.16316311,  2.52163441,
        9.17882304,  1.15220912,  5.52354956,  1.40315369, 12.88481778,
        7.11016454,  9.20983311,  7.7518365 ,  7.84463688,  5.98013852,
        0.48110152, 11.0551421 , 17.9490547 , 14.14301458,  5.27753761,
        9.56799819,  4.18011144, 11.64543618, 15.83433144,  5.55529629,
       14.11430639, 12.16876846, 14.14428281, 14.01921846, 11.45673019,
       10.93027948,  1.43735709,  5.14307822,  4.2002981 , 13.50043267,
        2.47574017, 14.98443835,  7.04887605, 19.69609251, 10.94075783])
In [17]:
plt.hist(a, bins=100)
plt.title('Histogram')
Out[17]:
Text(0.5,1,'Histogram')
In [18]:
a.mean()
Out[18]:
10.033316428471684
In [19]:
a.std()
Out[19]:
4.985285044832816
In [20]:
(a<-5).sum()
Out[20]:
14

uniform()

정해진 범위 내에서 실수 선택

In [21]:
a = np.random.normal([100,200],20,size=10000) 
a[:100]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-21-1cd34c01950e> in <module>()
----> 1 a = np.random.normal([100,200],20,size=10000)
      2 a[:100]

mtrand.pyx in mtrand.RandomState.normal()

mtrand.pyx in mtrand.cont2_array()

ValueError: shape mismatch: objects cannot be broadcast to a single shape

permutation()

permutation은 섞어 결과를 내어주는데, shuple은 자기 자신을 섞음.

In [23]:
a = np.arange(100)
a
Out[23]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
In [24]:
a2 = np.random.permutation(a) # 기존의 값들을 마음대로 섞어버리기
a2
Out[24]:
array([45, 12, 39, 26, 77, 44, 17, 40, 31,  1, 64, 30, 19, 24, 22, 32, 47,
        7, 69, 74, 36, 51, 79, 92, 96, 54, 91,  5, 90, 73, 83, 85, 61, 13,
       70, 29, 76,  8, 38, 60, 37, 66, 46, 14, 34, 20, 67,  6, 71, 18, 11,
       62, 94,  4, 58, 78, 86, 27, 41, 42, 81, 84, 50, 55, 99, 28, 16, 68,
       23,  2, 65, 88, 52, 98, 80, 95, 57, 48,  0, 59, 89, 33, 53, 87, 10,
       15, 93, 21, 97, 35, 82, 75, 25,  3,  9, 49, 72, 43, 56, 63])
In [30]:
plt.plot(a, label='a')
plt.plot(a2, 'ro--', label='a2')        # r: red, o : 동그라미, --: 점선, g: green, ^: 세모, ':' : 세밀한 점선 
plt.legend()     # 범례를 쓰겠다.
Out[30]:
<matplotlib.legend.Legend at 0x61d7fd1c88>

연습문제

Iris 데이터를 줄 단위로(샘플 단위로) 섞으시오.

In [61]:
f = open('iris.csv')

line = f.readline()
features = line.strip().split(',')[:4]

labels = ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']

data=[]
for line in f:
    I = line.strip().split(',')
    I[:4] = [float(i) for i in I[:4]]
    I[4] = labels.index(I[4])
    
    data.append(I)
    
f.close()

iris = np.array(data)
In [63]:
idx = np.arange(150)
idx
Out[63]:
array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
        13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
        26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
        39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
        52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
        65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
        78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
        91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
       104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
       117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
       130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
       143, 144, 145, 146, 147, 148, 149])
In [64]:
idx2 = np.random.permutation(idx)
idx2
Out[64]:
array([127,   3, 126, 107,  32,  17, 146,  38, 143,  31, 116, 130,   8,
        61, 141,  77,  70, 108,  12,  26,  51, 145,  83, 140,   0,  21,
         6,   2,  85,  48, 106,  69,   5, 144,  27, 117,  59, 104,  10,
       131,  67,  79,  65, 112,   7,  49, 103, 148,  16, 119, 100,  50,
        52, 135,  30,  18,  46,  14,  89, 101,  35,  29,   4,   9,  73,
       138, 125,  64, 121, 124, 105,  94, 142,  33, 122, 113, 139,  20,
       114,  93,  76,  97,  15,  23, 109, 133,  62,  24, 102, 134,  37,
        75,  56,  13, 132, 115,  86,  88,  72, 136, 137,  74,  47, 147,
        41,  39,  57,  42,  55,  58,  63,  54,  60, 118,  82,  66,  36,
        81,  71,  91,  87, 120,  45, 110,  28,  68, 111,  99,  95,  34,
        90,  11,  80,  25,  43,  22, 123,  98,   1,  96, 149,  40,  53,
        19, 129,  84,  78,  44,  92, 128])
In [65]:
iris[idx2]             # 샘플을 분리해 낼 때 사용
Out[65]:
array([[6.1, 3. , 4.9, 1.8, 2. ],
       [4.6, 3.1, 1.5, 0.2, 0. ],
       [6.2, 2.8, 4.8, 1.8, 2. ],
       [7.3, 2.9, 6.3, 1.8, 2. ],
       [5.2, 4.1, 1.5, 0.1, 0. ],
       [5.1, 3.5, 1.4, 0.3, 0. ],
       [6.3, 2.5, 5. , 1.9, 2. ],
       [4.4, 3. , 1.3, 0.2, 0. ],
       [6.8, 3.2, 5.9, 2.3, 2. ],
       [5.4, 3.4, 1.5, 0.4, 0. ],
       [6.5, 3. , 5.5, 1.8, 2. ],
       [7.4, 2.8, 6.1, 1.9, 2. ],
       [4.4, 2.9, 1.4, 0.2, 0. ],
       [5.9, 3. , 4.2, 1.5, 1. ],
       [6.9, 3.1, 5.1, 2.3, 2. ],
       [6.7, 3. , 5. , 1.7, 1. ],
       [5.9, 3.2, 4.8, 1.8, 1. ],
       [6.7, 2.5, 5.8, 1.8, 2. ],
       [4.8, 3. , 1.4, 0.1, 0. ],
       [5. , 3.4, 1.6, 0.4, 0. ],
       [6.4, 3.2, 4.5, 1.5, 1. ],
       [6.7, 3. , 5.2, 2.3, 2. ],
       [6. , 2.7, 5.1, 1.6, 1. ],
       [6.7, 3.1, 5.6, 2.4, 2. ],
       [5.1, 3.5, 1.4, 0.2, 0. ],
       [5.1, 3.7, 1.5, 0.4, 0. ],
       [4.6, 3.4, 1.4, 0.3, 0. ],
       [4.7, 3.2, 1.3, 0.2, 0. ],
       [6. , 3.4, 4.5, 1.6, 1. ],
       [5.3, 3.7, 1.5, 0.2, 0. ],
       [4.9, 2.5, 4.5, 1.7, 2. ],
       [5.6, 2.5, 3.9, 1.1, 1. ],
       [5.4, 3.9, 1.7, 0.4, 0. ],
       [6.7, 3.3, 5.7, 2.5, 2. ],
       [5.2, 3.5, 1.5, 0.2, 0. ],
       [7.7, 3.8, 6.7, 2.2, 2. ],
       [5.2, 2.7, 3.9, 1.4, 1. ],
       [6.5, 3. , 5.8, 2.2, 2. ],
       [5.4, 3.7, 1.5, 0.2, 0. ],
       [7.9, 3.8, 6.4, 2. , 2. ],
       [5.8, 2.7, 4.1, 1. , 1. ],
       [5.7, 2.6, 3.5, 1. , 1. ],
       [6.7, 3.1, 4.4, 1.4, 1. ],
       [6.8, 3. , 5.5, 2.1, 2. ],
       [5. , 3.4, 1.5, 0.2, 0. ],
       [5. , 3.3, 1.4, 0.2, 0. ],
       [6.3, 2.9, 5.6, 1.8, 2. ],
       [6.2, 3.4, 5.4, 2.3, 2. ],
       [5.4, 3.9, 1.3, 0.4, 0. ],
       [6. , 2.2, 5. , 1.5, 2. ],
       [6.3, 3.3, 6. , 2.5, 2. ],
       [7. , 3.2, 4.7, 1.4, 1. ],
       [6.9, 3.1, 4.9, 1.5, 1. ],
       [7.7, 3. , 6.1, 2.3, 2. ],
       [4.8, 3.1, 1.6, 0.2, 0. ],
       [5.7, 3.8, 1.7, 0.3, 0. ],
       [5.1, 3.8, 1.6, 0.2, 0. ],
       [5.8, 4. , 1.2, 0.2, 0. ],
       [5.5, 2.5, 4. , 1.3, 1. ],
       [5.8, 2.7, 5.1, 1.9, 2. ],
       [5. , 3.2, 1.2, 0.2, 0. ],
       [4.7, 3.2, 1.6, 0.2, 0. ],
       [5. , 3.6, 1.4, 0.2, 0. ],
       [4.9, 3.1, 1.5, 0.1, 0. ],
       [6.1, 2.8, 4.7, 1.2, 1. ],
       [6. , 3. , 4.8, 1.8, 2. ],
       [7.2, 3.2, 6. , 1.8, 2. ],
       [5.6, 2.9, 3.6, 1.3, 1. ],
       [5.6, 2.8, 4.9, 2. , 2. ],
       [6.7, 3.3, 5.7, 2.1, 2. ],
       [7.6, 3. , 6.6, 2.1, 2. ],
       [5.6, 2.7, 4.2, 1.3, 1. ],
       [5.8, 2.7, 5.1, 1.9, 2. ],
       [5.5, 4.2, 1.4, 0.2, 0. ],
       [7.7, 2.8, 6.7, 2. , 2. ],
       [5.7, 2.5, 5. , 2. , 2. ],
       [6.9, 3.1, 5.4, 2.1, 2. ],
       [5.4, 3.4, 1.7, 0.2, 0. ],
       [5.8, 2.8, 5.1, 2.4, 2. ],
       [5. , 2.3, 3.3, 1. , 1. ],
       [6.8, 2.8, 4.8, 1.4, 1. ],
       [6.2, 2.9, 4.3, 1.3, 1. ],
       [5.7, 4.4, 1.5, 0.4, 0. ],
       [5.1, 3.3, 1.7, 0.5, 0. ],
       [7.2, 3.6, 6.1, 2.5, 2. ],
       [6.3, 2.8, 5.1, 1.5, 2. ],
       [6. , 2.2, 4. , 1. , 1. ],
       [4.8, 3.4, 1.9, 0.2, 0. ],
       [7.1, 3. , 5.9, 2.1, 2. ],
       [6.1, 2.6, 5.6, 1.4, 2. ],
       [4.9, 3.1, 1.5, 0.1, 0. ],
       [6.6, 3. , 4.4, 1.4, 1. ],
       [6.3, 3.3, 4.7, 1.6, 1. ],
       [4.3, 3. , 1.1, 0.1, 0. ],
       [6.4, 2.8, 5.6, 2.2, 2. ],
       [6.4, 3.2, 5.3, 2.3, 2. ],
       [6.7, 3.1, 4.7, 1.5, 1. ],
       [5.6, 3. , 4.1, 1.3, 1. ],
       [6.3, 2.5, 4.9, 1.5, 1. ],
       [6.3, 3.4, 5.6, 2.4, 2. ],
       [6.4, 3.1, 5.5, 1.8, 2. ],
       [6.4, 2.9, 4.3, 1.3, 1. ],
       [4.6, 3.2, 1.4, 0.2, 0. ],
       [6.5, 3. , 5.2, 2. , 2. ],
       [4.5, 2.3, 1.3, 0.3, 0. ],
       [5.1, 3.4, 1.5, 0.2, 0. ],
       [4.9, 2.4, 3.3, 1. , 1. ],
       [4.4, 3.2, 1.3, 0.2, 0. ],
       [5.7, 2.8, 4.5, 1.3, 1. ],
       [6.6, 2.9, 4.6, 1.3, 1. ],
       [6.1, 2.9, 4.7, 1.4, 1. ],
       [6.5, 2.8, 4.6, 1.5, 1. ],
       [5. , 2. , 3.5, 1. , 1. ],
       [7.7, 2.6, 6.9, 2.3, 2. ],
       [5.8, 2.7, 3.9, 1.2, 1. ],
       [5.6, 3. , 4.5, 1.5, 1. ],
       [5.5, 3.5, 1.3, 0.2, 0. ],
       [5.5, 2.4, 3.7, 1. , 1. ],
       [6.1, 2.8, 4. , 1.3, 1. ],
       [6.1, 3. , 4.6, 1.4, 1. ],
       [6.3, 2.3, 4.4, 1.3, 1. ],
       [6.9, 3.2, 5.7, 2.3, 2. ],
       [4.8, 3. , 1.4, 0.3, 0. ],
       [6.5, 3.2, 5.1, 2. , 2. ],
       [5.2, 3.4, 1.4, 0.2, 0. ],
       [6.2, 2.2, 4.5, 1.5, 1. ],
       [6.4, 2.7, 5.3, 1.9, 2. ],
       [5.7, 2.8, 4.1, 1.3, 1. ],
       [5.7, 3. , 4.2, 1.2, 1. ],
       [4.9, 3.1, 1.5, 0.1, 0. ],
       [5.5, 2.6, 4.4, 1.2, 1. ],
       [4.8, 3.4, 1.6, 0.2, 0. ],
       [5.5, 2.4, 3.8, 1.1, 1. ],
       [5. , 3. , 1.6, 0.2, 0. ],
       [5. , 3.5, 1.6, 0.6, 0. ],
       [4.6, 3.6, 1. , 0.2, 0. ],
       [6.3, 2.7, 4.9, 1.8, 2. ],
       [5.1, 2.5, 3. , 1.1, 1. ],
       [4.9, 3. , 1.4, 0.2, 0. ],
       [5.7, 2.9, 4.2, 1.3, 1. ],
       [5.9, 3. , 5.1, 1.8, 2. ],
       [5. , 3.5, 1.3, 0.3, 0. ],
       [5.5, 2.3, 4. , 1.3, 1. ],
       [5.1, 3.8, 1.5, 0.3, 0. ],
       [7.2, 3. , 5.8, 1.6, 2. ],
       [5.4, 3. , 4.5, 1.5, 1. ],
       [6. , 2.9, 4.5, 1.5, 1. ],
       [5.1, 3.8, 1.9, 0.4, 0. ],
       [5.8, 2.6, 4. , 1.2, 1. ],
       [6.4, 2.8, 5.6, 2.1, 2. ]])
In [69]:
iris2 = iris[idx2]

iris_train = iris2[:100]
iris_test = iris2[100:]

iris_train.shape, iris_test.shape
Out[69]:
((100, 5), (50, 5))

choice

  • 주어진 데이터에서 임의로 뽑느다.
  • (주의) 반복 선택이 가능하다.
In [70]:
a = np.arange(10)
a
Out[70]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [72]:
np.random.choice(a,100)     #뽑을때마다 독립적
Out[72]:
array([7, 4, 0, 1, 0, 7, 0, 9, 4, 1, 8, 1, 4, 6, 6, 4, 6, 5, 3, 7, 5, 2,
       7, 8, 6, 7, 1, 6, 0, 6, 3, 5, 3, 6, 9, 4, 8, 3, 4, 3, 1, 8, 0, 5,
       3, 0, 6, 5, 0, 6, 5, 1, 0, 7, 0, 3, 7, 9, 7, 6, 8, 3, 4, 9, 0, 3,
       7, 5, 1, 7, 5, 8, 1, 5, 3, 5, 4, 6, 3, 3, 7, 8, 9, 5, 3, 5, 5, 7,
       3, 0, 4, 6, 5, 4, 4, 8, 2, 3, 0, 0])
In [73]:
np.random.choice([0,1],10)
Out[73]:
array([0, 0, 0, 1, 1, 0, 0, 0, 1, 1])
In [74]:
np.random.randint(2, size=10)
Out[74]:
array([0, 1, 1, 1, 1, 1, 0, 0, 0, 0])

seed()

  • 동일한 실험 결과를 위해 랜덤 발생을 고정시킨다.
In [76]:
np.random.seed(2013)
a = np.random.randint(2,size=10)
b = np.random.randint(2,size=10)
display(a,b)
array([0, 0, 0, 1, 1, 1, 1, 0, 1, 0])
array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0])
In [78]:
np.random.seed(2013)               # 랜덤인데 위에 값 그대로 나옴
a = np.random.randint(2,size=10)
b = np.random.randint(2,size=10)
display(a,b)
array([0, 0, 0, 1, 1, 1, 1, 0, 1, 0])
array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0])

'beginner > 파이썬 기초' 카테고리의 다른 글

NumPy_함수  (0) 2019.02.13
NumPy_사칙연산  (0) 2019.02.13
NumPy_랜덤-1  (0) 2019.02.12
NumPy_생성하기  (0) 2019.02.12
함수와 모듈  (0) 2019.02.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함