# Numpy

1. Numpy  as np 기본 개념 및 기능

- 파이썬 데이터 분석에서 가장 많이 사용하는 기본 데이터형, 다양한 값을 연속으로 저장하는 것.

- Numpy 배열 - 동일한 데이터 타입을 가지는 다차원 배열, 리스트보다 메모리 효율성이 뛰어나고 베거화 연산을 지원하여 반복문 없이 빠른 계산이 가능.

```javascript
import numpy as np
import time

# 데이터 생성
data_list = list(range(1, 100000001))  # 1부터 1,000,000까지의 리스트 생성
data_array = np.array(data_list)     # 같은 데이터를 가진 NumPy 배열 생성

# 비교 - 1
# 루프를 사용한 연산
start_time = time.time()

result_list = []
for x in data_list:
    result_list.append(x * 2)

end_time = time.time()
loop_duration = end_time - start_time

print(f"루프를 사용한 연산 시간: {loop_duration:.6f}초")

# 비교 - 2
# 벡터화된 연산 (Vectorized Operations)
start_time = time.time()

result_array = data_array * 2

end_time = time.time()
vectorized_duration = end_time - start_time

print(f"Vectorized Operations 연산 시간: {vectorized_duration:.6f}초")

# 결과 비교
print(f"Vectorized Operations 연산 시간 빠르기 : {loop_duration / vectorized_duration:.2f}배 빠름")

```

![Image](https://upload.cafenono.com/image/slashpagePost/20260116/181401_3nkerlD28Gry2HVIEq?q=80&s=1280x180&t=outside&f=webp)

집 밖으로 나와 카페에서 laptop으로 작업하다보니 시간이 분단위로 뛴다 ㄷㄷ

1. 넘파이 배열 속성(dtype, ndim, T, size, nbtypes, flat)

```javascript
import numpy as np

# 배열 생성
array = np.array([[1, 2, 3], [4, 5, 6]])

# 데이터형(dtype) 확인
print("Data Type (dtype):", array.dtype)

# 모양(shape) 확인
print("Shape (shape):", array.shape)

# 차원(ndim) 확인
print("Number of Dimensions (ndim):", array.ndim)

# 배열의 행/열 변환 (transpose)
print("Transposed Array (T):")
print(array.T)

# 배열의 원소 수(size) 확인
print("Total Number of Elements (size):", array.size)

# 배열의 전체 바이트 수(nbytes) 확인
print("Total Bytes (nbytes):", array.nbytes)
```

![Image](https://upload.cafenono.com/image/slashpagePost/20260116/181428_7rPewsosWkGQrJIruD?q=80&s=1280x180&t=outside&f=webp)

1. 평탄화

```javascript
import numpy as np
# 2D 배열 생성
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
# 배열을 1D로 평탄화
flattened_array = array_2d.flatten()
print("Original 2D Array:")
print(array_2d)
print("\nFlattened 1D Array:")
print(flattened_array)
```

![Image](https://upload.cafenono.com/image/slashpagePost/20260116/181437_diM77D2eyQp7nBRMxo?q=80&s=1280x180&t=outside&f=webp)

1. 인덱싱

```javascript
import numpy as np
# 1D 배열
array_1d = np.array([10, 20, 30, 40, 50])
print("1D 배열에서 3번째 요소:", array_1d[2])
# 2D 배열
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("2D 배열에서 (2, 3) 요소:", array_2d[1, 2])

```

![Image](https://upload.cafenono.com/image/slashpagePost/20260116/181449_LQ7RRAXTIjVkImxBgP?q=80&s=1280x180&t=outside&f=webp)

1. 슬라이싱 中

```javascript
import numpy as np

# 2D 배열
array_2d = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 14]])
print("강사님이 시키신 것 :")
print(array_2d[0:2:1, 0:2:1])
```

![Image](https://upload.cafenono.com/image/slashpagePost/20260116/181459_Dx05GOFf8ydYQcWEuJ?q=80&s=1280x180&t=outside&f=webp)

## 💭 회고

1. **배운 점**

- Pandas 데이터프레임 - 판다스는 데이터 전처리와 분석에 특화된 라이브러리로, DataFrame과 Series를 통해 테이블 형태의 데이터를 쉽게 다룸.

2. **어려운 점/개선할 점**

- 다차원 배열의 복잡한 인덱싱 및 슬라이싱은 아직 완벽하게 익숙하지 않아 추가적인 실습이 필요함.

- 벡터화 연산과 브로드캐스팅의 원리를 다양한 사례로 더 실습해 보며, 성능 차이를 직접 체험해 보고 싶음.(프로젝트와 실무에 중요할듯)

For the site tree, see the [root Markdown](https://slashpage.com/sonbo.md).
