<jupyter_start><jupyter_text>**Basic Indexing and Slicing**  Indexing and Slicing Vector ndarrays **indexing**<jupyter_code>import numpy as np

a = np.arange(10)
print(f"ndarray: \n{a}\n")

print("a[0]: ", a[0])
print("a[2]: ", a[2])

print("a[-1]: ", a[-1])
print("a[-2]: ", a[-2])
import numpy as np

a = np.arange(5)
print(f"ndarray: \n{a}\n")

for data in a:
    print(data)<jupyter_output>ndarray: 
[0 1 2 3 4]

0
1
2
3
4<jupyter_text>**slicing**<jupyter_code>import numpy as np

a = np.arange(10)
print(f"ndarray: \n{a}\n")

print("a[0:3]: ", a[0:3])
print("a[3:-1]: ", a[3:-1])
print(f"ndarray: \n{a}\n")

print("a[:4]: ", a[:4])
print("a[-2:]: ", a[-2:])
print(f"ndarray: \n{a}\n")

print("a[2:7:2]: ", a[2:7:2])
print("a[7:2:-1]: ", a[7:2:-1]) # 반대방향
import numpy as np

a = np.arange(10)
indices = np.array([0, 3, 6, -1])
print(f"ndarray: \n{a}\n")

print(a[[0, 3, 6, -1]])
print(a[indices])
print(a[a%3==0])<jupyter_output>ndarray: 
[0 1 2 3 4 5 6 7 8 9]

[0 3 6 9]
[0 3 6 9]
[0 3 6 9]<jupyter_text>Element Assignment with Slicing<jupyter_code>a = list(range(1,11))
print(a)

for data_idx in range(5):
    a[data_idx] = 0
print(a)
# using numpy without for
a = np.arange(1,11)
print(a)

a[:5] = 0
print(a)
a = np.arange(1,11)
print(a)

a[::2] = 200
print(a)

a[5:-1:3] = 300
print(a)<jupyter_output>[ 1  2  3  4  5  6  7  8  9 10]
[200   2 200   4 200   6 200   8 200  10]
[200   2 200   4 200 300 200   8 300  10]<jupyter_text>Indexing and Slicing Matrix ndarrays<jupyter_code>a = [[0,1,2],[3,4,5],[6,7,8]]
print(a[0])
print(a[1])
print(a[2])
print(a[0][0], a[0][1], a[0][2])
print(a[1][0], a[1][1], a[1][2])
print(a[2][0], a[2][1], a[2][2])
# using numpy
import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

print("a[0]: ",a[0])
print("a[1]: ",a[1])
print("a[2]: ",a[2])
# for 문에서 작동할 경우
a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

for data in a:
    print(data)
import numpy as np

a = np.arange(9).reshape((3,3))
print(f"ndarray: \n{a}\n")

print(a[0,0], a[0,1], a[0,2])
print(a[1,0], a[1,1], a[1,2])
print(a[2,0], a[2,1], a[2,2])
import numpy as np

a = np.arange(12).reshape((3,-1))
print(f"ndarray: \n{a}\n")

print("a[0,1:] :", a[0, 1:])
print("a[1,:3] :", a[0, :3])
print("a[2,1:3] :", a[2, 1:3])
import numpy as np

a = np.arange(12).reshape((4,-1))
print(f"ndarray: \n{a}\n")

print("a[1:,0] :", a[1:,0])
print("a[:3,1] :", a[:3,1])
print("a[1:3,2] :", a[1:3,2])
import numpy as np

a = np.arange(16).reshape((4,-1))
print(f"ndarray: \n{a}\n")

print("a[1:3,1:3] :\n", a[1:3,1:3])
print("a[:2,:3] :\n", a[:2,:3])
print("a[1:,2:] :\n", a[1:,2:])
print("a[2:,-2:] :\n", a[2:,-2:])    
import numpy as np

image = np.arange(9).reshape((3,3))
print(f"ndarray: \n{image}\n")

# row는 그대로, column은 반대방향
horizontal_flip = image[:, ::-1]
print(f"horizontal_flip: \n{horizontal_flip}\n")

# row가 반대방향
vertical_flip = image[:, ::-1]
print(f"vertical_flip: \n{vertical_flip}\n")

# 180도 회전
rotation = image[::-1, ::-1]
print(f"rotation: \n{rotation}\n")
# '...' 전부 다 가져오기
import numpy as np

a = np.arange(16).reshape((4,4))
print(f"ndarray: \n{a}\n")

print("a[0, :] : \n", a[0, :] )

print("a[0, ...] : \n", a[0, ...])<jupyter_output>ndarray: 
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

a[0, :] : 
 [0 1 2 3]
a[0, ...] : 
 [0 1 2 3]<jupyter_text>Indexing and Slicing 3rd Order Tensor ndarrays<jupyter_code>import numpy as np

a = np.arange(3*3*3).reshape((3,3,3))
print(f"ndarray: \n{a}\n")

print("a[0] : \n", a[0])
import numpy as np

a = np.arange(3*3*3).reshape((3,3,3))
print(f"ndarray: \n{a}\n")

print("a[0,0] : \n", a[0,0])
import numpy as np

a = np.arange(3*3*3).reshape((3,3,3))
print(f"ndarray: \n{a}\n")

print("a[0, :2, :2] : \n", a[0, :2, :2], '\n')
print("a[1, :2, :] : \n", a[1, :2, :],'\n')
print("a[2, 1:, 1:] : \n", a[2, 1:, 1:])
import numpy as np

images = np.random.normal(size=(32, 100, 200))
print("image set: ", images.shape)

image0 = images[0, : , :]
print(image0.shape)

image0 = images[0, ...]
print(image0.shape)

col0 = images[:, :, 0]
print(col0.shape)

col0 = images[..., 0]
print(col0.shape)

image0_col0 = images[0, ... , 0]
print(image0_col0.shape)
import numpy as np

a = np.arange(27).reshape((3,3,3))
print(f"ndarray: \n{a}\n")

print(f"a[:, :2, :] : \n{a[:, :2, :]}\n")
print(f"a[..., 0] : \n{a[..., 0]}\n")
print(f"a[1:, 1:, 1:] : \n{a[1:, 1:, 1:]}\n")
import numpy as np

image = np.random.normal(size=(3, 500, 300))
image_r = image[0]
image_g = image[1]
image_b = image[2]

print(image_r.shape, image_g.shape, image_b.shape)
# unpacking
image_r, image_g, image_b = image
print(image_r.shape, image_g.shape, image_b.shape)
import numpy as np

image = np.random.normal(size=(3, 500, 300))

top_left = image[:, :100, :100]
top_right = image[:, :100, -100:]
bottom_left = image[:, -100:, :100]
bottom_right = image[:, -100:, -100:]

print(top_left.shape, top_right.shape)
print(bottom_left.shape,bottom_right.shape )<jupyter_output>(3, 100, 100) (3, 100, 100)
(3, 100, 100) (3, 100, 100)<jupyter_text>**Indexing with int ndarrays**  Indexing and Slicing Vector ndarrays<jupyter_code>import numpy as np

a = np.arange(10)
print(f"ndarray: \n{a}\n")

indices = np.array([0, 3, 4])
print(a[indices])<jupyter_output>ndarray: 
[0 1 2 3 4 5 6 7 8 9]

[0 3 4]<jupyter_text>중복된 index도 출력 가능<jupyter_code>import numpy as np

a = np.arange(10)
print(f"ndarray: \n{a}\n")

indices = np.array([0, 0, 5, 5])
print(a[indices])<jupyter_output>ndarray: 
[0 1 2 3 4 5 6 7 8 9]

[0 0 5 5]<jupyter_text>![aa.PNG](attachment:aa.PNG)<jupyter_code>import numpy as np

a = np.arange(10)
print(f"ndarray: \n{a}\n")

indices = np.array([[1, 2, 3], [5, 8, 9]])
print(a[indices])
import numpy as np

a = np.random.randint(0, 20, (10, ))
print(f"ndarray: \n{a}\n")

indices = np.random.randint(0, 10, size=(2, 3, 4))
print(f"indices : \n{indices}\n")

print(f"a[indices]: \n{a[indices]}")<jupyter_output>ndarray: 
[ 6  5  2 19  7  0 13 14  6  2]

indices : 
[[[8 2 9 2]
  [5 8 8 3]
  [6 3 6 1]]

 [[5 4 9 4]
  [5 3 3 6]
  [9 6 0 7]]]

a[indices]: 
[[[ 6  2  2  2]
  [ 0  6  6 19]
  [13 19 13  5]]

 [[ 0  7  2  7]
  [ 0 19 19 13]
  [ 2 13  6 14]]]<jupyter_text>![bb.PNG](attachment:bb.PNG)<jupyter_code>import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

indices = np.array([0,2])
print(f"indices : \n{indices}\n")

print(f"a[indices]: \n{a[indices]}")
import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

indices = np.array([0,0,1,1,2,2])
print(f"indices : \n{indices}\n")

print(f"a[indices]: \n{a[indices]}")
import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

indices = np.array([[0,1,2],[-3,-2,-1]])
print(f"indices : \n{indices}\n")

print(f"a[indices]: \n{a[indices]}")
import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

indices0, indices1 = np.array([0]), np.array([0])
print(a[indices0, indices1])

indices0, indices1 = np.array([1]), np.array([2])
print(a[indices0, indices1])

indices0, indices1 = np.array([-1]), np.array([0])
print(a[indices0, indices1])
import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

indices0, indices1 = np.array([0, 1, 2]), np.array([1, 2, 3])

print("paired indices")
for idx0, idx1 in zip(indices0, indices1):
    print(f"({idx0}, {idx1})\n")

print("a[indices0, indices1]: \n" ,a[indices0, indices1])
import numpy as np

a = np.arange(12).reshape((3,4))
print(f"ndarray: \n{a}\n")

indices0 = np.array([[0, 1, 2], [0, 1, 2]])
indices1 = np.array([[0, 1, 2], [1, 2, 3]])

print("paired indices")
for row_indices0, row_indices1 in zip(indices0, indices1):
    for idx0, idx1 in zip(row_indices0, row_indices1):
        print(f"({idx0}, {idx1})", end=' ')
    print()

print("a[indices0, indices1]: \n" ,a[indices0, indices1])<jupyter_output>ndarray: 
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

paired indices
(0, 0) (1, 1) (2, 2) 
(0, 1) (1, 2) (2, 3) 
a[indices0, indices1]: 
 [[ 0  5 10]
 [ 1  6 11]]<jupyter_text>**Indexing with bool ndarrays**  Indexing and Slicing Vector ndarrays<jupyter_code>import numpy as np

a = np.arange(5)
print(f"ndarray: \n{a}\n")

# true 뽑는다 False 안뽑는다
b_indices = np.array([True, False, True, False, True])
print(f"b_indices : \n{b_indices}\n")

print(f"a[b_indices]: \n{a[b_indices]}")
import numpy as np

a = np.random.randint(0, 20 , (10,))
print(f"ndarray: \n{a}\n")

b_indices = (a % 2 == 0)
print(f"b_indices : \n{b_indices}\n")

print(f"a[b_indices]: \n{a[b_indices]}")<jupyter_output>ndarray: 
[11  0  6  2 14  4  9 11  4  6]

b_indices : 
[False  True  True  True  True  True False False  True  True]

a[b_indices]: 
[ 0  6  2 14  4  4  6]<jupyter_text>Indexing and Slicing Matrix ndarrays<jupyter_code>import numpy as np

a = np.random.randint(0, 20 , (2,2))
print(f"ndarray: \n{a}\n")

b_indices = np.array([[True, False],
                     [False, True]])

print(f"b_indices : \n{b_indices}\n")

print(f"a[b_indices]: \n{a[b_indices]}")
import numpy as np

a = np.random.randint(0, 20 , (3 ,4))
print(f"ndarray: \n{a}\n")

b_indices = (a > 10)

print(f"b_indices : \n{b_indices}\n")

print(f"a[b_indices]: \n{a[b_indices]}")<jupyter_output>ndarray: 
[[ 3 11 15  7]
 [19  8 18 17]
 [ 0 12 11  0]]

b_indices : 
[[False  True  True False]
 [ True False  True  True]
 [False  True  True False]]

a[b_indices]: 
[11 15 19 18 17 12 11]<jupyter_text>**Extracting Indices**<jupyter_code>import numpy as np

a = np.array([True, False, True, False])

nonzero = np.nonzero(a)
where = np.where(a)

print(f"a: \n{a}\n")
print(f"nonzero: \n{nonzero}")
print(f"where: \n{where}")
import numpy as np

a = np.array([[True, False], [True, False]])

nonzero = np.nonzero(a)
where = np.where(a)

print(f"a: \n{a}\n")
print(f"nonzero: \n{nonzero}")
print(f"where: \n{where}")
import numpy as np

a = np.array([[[True, False, True], 
              [True, False, False]],
             
             [[True, False, True], 
              [True, False, False]]])

nonzero = np.nonzero(a)
where = np.where(a)

print(f"a: \n{a}\n")
print(f"nonzero: \n{nonzero}")
print(f"where: \n{where}")<jupyter_output>a: 
[[[ True False  True]
  [ True False False]]

 [[ True False  True]
  [ True False False]]]

nonzero: 
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 0, 1, 0, 0, 1], dtype=int64), array([0, 2, 0, 0, 2, 0], dtype=int64))
where: 
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 0, 1, 0, 0, 1], dtype=int64), array([0, 2, 0, 0, 2, 0], dtype=int64))<jupyter_text>nonzero, where은 0이 아닌 값의 index만 돌려주는 것index가 boolean의 경우에는 true인 값 index만 돌려주는 것<jupyter_code>import numpy as np

a = np.random.randint(-2, 3, size=(3,3))

u_nonzero = a[np.nonzero(a)]
u_where = a[np.where(a)]
u_bool = a[a != 0]

print(f"a: \n{a}\n")
print(f"using nonzero: \n{u_nonzero}")
print(f"u_where: \n{u_where}")
print(f"u_bool: \n{u_bool}")
import numpy as np

a = np.random.randint(-2, 3, size=(3,3))

u_nonzero = a[np.nonzero(a > 0)] # np.nonzero -> (3,3)
u_where = a[np.where(a > 0)]
u_bool = a[a > 0]

print(f"a: \n{a}\n")
print(f"using nonzero: \n{u_nonzero}")
print(f"u_where: \n{u_where}")
print(f"u_bool: \n{u_bool}")<jupyter_output>a: 
[[ 0 -1  0]
 [-1 -1 -1]
 [ 2 -2  0]]

using nonzero: 
[2]
u_where: 
[2]
u_bool: 
[2]
{'fraction_non_alphanumeric': 0.23735616804923734, 'fraction_numerical': 0.08018909999108019, 'mean_word_length': 4.246607393542349, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '149758', 'license': 'MIT', 'n_tokens_mistral': 5863, 'n_tokens_neox': 5494, 'n_words': 1221, 'path': 'Python Practice/Numpy Study/Indexing and Slicing ndarrays.ipynb', 'repo_name': 'kimbyeolhee/TIL', 'size': '11211'}