<jupyter_start><jupyter_text>reshape으로 새로운 차원 생성  **Making New Dimension**  Using np.reshape<jupyter_code># reshape 은 원소 개수가 변하지 않는다.
# 차원 변형 가능
import numpy as np

a = np.arange(9)

b = a.reshape((1, 9))
c = a.reshape((9, 1))

print("a\n", a)
print("b\n", b)
print("c\n", c)
import numpy as np

a = np.arange(9)

b = a.reshape((1, 1, 9))
c = a.reshape((1, 9, 1))
d = a.reshape((9, 1, 1))

print(a)
print(b)
print(c)
print(d)

a = np.random.normal(size=(100, 200))

b = a.reshape((1, 100, 200))
c = a.reshape((100, 200, 1))

print(b.shape)
print(c.shape)<jupyter_output>(1, 100, 200)
(100, 200, 1)<jupyter_text>유지보수가 어렵다는 단점 존재<jupyter_code>a = (10, 20)
print(*a)
import numpy as np

a = np.random.normal(size=(100, 150))

print(a.shape)
print(*a.shape)
print((1, *a.shape))
print((*a.shape, 1))
import numpy as np

a = np.random.normal(size=(100, 200))

b = a.reshape((1, *a.shape))
c = a.reshape((*a.shape, 1))

print(a.shape)

print(b.shape)
print(c.shape)<jupyter_output>(100, 200)
(1, 100, 200)
(100, 200, 1)<jupyter_text>Using Slicing<jupyter_code>import numpy as np

a = np.arange(9)

row_vec1 = a[np.newaxis, :]  # np.newaxis= None
row_vec2 = a[None, :] # shape = (1, 9)

print(row_vec1.shape)
print(row_vec2.shape)
col_vec1 = a[:, np.newaxis]
col_vec2 = a[:, None]

print(col_vec1.shape)
print(col_vec2.shape)
import numpy as np

a = np.random.normal(size=(100, 200))

b = a[np.newaxis, ...]
c = a[... , np.newaxis]

print(b.shape)
print(c.shape)<jupyter_output>(1, 100, 200)
(100, 200, 1)<jupyter_text>Using expand_dims API<jupyter_code>import numpy as np

a = np.arange(9)

b = np.expand_dims(a, axis=0)
c = np.expand_dims(a, axis=1)

print(b.shape)
print(c.shape)
import numpy as np

a = np.arange(9)

b = np.expand_dims(a, axis=(0, 1))
c = np.expand_dims(a, axis=(0, 2))
d = np.expand_dims(a, axis=(1, 2))

print(b.shape)
print(c.shape)
print(d.shape)
import numpy as np

a = np.arange(9).reshape((3, 3))

b = np.expand_dims(a, axis=0)
c = np.expand_dims(a, axis=1)
d = np.expand_dims(a, axis=-1)
e = np.expand_dims(a, axis=(0,-1))

print(b.shape)
print(c.shape)
print(d.shape)
print(e.shape)<jupyter_output>(1, 3, 3)
(3, 1, 3)
(3, 3, 1)
(1, 3, 3, 1)<jupyter_text>**Removing Dummy Dimensions**  Using np.reshape<jupyter_code>import numpy as np

a = np.ones(shape=(1, 10))

b = a.reshape((10, ))
c = a.reshape((-1, ))
d = a.flatten()

print(b.shape)
print(c.shape)
print(d.shape)<jupyter_output>(10,)
(10,)
(10,)<jupyter_text>Using Slicing<jupyter_code>import numpy as np

a = np.ones(shape=(1,3,4))
b = np.ones(shape=(3,4,1))

c = a.reshape(*a.shape[1:]) # *(1, [3, 4]) -> unpacking (3, 4)
d = b.reshape(*b.shape[:-1])

print(c.shape)
print(d.shape)
import numpy as np

a = np.ones(shape=(3, 3))

row, col = a[1, :], a[:, 1]

print(row.shape)
print(col.shape)
import numpy as np

a = np.arange(9).reshape((1, -1))
b = np.arange(9).reshape((-1, 1))

c = a[0, :]
d = b[:, 0]

print(c.shape)
print(d.shape)
import numpy as np

a = np.ones(shape=(1, 3, 4))
b = np.ones(shape=(3, 4, 1))

c = a[0, ...]
d = b[..., 0]

print(c.shape)
print(d.shape)<jupyter_output>(3, 4)
(3, 4)<jupyter_text>Using squeeze API<jupyter_code>import numpy as np

a = np.ones(shape = (1, 1, 4, 1, 3, 1))

b = np.squeeze(a) #차원 값이 1인 것 제거
d = a.squeeze()

print(a.shape)
print(b.shape)
print(d.shape)<jupyter_output>(1, 1, 4, 1, 3, 1)
(4, 3)
(4, 3)<jupyter_text>**Changing Demensions**  Using np.swapaxes API 두 가지 차원을 바꿀 때 효과적인 api<jupyter_code>import numpy as np

a = np.random.normal(size=(3, 4, 5, 6))

b = np.swapaxes(a, 0, 1)
c = np.swapaxes(a, 0, 2)
d = np.swapaxes(a, 0, 3)

print(a.shape)
print(b.shape)
print(c.shape)
print(d.shape)
import numpy as np

a = np.random.normal(size=(3, 200, 100))

b = np.swapaxes(a, 0, -1) # 첫번째 차원과 마지막 차원을 바꾸기

print(a.shape)
print(b.shape)<jupyter_output>(3, 200, 100)
(100, 200, 3)<jupyter_text>Using np.moveaxis API<jupyter_code>import numpy as np

a = np.random.normal(size=(3, 4, 5, 6))

b = np.moveaxis(a, source=0, destination=1)  # source만 이동하는 것
c = np.moveaxis(a, source=0, destination=2)
d = np.moveaxis(a, source=0, destination=-1)

print(a.shape)
print(b.shape)
print(c.shape)
print(d.shape)<jupyter_output>(3, 4, 5, 6)
(4, 3, 5, 6)
(4, 5, 3, 6)
(4, 5, 6, 3)<jupyter_text>Using np.transpose API<jupyter_code>import numpy as np

a = np.random.normal(size=(3, 4))

b = np.transpose(a)
c = a.T

print(a.shape)
print(b.shape)
print(c.shape)
import numpy as np

a = np.random.normal(size=(3, 4, 5, 6, 7))

b = np.transpose(a)
c = a.T

print(a.shape)
print(b.shape)
print(c.shape)
import numpy as np

a = np.random.normal(size=(3, 4, 5))

b = np.transpose(a, axes=(0, 1, 2))
c = np.transpose(a, axes=(1, 2, 0))
d = np.transpose(a, axes=(2, 0, 1))
e = np.transpose(a, axes=(2, 1, 0))

print(a.shape)
print(b.shape)
print(c.shape)
print(d.shape)
print(e.shape)
import numpy as np

a = np.random.normal(size=(3, 4, 5, 6))

b = np.transpose(a, axes=tuple(range(a.ndim))[::-1])  
# a.ndim = 4 / range(a.ndim) = 0, 1, 2, 3 / tuple(0, 1, 2, 3) = (0,1,2,3)

print(b.shape)<jupyter_output>(6, 5, 4, 3)
{'fraction_non_alphanumeric': 0.21078821656050956, 'fraction_numerical': 0.06369426751592357, 'mean_word_length': 4.438311688311688, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '170180', 'license': 'MIT', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2541, 'n_words': 557, 'path': 'Python Practice/Numpy Study/Dimensionality Manipulations.ipynb', 'repo_name': 'kimbyeolhee/TIL', 'size': '5024'}