배열 요소 삭제 함수(Delete Function)
numpy.delete(arr, obj, axis=None)
지정한 Index의 요소를 삭제시켜주는 함수입니다.
▪Parameters
‣ arr : 입력 배열,
‣ obj : 값 추가 전 인덱스를 지정하는 개체 삽입(slice or Index),
‣ axis : 축 설정(선택 사항), 계산이 진행되는 축 설정
‣ out : ndarray, 삭제된 배열 출력 값(axis 지정이 없다면 1D Array 형태로 출력)
예제(Example)
<Example 01>
import numpy as np
a = np.arange(1, 10, 1)
print('Initial np values : ', a)
a = np.delete(a, 0)
print('1st delete data : ', a)
a = np.delete(a, 1)
print('2nd delete data : ', a)
a = np.delete(a, 2)
print('3rd delete data : ', a)
a = np.delete(a, 3)
print('4th delete data : ', a)
a = np.delete(a, 4)
print('5th delete data : ', a)
더보기
nitial np values : [1 2 3 4 5 6 7 8 9]
1st delete data : [2 3 4 5 6 7 8 9]
2nd delete data : [2 4 5 6 7 8 9]
3rd delete data : [2 4 6 7 8 9]
4th delete data : [2 4 6 8 9]
5th delete data : [2 4 6 8]
<Example 02>
import numpy as np
a = np.array([[1, 2, 3],[4, 5, 6], [7, 8, 9]])
a = np.delete(a, 1, 0)
print('delete data from Axis = 0 : ')
print(a)
a = np.delete(a, 1, 1)
print('delete data from Axis = 1 : ')
print(a)
더보기
delete data from Axis = 0 :
[[1 2 3]
[7 8 9]]
delete data from Axis = 1 :
[[1 3]
[7 9]]
<Example 03>
import numpy as np
a = np.array([[1, 2, 3],[4, 5, 6], [7, 8, 9]])
a = np.delete(a, np.s_[::2], 0)
print('delete data from Axis = 0 : ')
print(a)
a = np.array([[1, 2, 3],[4, 5, 6], [7, 8, 9]])
a = np.delete(a, np.s_[::2], 1)
print('delete data from Axis = 1 : ')
print(a)
더보기
delete data from Axis = 0 :
[[4 5 6]]
delete data from Axis = 1 :
[[2]
[5]
[8]]
Numpy 함수 모음
※ 이 글이 도움이 되었다면 "👆🏻구독"과 "🤍공감" 버튼을 클릭해주세요. 클릭 한번이 글 쓰는데 큰 힘이 됩니다.