• 회원가입
  • 로그인
  • 구글아이디로 로그인

[numpy] PY - NumPy Array Copy vs View (배열 복사 vs 뷰) ★

2954  

목차

  1. 배열 (Copy / View) 차이점
  2. 배열 Copy
  3. 배열 view
  4. 배열의 데이터 개별소유 여부 확인

 

배열 (Copy / View) 차이점


copy()

원본과 복사본이 별개

즉, 서로 영향 안 미침. 

즉, 복사본 배열이 데이터 개별 소유 O.

 

view()

원본과 복사본 상호 영향

즉, 서로 영향 미침. 

즉, 복사본 배열이 데이터 개별 소유 X.

 

 

배열 Copy

※ 원본 변경 시, 복사본에 영향 안 미침.

 

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.copy()

arr[0] = 34

print(arr) # [34  2  3  4  5]

print(x) # [1 2 3 4 5]

 


※ 복사본 변경 시, 원본에 영향 안 미침.

 

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.copy()

x[0] = 34

print(arr) # [1  2  3  4  5]

print(x) # [34 2 3 4 5]

 

 

배열 view

※ 원본 변화가 복사본에 영향 미침.

 

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.view()

arr[0] = 34

print(arr) # [34  2  3  4  5]

print(x) # [34  2  3  4  5]

 


※ 복사본 변화가 원본에 영향 미침.

 

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.view()

x[0] = 34

print(arr) # [34  2  3  4  5]

print(x) # [34  2  3  4  5] 

 

 

배열의 데이터 개별소유 여부 확인

 

NumPy 배열 객체의 base 속성 이용.

copy()로 생성된 배열 (= 데이터 개별 소유 O) 경우, None 반환.

view()로 생성된 배열 (= 데이터 개별 소유 X) 경우, 원본 배열 반환.

 


[예제]

 

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.copy()

y = arr.view()

print(x.base) # None

print(y.base) # [1 2 3 4 5]

 



분류 제목
module Python - math.erf() 메서드 -
module Python - math.erfc() 메서드 -
module Python - math.exp() 메서드 -
module Python - math.expm1() 메서드 -
module Python - math.fabs() 메서드 -
module Python - math.factorial() 메서드 -
module Python - math.floor() 메서드 ★ - 바닥 반올림 (= 하위 정수로 반올림. = floor메…
module Python - math.fmod() 메서드 -
module Python - math.frexp() 메서드 -
module Python - math.fsum() 메서드 -
module Python - math.gamma() 메서드 -
module Python - math.gcd() 메서드 -
module Python - math.hypot() 메서드 -
module Python - math.isclose() 메서드 -
module Python - math.isfinite() 메서드 - 숫자 유한 여부 체크 (= math.isfinite메…
module Python - math.isinf() 메서드 -
module Python - math.isnan() 메서드 ★ - NaN 여부 체크. (= isnan메서드 = 이즈난메서…
module Python - math.isqrt() 메서드 -
module Python - math.ldexp() 메서드 -
module Python - math.lgamma() 메서드 -
20/24
목록
찾아주셔서 감사합니다. Since 2012