원소별 산술 연산
배열을 원소별로 더하고 빼고 곱하고 나누며, NumPy가 명시적인 Python 반복문 없이 연산하는 방식을 이해합니다.
원소별 산술 연산은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
The Problem with Python Loops
Adding lists in pure Python needs a slow loop. NumPy does it in fast C instead — often 10 to 100 times quicker. This trick is called vectorisation. ⚡
# Python list approach -- slow
a = [1, 2, 3, 4]
b = [10, 20, 30, 40]
result = [x + y for x, y in zip(a, b)]
print(result) # [11, 22, 33, 44]
# NumPy approach -- fast
import numpy as np
na, nb = np.array(a), np.array(b)
print(na + nb) # [11 22 33 44]Addition and Subtraction
The + and - operators work element by element on arrays of the same shape, pairing up matching positions in one quick pass.
import numpy as np
a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(a + b) # [11 22 33]
print(a - b) # [ 9 18 27]Multiplication and Division
Use * and / for element-wise multiplication and division — note * is not matrix multiply. Division always gives floats, even from whole numbers.
import numpy as np
a = np.array([10, 20, 30])
b = np.array([2, 4, 5])
print(a * b) # [ 20 80 150]
print(a / b) # [5. 5. 6.]
print(a // b) # [5 5 6]
print(a % b) # [0 0 0]Scalar Arithmetic (Broadcasting Preview)
Add a single number to an array and NumPy applies it to every element. That's broadcasting in its simplest form — great for shifting or scaling data.
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a + 10) # [11 12 13 14 15]
print(a * 2) # [ 2 4 6 8 10]
print(a ** 2) # [ 1 4 9 16 25]
print(a / 10) # [0.1 0.2 0.3 0.4 0.5]Exponentiation and Square Root
Use ** to raise every element to a power. For square roots, reach for the np.sqrt ufunc — it's built in C and faster than looping in Python.
import numpy as np
a = np.array([1.0, 4.0, 9.0, 16.0])
print(a ** 0.5) # [1. 2. 3. 4.]
print(np.sqrt(a)) # [1. 2. 3. 4.]
print(a ** 2) # [ 1. 16. 81. 256.]Comparison Operators Return Boolean Arrays
Compare an array with >, ==, or < and you get back a boolean array of True/False values — the foundation for filtering your data.
import numpy as np
a = np.array([3, 7, 2, 9, 1])
print(a > 4) # [False True False True False]
print(a == 2) # [False False True False False]
print(a >= 3) # [ True True False True False]Logical Operators on Boolean Arrays
To combine boolean arrays, use the symbols &, |, and ~ — not the words and/or/not. Wrap each condition in parentheses to avoid precedence surprises.
import numpy as np
a = np.array([3, 7, 2, 9, 1])
mask = (a > 2) & (a < 8)
print(mask) # [ True True False False False]
print(a[mask]) # [3 7]In-Place Operations with +=
Operators like += change an array in place, saving memory by skipping a new copy. Just keep dtypes compatible — you can't add 0.5 to an int array this way.
import numpy as np
a = np.array([1.0, 2.0, 3.0])
a += 10.0
print(a) # [11. 12. 13.]
a *= 2.0
print(a) # [22. 24. 26.]2-D Array Arithmetic
Element-wise math works the same on 2-D arrays: add two matrices of matching shape and each cell sums with its counterpart. No nested loops needed.
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A + B)
# [[ 6 8]
# [10 12]]
print(A * B) # element-wise, NOT matrix multiply
# [[ 5 12]
# [21 32]]dtype Promotion in Mixed Operations
Mix two dtypes and NumPy promotes both to the safer type, so no info is lost — int plus float gives float. Watch out: this can quietly grow your memory use.
import numpy as np
a = np.array([1, 2, 3], dtype=np.int32)
b = np.array([0.5, 1.5, 2.5], dtype=np.float64)
c = a + b
print(c) # [1.5 3.5 5.5]
print(c.dtype) # float64Absolute Value and Sign
Use np.abs for absolute values and np.sign to get -1, 0, or +1 per element. Both are fast ufuncs, common in loss functions and normalising steps.
import numpy as np
a = np.array([-3, 0, 4, -7, 2])
print(np.abs(a)) # [3 0 4 7 2]
print(np.sign(a)) # [-1 0 1 -1 1]Quick Check
Test your understanding of NumPy element-wise arithmetic from this lesson.
Lesson Recap
You've got it! NumPy does math element-wise with no loops, scalars broadcast across the whole array, and comparisons give boolean arrays. Next: slicing and indexing.
자주 묻는 질문
“원소별 산술 연산” 강의는 무료인가요?
네 — “원소별 산술 연산” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“원소별 산술 연산”에서 뭘 배우나요?
배열을 원소별로 더하고 빼고 곱하고 나누며, NumPy가 명시적인 Python 반복문 없이 연산하는 방식을 이해합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“원소별 산술 연산” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- NumPy 배열 만들기
- 배열 속성과 검사
- 원소별 산술 연산
- 배열 슬라이싱과 인덱싱