<jupyter_start><jupyter_text>Numpy_Tutorial> 넘파이 튜토리얼- toc: true - badges: true- comments: true- categories: [Python]- image: images/ --- - https://docs.scipy.org/doc/numpy-1.15.0/user/quickstart.html- https://github.com/ndb796/Python-Data-Analysis-and-Image-Processing-Tutorial- https://www.machinelearningplus.com/python/numpy-tutorial-part1-array-python-examples/- http://web.mit.edu/dvp/Public/numpybook.pdf- https://docs.scipy.org/doc/numpy-1.11.0/numpy-user-1.11.0.pdf --- The Basic<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>- NumPy’s main object = the homogeneous multidimensional array.- In NumPy dimensions are called axes. ```1차원[1, 2, 1]2차원[[ 1., 0., 0.], [ 0., 1., 2.]]```  - NumPy’s array class is called ndarray.- numpy.array is not the same as the Standard Python Library class array.    - 파이썬의 array는 1차원만을 다룬다. - The more important attributes of an ndarray object are:    - `ndarray.ndim` : 넘파이의 차원    - `ndarray.shape` : 넘파이의 모양 (n, m)    - `ndarray.size` : This is equal to the product of the elements of shape. (n*m)    - `ndarray.dtype` : array안 요소의 타입    - `ndarray.itemsize` : array안 요소들의 사이즈(bytes)    - `ndarray.data` : Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.         An example```python>>> import numpy as np>>> a = np.arange(15).reshape(3, 5)>>> aarray([[ 0,  1,  2,  3,  4],       [ 5,  6,  7,  8,  9],       [10, 11, 12, 13, 14]])>>> a.shape(3, 5)>>> a.ndim2>>> a.dtype.name'int64'>>> a.itemsize8>>> a.size15>>> type(a)>>> b = np.array([6, 7, 8])>>> barray([6, 7, 8])>>> type(b)```  Array Creation you can create an array from a regular Python list or tuple using the array function.```python>>> a = np.array([2,3,4])>>> aarray([2, 3, 4])``` 2차원 array```python>>> b = np.array([(1.5,2,3), (4,5,6)])>>> barray([[ 1.5,  2. ,  3. ],       [ 4. ,  5. ,  6. ]])``` The function `zeros` creates an array full of zeros, ```python>>> np.zeros( (3,4) )array([[ 0.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  0.]])>>> np.ones( (2,3,4), dtype=np.int16 )                 dtype can also be specifiedarray([[[ 1, 1, 1, 1],        [ 1, 1, 1, 1],        [ 1, 1, 1, 1]],       [[ 1, 1, 1, 1],        [ 1, 1, 1, 1],        [ 1, 1, 1, 1]]], dtype=int16)``` To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.```python>>> np.arange( 10, 30, 5 )array([10, 15, 20, 25])>>> np.arange( 0, 2, 0.3 )                  it accepts float argumentsarray([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])>>> np.linspace( 0, 2, 9 )                  9 numbers from 0 to 2array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])``` other method```array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, numpy.random.rand, numpy.random.randn, fromfunction, fromfile```  Printing Arrays 1차원은 행으로, 2,3 차원은 행렬로 표현된다.```python>>> a = np.arange(6)                          1d array>>> print(a)[0 1 2 3 4 5]>>> b = np.arange(12).reshape(4,3)            2d array>>> print(b)[[ 0  1  2] [ 3  4  5] [ 6  7  8] [ 9 10 11]]>>> c = np.arange(24).reshape(2,3,4)          3d array>>> print(c)[[[ 0  1  2  3]  [ 4  5  6  7]  [ 8  9 10 11]] [[12 13 14 15]  [16 17 18 19]  [20 21 22 23]]]```  Basic Operations ```python>>> a = np.array( [20,30,40,50] )>>> b = np.arange( 4 )>>> barray([0, 1, 2, 3])>>> c = a-b>>> carray([20, 29, 38, 47])>>> b**2array([0, 1, 4, 9])>>> 10*np.sin(a)array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])>>> a<35array([ True, True, False, False])``` *는 각각의 요소끼리 곱하고 @, dot은 행렬곱을 한다.```python>>> A = np.array( [[1,1],...             [0,1]] )>>> B = np.array( [[2,0],...             [3,4]] )>>> A * B                        elementwise productarray([[2, 0],       [0, 4]])>>> A @ B                        matrix productarray([[5, 4],       [3, 4]])>>> A.dot(B)                     another matrix productarray([[5, 4],       [3, 4]])``` += , *= 연산```python>>> a = np.ones((2,3), dtype=int)>>> b = np.random.random((2,3))>>> a *= 3>>> aarray([[3, 3, 3],       [3, 3, 3]])>>> b += a>>> barray([[ 3.417022  ,  3.72032449,  3.00011437],       [ 3.30233257,  3.14675589,  3.09233859]])>>> a += b                   b is not automatically converted to integer typeTraceback (most recent call last):  ...TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int64') with casting rule 'same_kind'```  전체합, 최솟값, 최댓값 등을 구할때는 ndarray 자체 메소드를 사용하여야한다.```python>>> a = np.random.random((2,3))>>> aarray([[ 0.18626021,  0.34556073,  0.39676747],       [ 0.53881673,  0.41919451,  0.6852195 ]])>>> a.sum()2.5718191614547998>>> a.min()0.1862602113776709>>> a.max()0.6852195003967595``` 연산들은 전체를 기준으로 하지만, axis를 추가해주면 각 행이나 열마다 함수를 적용할 수 있다.```python>>> barray([[ 0,  1,  2,  3],       [ 4,  5,  6,  7],       [ 8,  9, 10, 11]])>>>>>> b.sum(axis=0)                             sum of each columnarray([12, 15, 18, 21])>>>>>> b.min(axis=1)                             min of each rowarray([0, 4, 8])>>>>>> b.cumsum(axis=1)                          cumulative sum along each row 누적합array([[ 0,  1,  3,  6],       [ 4,  9, 15, 22],       [ 8, 17, 27, 38]])``` - 서로 다른 형태의 Numpy 연산<jupyter_code>array1 = np.arange(4).reshape(2,2)
array2 = np.arange(2)
array3 = array1 + array2
print(array3)<jupyter_output>[[0 2]
 [2 4]]<jupyter_text>- 브로드캐스트  - 마스킹 연산  Numpy 원소의 값을 조건에 따라 바꿀 때 다음과 같이한다.반복문을 이용할 때보다 매우 빠르게 동작한다.대체로 이미지 처리(Image Processing)에서 자주 활용된다.<jupyter_code>array1 = np.arange(16).reshape(4,4)
print(array1)

array2 = array1 < 10
print(array2)

array1[array2] = 100
print(array1)<jupyter_output>[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]
[[ True  True  True  True]
 [ True  True  True  True]
 [ True  True False False]
 [False False False False]]
[[100 100 100 100]
 [100 100 100 100]
 [100 100  10  11]
 [ 12  13  14  15]]<jupyter_text>Universal Functions- NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”(ufunc).```python>>> B = np.arange(3)>>> Barray([0, 1, 2])>>> np.exp(B)array([ 1.        ,  2.71828183,  7.3890561 ])>>> np.sqrt(B)array([ 0.        ,  1.        ,  1.41421356])>>> C = np.array([2., -1., 4.])>>> np.add(B, C)array([ 2.,  0.,  6.])``` other method```all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor, inner, inv, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var, vdot, vectorize, where```  Indexing, Slicing and Iteration - 1차원 array는 파이썬의 리스트와 같이 인덱싱, 슬라이싱을 할 수 있다, - Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas        반점을 사이에 둔 튜플 필요```python>>> def f(x,y):...     return 10*x+y...>>> b = np.fromfunction(f,(5,4),dtype=int)>>> barray([[ 0,  1,  2,  3],       [10, 11, 12, 13],       [20, 21, 22, 23],       [30, 31, 32, 33],       [40, 41, 42, 43]])>>> b[2,3]23>>> b[0:5, 1]                        each row in the second column of barray([ 1, 11, 21, 31, 41])>>> b[ : ,1]                         equivalent to the previous examplearray([ 1, 11, 21, 31, 41])>>> b[1:3, : ]                       each column in the second and third row of barray([[10, 11, 12, 13],[20, 21, 22, 23]])``` `Iterating` over multidimensional arrays is done with respect to the first axis:```python>>> for row in b:            print(row)[0 1 2 3][10 11 12 13][20 21 22 23][30 31 32 33][40 41 42 43]```However, if one wants to perform an operation on each element in the array, one can use the flat attribute which is an iterator over all the elements of the array:```python>>> for element in b.flat:            print(element)012310111213202122233031323340414243``` other method```Indexing, Indexing (reference), newaxis, ndenumerate, indices``` --- Shape Manipulation  Changing the shape of an array - 모양을 바꾸는 3가지 방법 `ravel()`, `reshape()`, `T`. 하지만 원래의 array는 바뀌지않는다! ```python>>> a.shape(3, 4)>>> a.ravel()   returns the array, flattenedarray([ 2.,  8.,  0.,  6.,  4.,  5.,  1.,  1.,  8.,  9.,  3.,  6.])>>> a.reshape(6,2)   returns the array with a modified shapearray([[ 2.,  8.],       [ 0.,  6.],       [ 4.,  5.],       [ 1.,  1.],       [ 8.,  9.],       [ 3.,  6.]])>>> a.T   returns the array, transposedarray([[ 2.,  4.,  8.],       [ 8.,  5.,  9.],       [ 0.,  1.,  3.],       [ 6.,  1.,  6.]])>>> a.T.shape(4, 3)>>> a.shape(3, 4)``` - 이번엔 원래의 array를 바꾸는 메소드 `resize````python>>> aarray([[ 2.,  8.,  0.,  6.],       [ 4.,  5.,  1.,  1.],       [ 8.,  9.,  3.,  6.]])>>> a.resize((2,6))>>> aarray([[ 2.,  8.,  0.,  6.,  4.,  5.],       [ 1.,  1.,  8.,  9.,  3.,  6.]])```  Stacking together different arrays - 행이나 열로 여러개의 array를 쌓을 수 있다.```python>>> a = np.floor(10*np.random.random((2,2)))>>> aarray([[ 8.,  8.],       [ 0.,  0.]])>>> b = np.floor(10*np.random.random((2,2)))>>> barray([[ 1.,  8.],       [ 0.,  4.]])>>> np.vstack((a,b))array([[ 8.,  8.],       [ 0.,  0.],       [ 1.,  8.],       [ 0.,  4.]])>>> np.hstack((a,b))array([[ 8.,  8.,  1.,  8.],       [ 0.,  0.,  0.,  4.]])``` - 가로축으로 합치고 싶을 때<jupyter_code>array1 = np.array([1,2,3])
array2 = np.array([4,5,6])
array3 = np.concatenate([array1, array2])

print(array3.shape)
print(array3)<jupyter_output>(6,)
[1 2 3 4 5 6]<jupyter_text>- 세로축으로 합치고 싶을 때<jupyter_code>array1 = np.arange(4).reshape(1,4)
array2 = np.arange(8).reshape(2,4)

print(array1)
print(array2)

array3 = np.concatenate([array1, array2], axis = 0) # axis = 0 이 추가됨!
print(array3)<jupyter_output>[[0 1 2 3]]
[[0 1 2 3]
 [4 5 6 7]]
[[0 1 2 3]
 [0 1 2 3]
 [4 5 6 7]]<jupyter_text>other method```hstack, vstack, column_stack, concatenate, c_, r_```  Splitting one array into several smaller ones - 각 행을 원하는 갯수로 나누어준다.```python>>> a = np.floor(10*np.random.random((2,12)))>>> aarray([[ 9.,  5.,  6.,  3.,  6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],       [ 1.,  4.,  9.,  2.,  2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])>>> np.hsplit(a,3)    Split a into 3[array([[ 9.,  5.,  6.,  3.],       [ 1.,  4.,  9.,  2.]]), array([[ 6.,  8.,  0.,  7.],       [ 2.,  1.,  0.,  6.]]), array([[ 9.,  7.,  2.,  7.],       [ 2.,  2.,  4.,  0.]])]``` - `split` 이용<jupyter_code>import numpy as np
array = np.arange(8).reshape(2, 4)
left, right = np.split(array, [2], axis=1) # array에서 3번째열을 기준으로 나눈다.
print(left.shape)
print(right.shape)
print(array)
print(left)<jupyter_output>(2, 2)
(2, 2)
[[0 1 2 3]
 [4 5 6 7]]
[[0 1]
 [4 5]]
{'fraction_non_alphanumeric': 0.21365408386421983, 'fraction_numerical': 0.10411714367214986, 'mean_word_length': 2.6996130847696094, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 297, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '94035', 'license': 'Apache-2.0', 'n_tokens_mistral': 5111, 'n_tokens_neox': 4673, 'n_words': 1288, 'path': '_notebooks/2021-12-25-Numpy.ipynb', 'repo_name': 'star77sa/TIL-Blog', 'size': '10517'}