<jupyter_start><jupyter_text>1. 배열 생성<jupyter_code>np.array([1, 2, 3, 4])
np.array([[1, 2, 3], [4, 5, 6]])<jupyter_output><empty_output><jupyter_text>1.1. 자료형 지정<jupyter_code>np.array([1.2, 2.1, -1.6, 4.45], dtype='f')<jupyter_output><empty_output><jupyter_text>1.2. 영벡터 생성<jupyter_code>np.zeros(3, dtype='i')
np.zeros((2, 3))<jupyter_output><empty_output><jupyter_text>1.3. 원소가 1인 벡터 생성<jupyter_code>np.ones((2, 6))<jupyter_output><empty_output><jupyter_text>1.4. 다른 배열을 이용한 벡터 생성<jupyter_code>a = np.array([[1, 2,], [3, 4], [5, 6]])
print(a)
print(a.shape)
b = np.zeros_like(a, dtype='f')
# b = np.ones_like(a, dtype='f')
print()
print(b)
print(b.shape)<jupyter_output>[[1 2]
 [3 4]
 [5 6]]
(3, 2)

[[1. 1.]
 [1. 1.]
 [1. 1.]]
(3, 2)<jupyter_text>1.5. 초기화 없이 배열 생성 @메모리에 있는 값을 바탕으로 배열 요소값이 지정됨<jupyter_code>a = np.empty((10, 3))
print(a)<jupyter_output>[[1.41158355e-311 9.58487353e-322 0.00000000e+000]
 [0.00000000e+000 6.95293141e-310 5.02034658e+175]
 [1.27010965e-075 2.57751692e-056 4.16150492e-061]
 [6.12673096e-062 1.47763641e+248 1.16096346e-028]
 [7.69165785e+218 1.35617292e+248 1.10208290e-046]
 [5.64373615e-038 1.40415688e+165 2.89818752e-057]
 [4.26050046e-096 6.32299154e+233 6.48224638e+170]
 [5.22411352e+257 5.74020278e+180 8.37174974e-144]
 [1.41529402e+161 9.16651763e-072 4.52660428e+097]
 [1.24716443e-047 3.58569463e+126 2.12201162e-314]]<jupyter_text>1.6. Arange 사용<jupyter_code>np.arange(10)
# np.arange(시작, 끝, 공차)
np.arange(3, 20, 4)<jupyter_output><empty_output><jupyter_text>2. 차원 확인<jupyter_code>a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)
print(np.ndim(a))
a = np.array([[[1, 2, 3], [4, 5, 6]], [[2, 4, 6], [-1, 0, 0]]])
print(a)
print(a.shape)
a = np.array([[[1, 2, 3]], [[-1, 0, 0]]])
print(a)
print(a.shape)
print(np.ndim(a))<jupyter_output>[[[ 1  2  3]]

 [[-1  0  0]]]
(2, 1, 3)
3<jupyter_text>3. 차원 변경<jupyter_code>a = np.array([1, 2, 3, 4, 5, 6])
print(a)
print(a.reshape([2, 3]))
print(a.reshape([2, -1]))
print(a.reshape([-1, 2]))
print(a.reshape([2, 3, 1]))<jupyter_output>[[[1]
  [2]
  [3]]

 [[4]
  [5]
  [6]]]<jupyter_text>3.1. 전치<jupyter_code>a = np.array([[1, 2, 3,], [4, 5, 6]])
print(a)
print(a.shape)
print()

b = a.T
print(b)
print(b.shape)<jupyter_output>[[1 2 3]
 [4 5 6]]
(2, 3)

[[1 4]
 [2 5]
 [3 6]]
(3, 2)<jupyter_text>3.2. 차원축 추가<jupyter_code>a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)
print()

a = np.expand_dims(a, axis=0)
print(a.shape)<jupyter_output>(2, 3)

(1, 2, 3)<jupyter_text>4. 자료형 변경<jupyter_code>a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
print(a.astype('float32'))<jupyter_output>[[1 2 3]
 [4 5 6]]
[[1. 2. 3.]
 [4. 5. 6.]]<jupyter_text>5. 배열 계산  5.1. Elementwise 계산<jupyter_code>a = np.array([[1, 2, 3], [4, 5, 6]]).astype('float32')
print(a)
print(a/2)
a = np.array([1, 2, 3, 4, 5, 6])
print(a)
b = a >= 4
print(b)
print(b.astype(np.int))<jupyter_output>[1 2 3 4 5 6]
[False False False  True  True  True]
[0 0 0 1 1 1]<jupyter_text>5.2. 행렬 곱<jupyter_code>a = np.array([[1, 0], [2, 2]])
print(a.shape)

b = np.array([[2, 2], [3, 0]])
print(b.shape)

print(np.dot(a, b))
a = np.array(
    [[1, 1, 2],
     [0, 2, 0]])
print(a.shape)

b = np.array(
    [[1, 2],
     [0, 3],
     [1, 1]])
print(b.shape)

print(np.dot(a, b))<jupyter_output>(2, 3)
(3, 2)
[[3 7]
 [0 6]]<jupyter_text>6. hstack<jupyter_code>a = np.array([1, 1, [2, 2, -1], [3, 0], 6])
print(a)
b = np.hstack(a)
print(b)
a = np.array([[1, 1, [2, 2, -1], [3, 0], 6], [12, 12, [21, 21, -11], [30, 0], 16]])
print(a)
b = np.hstack(a)
print(b)
tmp = list()
for i in a:
    tmp.append(np.hstack(i))

tmp1 = np.vstack(tmp)
print(tmp1)<jupyter_output>[[  1   1   2   2  -1   3   0   6]
 [ 12  12  21  21 -11  30   0  16]]
{'fraction_non_alphanumeric': 0.2467992372650504, 'fraction_numerical': 0.18060473985290112, 'mean_word_length': 4.36058394160584, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '2973', 'license': 'MIT', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2161, 'n_words': 397, 'path': 'jupyter_notebooks/Numpy_Memo0.ipynb', 'repo_name': '0pt3ryx/0pt3ryx.github.io', 'size': '3671'}