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

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

2959  

목차

  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 - statistics.mode() 메서드 ★ - 최빈값 반환. (= mode메서드 = 모드)
module Python - statistics.pstdev() 메서드 -
module Python - statistics.stdev() 메서드 -
module Python - statistics.pvariance() 메서드 -
module Python - statistics.variance() 메서드 -
module Python - math 모듈 메서드・상수 종류 (※ 수학 모듈)
module Python - math.acos() 메서드 -
module Python - math.acosh() 메서드 -
module Python - math.asin() 메서드 -
module Python - math.asinh() 메서드 -
module Python - math.atan() 메서드 -
module Python - math.atan2() 메서드 -
module Python - math.atanh() 메서드 -
module Python - math.ceil() 메서드 ★ - 천장 반올림 (= 상위 정수로 반올림. = ceil메서드…
module Python - math.comb() 메서드 -
module Python - math.copysign() 메서드 -
module Python - math.cos() 메서드 -
module Python - math.cosh() 메서드 -
module Python - math.degrees() 메서드 -
module Python - math.dist() 메서드 -
19/24
목록
찾아주셔서 감사합니다. Since 2012