NumPy

NumPy("넘파이"라 읽는다)는 행렬이나 일반적으로 대규모 다차원 배열을 쉽게 처리할 수 있도록 지원하는 파이썬의 라이브러리이다. NumPy는 데이터 구조 외에도 수치 계산을 위해 효율적으로 구현된 기능을 제공한다.
예제.
»&gt; import numpy as np
»&gt; x = np.array([1, 2, 3])
»&gt; x
array([1, 2, 3])
»&gt; y = np.arange(10) # like Python's range, but returns an array
»&gt; y
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
»&gt; a = np.array([1, 2, 3, 6])
»&gt; b = np.linspace(0, 2, 4) # create an array with four equally spaced points starting with 0 and ending with 2.
»&gt; c = a - b
»&gt; c
array([ 1. , 1.33333333, 1.66666667, 4. ])
»&gt; a**2
array([ 1, 4, 9, 36])
»&gt; a = np.linspace(-np.pi, np.pi, 100)
»&gt; b = np.sin(a)
»&gt; c = np.cos(a)
»&gt; from numpy.random import rand
»&gt; from numpy.linalg import solve, inv
»&gt; a = np.array(1, 2, 3], [3, 4, 6.7], [5, 9.0, 5)
»&gt; a.transpose()
array( 1. , 3. , 5. ],
 [ 2. , 4. , 9. ],
 [ 3. , 6.7, 5. )
»&gt; inv(a)
array(-2.27683616, 0.96045198, 0.07909605],
 [ 1.04519774, -0.56497175, 0.1299435 ],
 [ 0.39548023, 0.05649718, -0.11299435)
»&gt; b = np.array([3, 2, 1])
»&gt; solve(a, b) # solve the equation ax = b
array([-4.83050847, 2.13559322, 1.18644068])
»&gt; c = rand(3, 3) * 20 # create a 3x3 random matrix of values within [0,1] scaled by 20
»&gt; c
array( 3.98732789, 2.47702609, 4.71167924],
 [ 9.24410671, 5.5240412 , 10.6468792 ],
 [ 10.38136661, 8.44968437, 15.17639591)
»&gt; np.dot(a, c) # matrix multiplication
array( 53.61964114, 38.8741616 , 71.53462537],
 [ 118.4935668 , 86.14012835, 158.40440712],
 [ 155.04043289, 104.3499231 , 195.26228855)
»&gt; a @ c # Starting with Python 3.5 and NumPy 1.10
array( 53.61964114, 38.8741616 , 71.53462537],
 [ 118.4935668 , 86.14012835, 158.40440712],
 [ 155.04043289, 104.3499231 , 195.26228855)
»&gt; import numpy as np
»&gt; import cv2
»&gt; r = np.reshape(np.arange(256*256)%256,(256,256)) # 256x256 pixel array with a horizontal gradient from 0 to 255 for the red color channel
»&gt; g = np.zeros_like(r) # array of same size and type as r but filled with 0s for the green color channel
»&gt; b = r.T # transposed r will give a vertical gradient for the blue color channel
»&gt; cv2.imwrite('gradients.png', np.dstack([b,g,r])) # OpenCV images are interpreted as BGR, the depth-stacked array will be written to an 8bit RGB PNG-file called 'gradients.png'
True
»&gt; # # # Pure iterative Python # # #
»&gt; points = 9,2,8],[4,7,2],[3,4,4],[5,6,9],[5,0,7],[8,2,7],[0,3,2],[7,3,0],[6,1,1],[2,9,6
»&gt; qPoint = [4,5,3]
»&gt; minIdx = -1
»&gt; minDist = -1
»&gt; for idx, point in enumerate(points): # iterate over all points
 dist = sum([(dp-dq)**2 for dp,dq in zip(point,qPoint)])**0.5 # compute the euclidean distance for each point to q
 if dist &lt; minDist or minDist &lt; 0: # if necessary, update minimum distance and index of the corresponding point
 minDist = dist
 minIdx = idx
»&gt; print 'Nearest point to q: ', points[minIdx]
Nearest point to q: [3, 4, 4]
»&gt; # # # Equivalent NumPy vectorization # # #
»&gt; import numpy as np
»&gt; points = np.array(9,2,8],[4,7,2],[3,4,4],[5,6,9],[5,0,7],[8,2,7],[0,3,2],[7,3,0],[6,1,1],[2,9,6)
»&gt; qPoint = np.array([4,5,3])
»&gt; minIdx = np.argmin(np.linalg.norm(points-qPoint,axis=1)) # compute all euclidean distances at once and return the index of the smallest one
»&gt; print 'Nearest point to q: ', points[minIdx]
Nearest point to q: [3 4 4]