다운로드
작성자: admin 작성일시: 2016-04-08 16:12:33 조회수: 7489 다운로드: 317
카테고리: Python 태그목록:

NumPy

요약: NumPy

NumPy란

NumPy Array

In:
import numpy as np
a = np.array([0, 1, 2, 3])
a
Out:
array([0, 1, 2, 3])
요약: Python list와 NumPy array의 차이

Python List vs NumPy Array

  • Python List
    • 여러가지 타입의 원소
    • 메모리 용량이 크고 속도가 느림
    • nesting 가능
    • 전체 연산 불가
  • NumPy Array
    • 동일 타입의 원소
    • 메모리 최적화, 계산 속도 향상
    • 크기(dimension)이 명확하게 정의
    • 전체 연산 가능
In:
L = range(1000)
%timeit [i**2 for i in L]
The slowest run took 4.21 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 93.3 µs per loop
In:
a = np.arange(1000)
%timeit a**2
The slowest run took 45.14 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 2.57 µs per loop
In:
L = range(3)
L
Out:
[0, 1, 2]
In:
L * 2
Out:
[0, 1, 2, 0, 1, 2]
In:
[i * 2 for i in L]
Out:
[0, 2, 4]
In:
a = np.arange(3)
a
Out:
array([0, 1, 2])
In:
a * 2
Out:
array([0, 2, 4])

Create Array (1D)

In:
a = np.array([0, 1, 2, 3])
a
Out:
array([0, 1, 2, 3])
In:
a.ndim
Out:
1
In:
a.shape
Out:
(4,)
In:
len(a)
Out:
4

Create Array (2D)

In:
b = np.array([[0, 1, 2], 
              [3, 4, 5]])    # 2 x 3 array
b
Out:
array([[0, 1, 2],
       [3, 4, 5]])
In:
b.ndim
Out:
2
In:
b.shape
Out:
(2, 3)
In:
len(b)
Out:
2
In:
a2 = np.array([[0, 1, 2, 3]]).T
a2
Out:
array([[0],
       [1],
       [2],
       [3]])
In:
a3 = np.array([[0], [1], [2], [3]])
a3
Out:
array([[0],
       [1],
       [2],
       [3]])
In:
a2.shape
Out:
(4, 1)

Create Array (3D)

In:
c = np.array([[[1,2], 
               [3,4]], 
              [[5,6], 
               [7,8]]])
c
Out:
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
In:
c.ndim
Out:
3
In:
c.shape
Out:
(2, 2, 2)
In:
len(c)
Out:
2

1 dim vs 2 dim

In:
a = np.arange(4)
a
Out:
array([0, 1, 2, 3])
In:
a.shape
Out:
(4,)
In:
b = np.array([[0, 1, 2, 3]])
b
Out:
array([[0, 1, 2, 3]])
In:
b.shape
Out:
(1, 4)
In:
c = np.array([[0], [1], [2], [3]])
c
Out:
array([[0],
       [1],
       [2],
       [3]])
In:
c.shape
Out:
(4, 1)

Transpose

In:
a = np.array([[0, 1, 2, 3]])
a
Out:
array([[0, 1, 2, 3]])
In:
a.shape
Out:
(1, 4)
In:
b = a.T
b
Out:
array([[0],
       [1],
       [2],
       [3]])
In:
b.shape
Out:
(4, 1)
요약: NumPy Arrary 생성 함수

Array Creation Functions

  • arange
  • linspace, logspace
  • zeros, ones
  • rand, randn
  • tile
In:
a = np.arange(10) # 0 .. n-1  (!)
a
Out:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In:
b = np.arange(1, 9, 2) # start, end (exclusive), step
b
Out:
array([1, 3, 5, 7])
In:
c = np.linspace(0, 1, 6)   # start, end, num-points
c
Out:
array([ 0. ,  0.2,  0.4,  0.6,  0.8,  1. ])
In:
d = np.linspace(0, 1, 5, endpoint=False)
d
Out:
array([ 0. ,  0.2,  0.4,  0.6,  0.8])
In:
a = np.ones((3, 3))  # reminder: (3, 3) is a tuple
a
Out:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In:
b = np.zeros((2, 2))
b
Out:
array([[ 0.,  0.],
       [ 0.,  0.]])
In:
c = np.diag([1,2,3])
c
Out:
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])
In:
d = np.eye(4)
d
Out:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])
In:
a = np.array([0, 1, 2])
a
Out:
array([0, 1, 2])
In:
np.tile(a, 2)
Out:
array([0, 1, 2, 0, 1, 2])
In:
np.tile(a, (3, 2))
Out:
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])
In:
np.tile(a, (2, 1, 2))
Out:
array([[[0, 1, 2, 0, 1, 2]],

       [[0, 1, 2, 0, 1, 2]]])
In:
b = np.array([[1, 2], [3, 4]])
b
Out:
array([[1, 2],
       [3, 4]])
In:
np.tile(b, 2)
Out:
array([[1, 2, 1, 2],
       [3, 4, 3, 4]])
In:
np.tile(b, (2, 1))
Out:
array([[1, 2],
       [3, 4],
       [1, 2],
       [3, 4]])
요약: NumPy array의 shape 변경 함수

Shape Change

  • reshape
  • flatten, ravel
In:
a = np.arange(20)
a
Out:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
In:
b = np.reshape(a, (4, 5))
b
Out:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
In:
c = a.reshape(4,5)
c
Out:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
  • 인수가 -1
    • numpy가 나머지 인수들을 이용하여 사이즈를 맞춘다.
In:
a = np.arange(24)
a.reshape(2, 12)
Out:
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]])
In:
a.reshape(2, -1)
Out:
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]])
In:
a.reshape(-1, 12)
Out:
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]])
In:
c
Out:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])
In:
d = c.flatten() # return a copy
d
Out:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
In:
d.base is None
Out:
True
In:
e = c.ravel()
e
Out:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
In:
e.base
Out:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
요약: Numpy stack 명령

Stack

  • hstack
  • vstack
  • dstack
In:
a = np.arange(5)
a
Out:
array([0, 1, 2, 3, 4])
In:
np.vstack([a * 10, a * 20])
Out:
array([[ 0, 10, 20, 30, 40],
       [ 0, 20, 40, 60, 80]])
In:
b = np.arange(5)[:, np.newaxis]
b
Out:
array([[0],
       [1],
       [2],
       [3],
       [4]])
In:
np.hstack([b * 10, b * 20])
Out:
array([[ 0,  0],
       [10, 20],
       [20, 40],
       [30, 60],
       [40, 80]])
In:
a = np.array((1,2,3))
b = np.array((2,3,4))
In:
a
Out:
array([1, 2, 3])
In:
a.shape
Out:
(3,)
In:
np.dstack((a,b))
Out:
array([[[1, 2],
        [2, 3],
        [3, 4]]])
In:
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.dstack((a,b))
Out:
array([[[1, 2]],

       [[2, 3]],

       [[3, 4]]])
요약: NumPy에서 사용 가능한 자료형

dtype

  • bool Boolean (True or False) stored as a byte
  • int8 Byte (-128 to 127)
  • int16 Integer (-32768 to 32767)
  • int32 Integer (-2147483648 to 2147483647)
  • int64 Integer (-9223372036854775808 to 9223372036854775807)
  • uint8 Unsigned integer (0 to 255)
  • uint16 Unsigned integer (0 to 65535)
  • uint32 Unsigned integer (0 to 4294967295)
  • uint64 Unsigned integer (0 to 18446744073709551615)
  • float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
  • float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
  • float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
  • S String
In:
a = np.array([1, 2, 3])
a.dtype
Out:
dtype('int64')
In:
b = np.array([1., 2., 3.])
b.dtype
Out:
dtype('float64')
In:
c = np.array([1, 2, 3], dtype=np.float64)
c.dtype
Out:
dtype('float64')
In:
d = np.array([1+2j, 3+4j, 5+6*1j])
d.dtype
Out:
dtype('complex128')
In:
e = np.array([True, False, False, True])
e.dtype
Out:
dtype('bool')
In:
f = np.array(['Bonjour', 'Hello', 'Hallo',])
f.dtype
Out:
dtype('S7')
  • NaN Not a Number
  • Inf Infinity
In:
x = np.array([1, -1, 0]) / np.array([0, 0, 0])
x
Out:
array([ inf, -inf,  nan])
In:
x[0]
Out:
inf
In:
np.inf, np.nan
Out:
(inf, nan)

Indexing

In:
a = np.arange(10)
a
Out:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In:
a[0], a[2], a[-1]
Out:
(0, 2, 9)
In:
a[::-1]
Out:
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

Multi-dimensional Indexing

In:
l = [[0,0,0],[0,1,0],[0,0,2]]
In:
l[1]
Out:
[0, 1, 0]
In:
l[1][1]
Out:
1
In:
a = np.diag(np.arange(3))
a
Out:
array([[0, 0, 0],
       [0, 1, 0],
       [0, 0, 2]])
In:
a[1, 1]
Out:
1
In:
a[2, 1] = 10 # third line, second column
a
Out:
array([[ 0,  0,  0],
       [ 0,  1,  0],
       [ 0, 10,  2]])
In:
a[2] = [10, 20, 30]
a
Out:
array([[ 0,  0,  0],
       [ 0,  1,  0],
       [10, 20, 30]])

Slicing

In:
a = np.arange(10)
a
Out:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In:
a[2:9:3] # [start:end:step]
Out:
array([2, 5, 8])
In:
a[:4]
Out:
array([0, 1, 2, 3])
In:
a[1:3]
Out:
array([1, 2])
In:
a[::2]
Out:
array([0, 2, 4, 6, 8])
In:
a[3:]
Out:
array([3, 4, 5, 6, 7, 8, 9])

Multi-dimensional Slicing

In:
a = np.arange(6) + (np.arange(6) * 10)[:, np.newaxis]
a
Out:
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])
In:
a[0,:]
Out:
array([0, 1, 2, 3, 4, 5])
In:
a[:,0]
Out:
array([ 0, 10, 20, 30, 40, 50])

newaxis

  • 차원 확장
In:
a = np.arange(4)
a
Out:
array([0, 1, 2, 3])
In:
a.shape
Out:
(4,)
In:
b = np.arange(4).reshape(4,1)
b
Out:
array([[0],
       [1],
       [2],
       [3]])
In:
b.shape
Out:
(4, 1)
In:
c = np.arange(4)[:, np.newaxis]
c
Out:
array([[0],
       [1],
       [2],
       [3]])
In:
c.shape
Out:
(4, 1)

View

  • A slicing operation creates a view on the original array, which is just a way of accessing array data.
  • Thus the original array is not copied in memory.
In:
a = np.arange(10)
a
Out:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In:
b = a[::2]
b
Out:
array([0, 2, 4, 6, 8])
In:
a[0] = 99
a
Out:
array([99,  1,  2,  3,  4,  5,  6,  7,  8,  9])
In:
b
Out:
array([99,  2,  4,  6,  8])

Copy

In:
a = np.arange(5)
a
Out:
array([0, 1, 2, 3, 4])
In:
b = a.copy()
b
Out:
array([0, 1, 2, 3, 4])
In:
a[0] = 99
a
Out:
array([99,  1,  2,  3,  4])
In:
b
Out:
array([0, 1, 2, 3, 4])
요약: NumPy의 fancy 인덱싱

Fancy indexing 팬시 인덱싱

  • Boolean Fancy Indexing
    • True인 원소만 선택
    • 크기가 같아야 한다.
  • list
    • 또는 tuple, or array
    • 지정된 인덱스만 선택
    • 크기가 달라도 된다.
  • multi dimension에도 사용 가능
  • create copy, not view
In:
a = np.arange(20)
a
Out:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
In:
a % 2
Out:
array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1])
In:
idx = (a % 2) == 0
idx
Out:
array([ True, False,  True, False,  True, False,  True, False,  True,
       False,  True, False,  True, False,  True, False,  True, False,
        True, False], dtype=bool)
In:
a[idx]
Out:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
In:
a[(a % 2) == 0]
Out:
array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
In:
a = np.arange(50) * 10
a
Out:
array([  0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120,
       130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250,
       260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380,
       390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490])
In:
idx = [1, 3, 4, -1, 30]
a[idx]
Out:
array([ 10,  30,  40, 490, 300])
In:
a[[1,3,4,-1,30]]
Out:
array([ 10,  30,  40, 490, 300])
In:
a = np.arange(6) + (np.arange(6) * 10)[:, np.newaxis]
a
Out:
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])
In:
a[[0,1,2,3,4],(1,2,3,4,5)]
Out:
array([ 1, 12, 23, 34, 45])
In:
a[3:, [0,2,5]]
Out:
array([[30, 32, 35],
       [40, 42, 45],
       [50, 52, 55]])

Array Operation

Elementwise operations

In:
a = np.array([1, 2, 3, 4])
a
Out:
array([1, 2, 3, 4])
In:
a + 1
Out:
array([2, 3, 4, 5])
In:
2**a
Out:
array([ 2,  4,  8, 16])
In:
b = np.ones(4) + 1
b
Out:
array([ 2.,  2.,  2.,  2.])
In:
a - b
Out:
array([-1.,  0.,  1.,  2.])
In:
a + b
Out:
array([ 3.,  4.,  5.,  6.])
In:
c = np.ones((3, 3))
c
Out:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In:
c * c # element-wise, NOT Matrix product
Out:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In:
c.dot(c) # matrix product
Out:
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.],
       [ 3.,  3.,  3.]])
In:
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
In:
a == b
Out:
array([False,  True, False,  True], dtype=bool)
In:
a > b
Out:
array([False, False,  True, False], dtype=bool)
In:
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
c = np.array([1, 2, 3, 4])
In:
np.array_equal(a, b)
Out:
False
In:
np.array_equal(a, c)
Out:
True
In:
a = np.arange(5)
In:
np.sin(a)
Out:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ])
In:
np.log(a)
Out:
array([       -inf,  0.        ,  0.69314718,  1.09861229,  1.38629436])
In:
np.exp(a)
Out:
array([  1.        ,   2.71828183,   7.3890561 ,  20.08553692,  54.59815003])
In:
np.log10(a)
Out:
array([       -inf,  0.        ,  0.30103   ,  0.47712125,  0.60205999])
In:
a = np.arange(4)
b = np.array([1, 2])
In:
a
Out:
array([0, 1, 2, 3])
In:
b
Out:
array([1, 2])
In:
a + b
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in ()
----> 1 a + b

ValueError: operands could not be broadcast together with shapes (4,) (2,) 

Dimension Reduction Operation

  • sum
  • min, max, argmin, argmax
  • mean, median, std, var
  • all, any
In:
x = np.array([1, 2, 3, 4])
x
Out:
array([1, 2, 3, 4])
In:
np.sum(x)
Out:
10
In:
x.sum()
Out:
10
In:
x = np.array([[1, 1], [2, 2]])
x
Out:
array([[1, 1],
       [2, 2]])

http://www.scipy-lectures.org/_images/reductions.png", style="width: 20%; margin: 0 auto 0 auto;">

In:
x.sum()
Out:
6
In:
x.sum(axis=0)   # columns (first dimension)
Out:
array([3, 3])
In:
x.sum(axis=1)   # rows (second dimension)
Out:
array([2, 4])
In:
x = np.array([1, 3, 2])
In:
x.min()
Out:
1
In:
x.max()
Out:
3
In:
x.argmin()  # index of minimum
Out:
0
In:
x.argmax()  # index of maximum
Out:
1
In:
np.all([True, True, False])
Out:
False
In:
np.any([True, True, False])
Out:
True
In:
a = np.zeros((100, 100), dtype=np.int)
a
Out:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])
In:
np.any(a != 0)
Out:
False
In:
np.all(a == a)
Out:
True
In:
a = np.array([1, 2, 3, 2])
b = np.array([2, 2, 3, 2])
c = np.array([6, 4, 4, 5])
In:
((a <= b) & (b <= c)).all()
Out:
True
In:
x = np.array([1, 2, 3, 1])
y = np.array([[1, 2, 3], [5, 6, 1]])
In:
x.mean()
Out:
1.75
In:
np.median(x)
Out:
1.5
In:
np.median(y, axis=-1) # last axis
Out:
array([ 2.,  5.])
In:
x.std()          # full population standard dev.
Out:
0.82915619758884995

Broadcasting

In:
a = np.tile(np.arange(0, 40, 10), (3, 1)).T
a
Out:
array([[ 0,  0,  0],
       [10, 10, 10],
       [20, 20, 20],
       [30, 30, 30]])
In:
b = np.array([0, 1, 2])
b
Out:
array([0, 1, 2])
In:
a + b
Out:
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22],
       [30, 31, 32]])
In:
a[:,0][:, np.newaxis]
Out:
array([[ 0],
       [10],
       [20],
       [30]])
In:
a[:,0][:, np.newaxis] + b
Out:
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22],
       [30, 31, 32]])
In:
a = np.ones((4, 5))
a
Out:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
In:
a[0]
Out:
array([ 1.,  1.,  1.,  1.,  1.])
In:
a[0] = 2
a
Out:
array([[ 2.,  2.,  2.,  2.,  2.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
In:
x, y = np.arange(5), np.arange(5)[:, np.newaxis]
In:
x
Out:
array([0, 1, 2, 3, 4])
In:
y
Out:
array([[0],
       [1],
       [2],
       [3],
       [4]])
In:
distance = np.sqrt(x ** 2 + y ** 2)
distance
Out:
array([[ 0.        ,  1.        ,  2.        ,  3.        ,  4.        ],
       [ 1.        ,  1.41421356,  2.23606798,  3.16227766,  4.12310563],
       [ 2.        ,  2.23606798,  2.82842712,  3.60555128,  4.47213595],
       [ 3.        ,  3.16227766,  3.60555128,  4.24264069,  5.        ],
       [ 4.        ,  4.12310563,  4.47213595,  5.        ,  5.65685425]])

ogrid, mgrid, meshgrid

In:
x, y = np.ogrid[0:3, 0:5]
In:
x
Out:
array([[0],
       [1],
       [2]])
In:
y
Out:
array([[0, 1, 2, 3, 4]])
In:
np.ogrid[-1:1:3j, -1:1:5j]
Out:
[array([[-1.],
        [ 0.],
        [ 1.]]), array([[-1. , -0.5,  0. ,  0.5,  1. ]])]
In:
x, y = np.mgrid[0:3, 0:5]
In:
x
Out:
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2]])
In:
y
Out:
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
In:
np.mgrid[-1:1:3j, -1:1:5j]
Out:
array([[[-1. , -1. , -1. , -1. , -1. ],
        [ 0. ,  0. ,  0. ,  0. ,  0. ],
        [ 1. ,  1. ,  1. ,  1. ,  1. ]],

       [[-1. , -0.5,  0. ,  0.5,  1. ],
        [-1. , -0.5,  0. ,  0.5,  1. ],
        [-1. , -0.5,  0. ,  0.5,  1. ]]])
In:
X, Y = np.meshgrid(np.arange(3), np.arange(5))
In:
X
Out:
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
In:
Y
Out:
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4]])
In:
zip(X.ravel(), Y.ravel())
Out:
[(0, 0),
 (1, 0),
 (2, 0),
 (0, 1),
 (1, 1),
 (2, 1),
 (0, 2),
 (1, 2),
 (2, 2),
 (0, 3),
 (1, 3),
 (2, 3),
 (0, 4),
 (1, 4),
 (2, 4)]
In:
plt.scatter(*np.vstack(np.meshgrid(np.linspace(-1,1,10), np.linspace(-2,2,10))).reshape(2,-1).tolist())
Out:

sort

In:
a = np.array([[4, 3, 5], [1, 2, 1]])
a
Out:
array([[4, 3, 5],
       [1, 2, 1]])
In:
a[:,0]
Out:
array([4, 1])
In:
b = np.sort(a, axis=0)
b
Out:
array([[1, 2, 1],
       [4, 3, 5]])
In:
b = np.sort(a, axis=1)
b
Out:
array([[3, 4, 5],
       [1, 1, 2]])
In:
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
j
Out:
array([2, 3, 1, 0])
In:
a[j]
Out:
array([1, 2, 3, 4])
요약: NumPy 유니버설 함수

Array용 수학 함수

  • universal function
    • 빠른 element-wise (vectorized) 연산
  • 모든 NumPy/Scipy 수학 함수는 자동으로 vectorized 연산 수행
In:
x = range(10)
x
Out:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In:
import math
math.exp(x)    
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in ()
      1 import math
----> 2 math.exp(x)

TypeError: a float is required
In:
math.exp(x[0])
Out:
1.0
In:
[math.exp(x_i) for x_i in x]
Out:
[1.0,
 2.718281828459045,
 7.38905609893065,
 20.085536923187668,
 54.598150033144236,
 148.4131591025766,
 403.4287934927351,
 1096.6331584284585,
 2980.9579870417283,
 8103.083927575384]
In:
np.exp(x)
Out:
array([  1.00000000e+00,   2.71828183e+00,   7.38905610e+00,
         2.00855369e+01,   5.45981500e+01,   1.48413159e+02,
         4.03428793e+02,   1.09663316e+03,   2.98095799e+03,
         8.10308393e+03])

Random Number

요약: NumPy 의 랜덤 넘버 생성 관련 함수

numpy.random 서브패키지

  • seed: pseudo random 상태 설정
  • shuffle: 조합(combination)
  • choice: 순열(permutation) 및 조합(combination)
  • rand: uniform
  • random_integers: uniform integer
  • randn: Gaussina normal

seed

  • 컴퓨터의 랜덤 생성은 사실 랜덤이 아니다.
  • 랜덤처럼 보이지만 정해진 알고리즘에 의해 생성되는 규칙적인 순열
  • 시작점이 정해지면 랜덤 함수를 사용해도 정해진 숫자가 나온다.
In:
np.random.seed(0)
  • numpy.random.shuffle(x)
    • Parameters:
      • x : array_like
        • The array or list to be shuffled.
In:
x = np.arange(10)
np.random.shuffle(x)
x
Out:
array([2, 8, 4, 9, 1, 6, 7, 3, 0, 5])
요약: numpy.random.choice
  • numpy.random.choice(a, size=None, replace=True, p=None)
    • Parameters:
      • a : 1-D array-like or int
        • If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n)
      • size : int or tuple of ints, optional
        • Output shape. If the given shape is, e.g., (m, n, k), then m n k samples are drawn. Default is None, in which case a single value is returned.
      • replace : boolean, optional
        • Whether the sample is with or without replacement
      • p : 1-D array-like, optional
        • The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
    • Returns:
      • samples : 1-D ndarray, shape (size,)
        • The generated random samples
In:
# same as shuffle
np.random.choice(5, 5, replace=False)
Out:
array([1, 0, 2, 3, 4])
In:
np.random.choice(5, 3, replace=False)
Out:
array([3, 1, 2])
In:
np.random.choice(5, 10)
Out:
array([0, 1, 1, 0, 1, 4, 3, 0, 3, 0])
In:
np.random.choice(5, 10, p=[0.1, 0, 0.3, 0.6, 0])
Out:
array([3, 3, 3, 3, 2, 3, 2, 3, 3, 3])
In:
x = np.random.rand(10000)
print(x[:10])
sns.distplot(x)
[ 0.26455561  0.77423369  0.45615033  0.56843395  0.0187898   0.6176355
  0.61209572  0.616934    0.94374808  0.6818203 ]
Out:
In:
np.random.rand(3,2)
Out:
array([[ 0.87423081,  0.47428492],
       [ 0.66369491,  0.88164841],
       [ 0.30465899,  0.89576302]])
In:
x = np.random.random_integers(-100, 100, 50)
sns.distplot(x, rug=True)
Out:
In:
x = np.random.randn(1000)
sns.distplot(x, rug=True)
Out:
In:
np.random.randn(3,4)
Out:
array([[-0.24084301, -0.6676534 ,  0.06900028,  0.45158204],
       [-0.23342743, -0.33696328, -0.49507512, -0.49871364],
       [-0.18672772,  0.24499567, -0.77532038, -0.23060432]])
요약: NumPy의 랜덤 넘버 카운트 함수

random number count

  • discrete values
    • unique()
    • bincount()
  • continuous values
    • histogram()
In:
np.unique([11, 11, 2, 2, 34, 34])
Out:
array([ 2, 11, 34])
In:
a = np.array([[1, 1], [2, 3]])
np.unique(a)
Out:
array([1, 2, 3])
In:
a = np.array(['a', 'b', 'b', 'c', 'a'])
index, count = np.unique(a, return_counts=True)
In:
count
Out:
array([2, 2, 1])
In:
index
Out:
array(['a', 'b', 'c'], 
      dtype='|S1')
In:
np.bincount([1, 1, 2, 2, 3, 3], minlength=6)
Out:
array([0, 2, 2, 2, 0, 0])
In:
np.histogram([1.1, 2.5, 1.8, 2.4, 0.7], bins=[0, 1, 2, 3])
Out:
(array([1, 2, 2]), array([0, 1, 2, 3]))
In:
np.histogram([1, 2, 1], bins=[0, 1, 2, 3])
Out:
(array([0, 2, 1]), array([0, 1, 2, 3]))
In:
np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3])
Out:
(array([1, 4, 1]), array([0, 1, 2, 3]))
In:
np.histogram(np.arange(4), bins=np.arange(5), density=True)
Out:
(array([ 0.25,  0.25,  0.25,  0.25]), array([0, 1, 2, 3, 4]))

질문/덧글

array 함수에서 bracket 한번씌울 때와 두번씌울 떄의 차이 than*** 2016년 7월 12일 7:11 오후

a = np.array([[1,2,3,4,5]])
b = a.T
할 때는 transpose가 이루어지는데

a = np.array([1,2,3,4,5])
b = a.T
할 때는 transpose가 안됩니다.
array를 만들때 []를 한번 하는지, 두번하는지 차이인데 둘은 어떤 차이점이 있는 것인가요?

답변: array 함수에서 bracket 한번씌울 때와 두번씌울 떄의 차이 관리자 2016년 7월 12일 7:16 오후

np.array([[1,2,3,4,5]]) 는 2차원 행렬이고 np.array([1,2,3,4,5])는 1차원 벡터입니다. 1차원 벡터에서는 수학적으로 transpose란 연산이 없기 때문에 아무런 변화가 없습니다.