커뮤니티
Python
제목:    Numpy
  2367   김윤중
  • Numpy
    • Numerical Python
    • import numpy as np
  • 다차원배열(n(multi)-dimensional array)
    • http://rfriend.tistory.com/283?category=675917
    • 빠른 처리속도
    • np.array() -> ndarray(multi-dimensional array)
       
    • np.asarray()
    • np.asfarray()
    • np.asarray_chkfinite()
       
    • np.zeros()
    • np.ones()
    • np.empty()
    • np.zeros_like()
    • np.ones_like()
    • np.empty_like()
       
    • np.arrange()
    • np.identity()
    • np.eye()
    • NumPy의 array() 로 ndarray
      • import numpy as np
      • arr1=np.array([1, 2, 3, 4, 5]) # making a array
    • list 객체를 먼저 만들고 np.array() 를 사용해서 배열로 변환해도 됩니다.
      • data_list = [6, 7, 8, 9, 10] # making a list
      • arr2 = np.array(data_list) # converting a list into an array
    • 같은 길이의 여러개의 리스트(lists)를 가지고 있는 리스트(a list)도 np.array()를 사용해서 배열(array)로 변환할 수 있습니다.
      • data_lists = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] # a list of equal-length lists
      • arr12 = np.array(data_lists) #converting a list of equal-length lists into a multi-dimensional array
      • arr12
        array([[ 1,  2,  3,  4,  5],
                 [ 6,  7,  8,  9, 10]])
    • ndarray obj.shape 함수 : ndarray의 차원 원소 수 점검
      • arr12.shape  # (2,5) a tuple indicating the size of each dimension
        • arr12.shape[0]  # 2
        • arr12.shape[1]  # 5
        • arr12.dtype     # dtype('int32') 
    • list,ndarray 변환
      • plst=[1,2]           # python list
      • narr=np.asarray(plst)  #converting :  python  list => numpy.ndarray
      • narr_1=np.asarray(nlst)  # narr is narr_1 => true 복사하지 않는다.,  동일한 데이형
      • narr_2=np.array(nlst)  # narr is narr_1 => False 복사한다. 동일해도 복사
      • np.asarray(narr, dtype=np.int32) is narr #True  Not copyed(Shllow copyed)
        # narr은 int32형이다.
      • np.asarray(narr, dtype=np.int64) is narr #False copyed(Deep copyed) 다른형
      • narr_f=np.asfrray(narr) # == np.asarray(narr, dtype=np.float64)
      • np.asarray_chkfinite(plst, dtype=float)
        [1., 3.] #원소에 결함원소를 점검 없으면 정상변환
      • np.asarray_chkfinite([1,2,np.nan], dtype=float) #있으면 오류메세지
        ValueError: array must not contain infs or NaNs
      • np.asarray_chkfinite([1,2,np.inf], dtype=float) #무한값이 있으면 오류메세지
        ValueError: array must not contain infs or NaNs
    • np.zeros(), np.ones(), np.empty() 함수는 괄호 안에 쓴 숫자 개수만큼의 '0', '1', '비어있는 배열', np.zeros((2, 5)), np.ones((5, 3)), np.empty((4, 3)) 처럼 괄호 안에 tuple을 넣어주면 다차원 배열
      • np.zeros(5) #array([ 0., 0., 0., 0., 0.])
      • np.ones(5) #array([ 1., 1., 1., 1., 1.])
      • np.empty(5) #array([ 0., 0., 0., 0., 0.]) empty values
      • np.zeros((2, 5))
        array([[ 0.,  0.,  0.,  0.,  0.],
                [ 0.,  0.,  0.,  0.,  0.]])
    • np.arange() 는 Python의 range() 함수처럼 0부터 괄호안의 숫자 만큼의 정수 배열 값을 '배열'로
      • f = np.arange(10) #array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])  cf. python rnage
    • np.zeros_like(), np.ones_like(), np.empty_like() 함수는 이미 있는 array와 동일한 모양과 데이터 형태를 유지
      • f25=f.reshape(2,5))
        ararray([[0, 1, 2, 3, 4],
                   [5, 6, 7, 8, 9]])
      • np.zeros_like(f_2_5)
        array([[0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0]])
      • np.ones_like(f_2_5)
        array([[1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1]])
      • np.empty_like(f_2_5)
        array([[0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0]])
    • 항등행렬(identity matrix), 단위행렬(unit matrix)  http://rfriend.tistory.com/141
      • np.identity(3)
        array([[ 1.,  0.,  0.,],
                [ 0.,  1.,  0.,], 
                [ 0.,  0.,  1.,]])
      • np.eye(3)
        array([[ 1.,  0.,  0.,],
                [ 0.,  1.,  0.,], 
                [ 0.,  0.,  1.,]])
    • data type변환
      • c=np.asarray(
  • Numpy Data Type  http://rfriend.tistory.com/285
    • Data Type
      • Boolean, Integer, Unsigned Integer, Float, Complex, String
    • Operations
      • Assigning Data Type
        • np.array[xx,xx], dtype=np.Type)
        • np.array([xx, xx], dtype=np.'Type Code')
      • Checking Data Type
        • object.dtype
        • x_float64
      • Converting Data Type
        • object.astype(np.Type)
    • Examples of a array [1.4, 2.6, 3.0 with data type pf float64:np.float64()
      • import numpy as np
      • # np.array[xx,xx],  np.array[xx,xx],np.float64), # np.array[xx,xx],np.'type code')
      • x_float64 = np.array([1.4, 2.6, 3.0]  
      • x_float64_1 = np.array([1.4, 2.6, 3.0, 4.9, 5.32], dtype=np.float64)
      • x_float64_2 = np.array([1.4, 2.6, 3.0, 4.9, 5.32], dtype=np.'f8'
      • x_float64.dtype   # Checking type
        dtype('float64')
      • x_float64                                   # checking coentent
         array([ 1.4 , 2.6 , 3. ])
      • x_int64=x_float64.astype(np.int64) # Convert flaot64 to int645 the decimal parts are truncated
      • x_int64_2 = np.int64(x_float64)
      • x_int64.dtype
        dtype('int64')
      • x_int64_2
         array([1, 2, 3], dtype=int64)
      • ......http://rfriend.tistory.com/285