NumPy Indexing and Selection
import numpy as np
arr = np.arange(0,11) #Creating sample array
arr #Show
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Bracket Indexing and Selection
대괄호 인덱싱 및 선택
# Get a value at an index
arr[8] # 8
# Get values in a range
arr[1:5] # array([1, 2, 3, 4])
#Get values in a range
arr[0:5] # array([0, 1, 2, 3, 4])
Broadcasting
Numpy arrays differ from a normal Python list because of their ability to broadcast:
# Setting a value with index range (Broadcasting)
arr[0:5]=100
arr # array([100, 100, 100, 100, 100, 5, 6, 7, 8, 9, 10])
# Reset array, we'll see why I had to reset in a moment
arr = np.arange(0,11)
arr # array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#Important notes on Slices
slice_of_arr = arr[0:6]
slice_of_arr # array([0, 1, 2, 3, 4, 5])
#Change Slice
slice_of_arr[:]=99
slice_of_arr # array([99, 99, 99, 99, 99, 99])
# Now note the changes also occur in our original array!
# 아래 배열에 적용 됩니다.
arr # array([99, 99, 99, 99, 99, 99, 6, 7, 8, 9, 10])
'''
Data is not copied, it's a view of the original array! This avoids memory problems!
배열은 복사되지 않고 원본 배열을 참조합니다! 이것은 메모리 문제를 방지합니다!
'''
# To get a copy, need to be explicit
# 배열을 복사하려면 명시적으로 지정해주어야 합니다.
arr_copy = arr.copy()
arr
Indexing a 2D array (matrices)
The general format is arr_2d[row][col] or arr_2d[row,col] .
I recommend usually using the comma notation for clarity.
arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
arr_2d
'''
array([[ 5, 10, 15],
[20, 25, 30],
[35, 40, 45]])
'''
#Indexing row
arr_2d[1]
# array([20, 25, 30])
arr_2d[1,0]
# 20
# 2D array slicing
# Shape (2,2) from top right corner
arr_2d[:2,1:]
'''
array([[10, 15],
[25, 30]])
'''
Fancy Indexing
Fancy indexing allows you to select entire rows or columns out of order.
Fancy indexing을 사용하면 전체 행 또는 열을 순서없이 선택할 수 있습니다.
#Set up matrix
arr2d = np.zeros((10,10))
#Length of array
arr_length = arr2d.shape[1]
#Set up array
for i in range(arr_length):
arr2d[i] = i
arr2d
'''
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],
[ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[ 5., 5., 5., 5., 5., 5., 5., 5., 5., 5.],
[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[ 7., 7., 7., 7., 7., 7., 7., 7., 7., 7.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.],
[ 9., 9., 9., 9., 9., 9., 9., 9., 9., 9.]])
'''
Fancy indexing allows the following
arr2d[[2,4,6,8]]
'''
array([[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[ 8., 8., 8., 8., 8., 8., 8., 8., 8., 8.]])
'''
#Allows in any order
arr2d[[6,4,2,7]]
'''
array([[ 6., 6., 6., 6., 6., 6., 6., 6., 6., 6.],
[ 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.],
[ 7., 7., 7., 7., 7., 7., 7., 7., 7., 7.]])
'''
Selection
Let's briefly go over how to use brackets for selection based off of comparison operators.
비교 연산자를 기반으로 선택에 대괄호를 사용하는 방법에 대해 간략하게 살펴 보겠습니다.
arr = np.arange(1,11)
arr # array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr > 4
# array([False, False, False, False, True, True, True, True, True, True], dtype=bool)
bool_arr = arr>4
bool_arr
# array([False, False, False, False, True, True, True, True, True, True], dtype=bool)
arr[bool_arr]
# array([ 5, 6, 7, 8, 9, 10])
arr[arr>2]
# array([ 3, 4, 5, 6, 7, 8, 9, 10])
x = 2
arr[arr>x]
# array([ 3, 4, 5, 6, 7, 8, 9, 10])
Great Job!
'Python > Numpy & Pandas' 카테고리의 다른 글
02-DataFrames (0) | 2020.11.30 |
---|---|
01-Introduction to Pandas, and Series (0) | 2020.11.30 |
03-Numpy Operations (0) | 2020.11.30 |
01-NumPy Arrays (0) | 2020.11.30 |
Numpy 실행시간 비교 (0) | 2020.11.30 |