Course materials ยท Introduction to Machine Learning

[Week 5] Powerful Data Computation: First Steps with NumPy ๐Ÿ”ข

English translation

Hello! Welcome to Week 5.

So far, we have learned to handle data using basic Python syntax.

What if we had millions of data points to handle?

Using only for loops would be too slow and inefficient.

Today we introduce NumPy, an essential Machine Learning library that addresses this problem.

How quickly and efficiently NumPy can process large amounts of numerical data,

We will also see directly why this matters in Machine Learning.

๐Ÿข Module 1: Why Are Python Lists Not Enough?

# Manage scores using a Python list
scores_list = [80, 95, 72, 100, 88]

# Steps to increase every score by 5
new_scores_list = [] # An empty list for the new scores
for score in scores_list:
    # Take each value, add 5, and append it to the new list
    new_scores_list.append(score + 5)

print(f"์›๋ณธ ์ ์ˆ˜ ๋ฆฌ์ŠคํŠธ: {scores_list}")
print(f"5์  ์˜ฌ๋ฆฐ ํ›„ ๋ฆฌ์ŠคํŠธ: {new_scores_list}")
print("\n๋ฐ์ดํ„ฐ๊ฐ€ 5๊ฐœ๋งŒ ์žˆ์–ด๋„ ์ด๋ ‡๊ฒŒ ๋ฒˆ๊ฑฐ๋กœ์šด๋ฐ, 100๋งŒ ๊ฐœ๋ผ๋ฉด...?")
์›๋ณธ ์ ์ˆ˜ ๋ฆฌ์ŠคํŠธ: [80, 95, 72, 100, 88]
5์  ์˜ฌ๋ฆฐ ํ›„ ๋ฆฌ์ŠคํŠธ: [85, 100, 77, 105, 93]

๋ฐ์ดํ„ฐ๊ฐ€ 5๊ฐœ๋งŒ ์žˆ์–ด๋„ ์ด๋ ‡๊ฒŒ ๋ฒˆ๊ฑฐ๋กœ์šด๋ฐ, 100๋งŒ ๊ฐœ๋ผ๋ฉด...?

โœจ Module 2: Introducing NumPy Arrays

import numpy as np # Conventionally imported with the alias np.

# Convert a Python list into a NumPy array
scores_array = np.array([80, 95, 72, 100, 88])

print(f"NumPy ๋ฐฐ์—ด: {scores_array}")
print(f"์ž๋ฃŒํ˜•: {type(scores_array)}")
NumPy ๋ฐฐ์—ด: [ 80  95  72 100  88]
์ž๋ฃŒํ˜•: <class 'numpy.ndarray'>
# Use a NumPy array to increase every score by 5
# No loop is needed!

new_scores_array = scores_array + 5

print(f"์›๋ณธ ์ ์ˆ˜ ๋ฐฐ์—ด: {scores_array}")
print(f"5์  ์˜ฌ๋ฆฐ ํ›„ ๋ฐฐ์—ด: {new_scores_array}")

# All arithmetic operations are available, not just addition.
print(f"\n๋ชจ๋“  ์ ์ˆ˜ 10% ์ธ์ƒ: {scores_array * 1.1}")
print(f"๋ชจ๋“  ์ ์ˆ˜ 2๋กœ ๋‚˜๋ˆ„๊ธฐ: {scores_array / 2}")
์›๋ณธ ์ ์ˆ˜ ๋ฐฐ์—ด: [ 80  95  72 100  88]
5์  ์˜ฌ๋ฆฐ ํ›„ ๋ฐฐ์—ด: [ 85 100  77 105  93]

๋ชจ๋“  ์ ์ˆ˜ 10% ์ธ์ƒ: [ 88.  104.5  79.2 110.   96.8]
๋ชจ๋“  ์ ์ˆ˜ 2๋กœ ๋‚˜๋ˆ„๊ธฐ: [40.  47.5 36.  50.  44. ]

๐Ÿš€ Module 3: Essential NumPy Features for Machine Learning

1. Array Shape

# 1-dimensional array (Vector)
# [์ƒ์ž1, ์ƒ์ž2, ์ƒ์ž3, ์ƒ์ž4, ์ƒ์ž5, ์ƒ์ž6] -> 6 items in one row
array_1d = np.array([1, 2, 3, 4, 5, 6])
print(f"1D ๋ฐฐ์—ด: {array_1d}")
print(f"1D ๋ฐฐ์—ด์˜ Shape: {array_1d.shape}") # (6,) -> One row containing 6 items

print("-" * 20)

# 2-dimensional array (Matrix)
# [[๊ตญ์–ด1, ์˜์–ด1], -> First line (row)
# [๊ตญ์–ด2, ์˜์–ด2], -> Second line (row)
# [๊ตญ์–ด3, ์˜์–ด3]] -> Third line (row)
array_2d = np.array([
    [90, 85], # Student 1
    [75, 92], # Student 2
    [88, 80]  # Student 3
])
print(f"2D ๋ฐฐ์—ด:\n{array_2d}")
print(f"2D ๋ฐฐ์—ด์˜ Shape: {array_2d.shape}")
# (3, 2) -> 3 rows, 2 columns
1D ๋ฐฐ์—ด: [1 2 3 4 5 6]
1D ๋ฐฐ์—ด์˜ Shape: (6,)
--------------------
2D ๋ฐฐ์—ด:
[[90 85]
 [75 92]
 [88 80]]
2D ๋ฐฐ์—ด์˜ Shape: (3, 2)

2. Changing Array Shape (Reshape)

# 1-dimensional array (6 items)
array_1d = np.array([1, 2, 3, 4, 5, 6])
print(f"์›๋ณธ 1D ๋ฐฐ์—ด: {array_1d}")
print(f"์›๋ณธ 1D ๋ฐฐ์—ด Shape: {array_1d.shape}")

# Reshape into a 2-dimensional array with 2 rows and 3 columns
# [1,2,3,4,5,6] -> [[1,2,3],
#                   [4,5,6]]
array_reshaped = array_1d.reshape(2, 3)
print(f"\n(2, 3)์œผ๋กœ Reshape:\n{array_reshaped}")
print(f"Shape: {array_reshaped.shape}")

# Use -1 to calculate the number of columns automatically
# "Reshape 6 elements into 3 rows. Determine the columns automatically!" -> Requires 2 columns
array_reshaped_auto = array_1d.reshape(3, -1)
print(f"\n(3, -1)์œผ๋กœ Reshape:\n{array_reshaped_auto}")
print(f"Shape: {array_reshaped_auto.shape}")
์›๋ณธ 1D ๋ฐฐ์—ด: [1 2 3 4 5 6]
์›๋ณธ 1D ๋ฐฐ์—ด Shape: (6,)

(2, 3)์œผ๋กœ Reshape:
[[1 2 3]
 [4 5 6]]
Shape: (2, 3)

(3, -1)์œผ๋กœ Reshape:
[[1 2]
 [3 4]
 [5 6]]
Shape: (3, 2)

3. Operations Along an Axis

# Korean and English scores for 3 students
scores_2d = np.array([
    [90, 85], # Student 1
    [75, 92], # Student 2
    [88, 80]  # Student 3
])
print(f"์›๋ณธ ์ ์ˆ˜ ๋ฐฐ์—ด (Shape: {scores_2d.shape}):\n{scores_2d}")

# 1. Overall sum / mean
print(f"\n์ „์ฒด ์ด์ : {scores_2d.sum()}")
print(f"์ „์ฒด ํ‰๊ท : {scores_2d.mean():.2f}")

# 2. axis=0 (vertical direction โ†“) operations
# Sum/mean of each 'column' -> Statistics by subject
print(f"\n--- axis=0 (๊ณผ๋ชฉ๋ณ„ ํ†ต๊ณ„) ---")

print(f"๊ณผ๋ชฉ๋ณ„ ์ด์ : {scores_2d.sum(axis=0)}")
# [90+75+88, 85+92+80]

print(f"๊ณผ๋ชฉ๋ณ„ ํ‰๊ท : {scores_2d.mean(axis=0)}")
# [avg(90,75,88), avg(85,92,80)]

print(f"๊ณผ๋ชฉ๋ณ„ ์ตœ๊ณ ์ : {scores_2d.max(axis=0)}")
# [max(90,75,88), max(85,92,80)]

# 3. axis=1 (horizontal direction โ†’) operations
# Sum/mean of each 'row' -> Statistics by student
print(f"\n--- axis=1 (ํ•™์ƒ๋ณ„ ํ†ต๊ณ„) ---")

print(f"ํ•™์ƒ๋ณ„ ์ด์ : {scores_2d.sum(axis=1)}")
# [90+85, 75+92, 88+80]

print(f"ํ•™์ƒ๋ณ„ ํ‰๊ท : {scores_2d.mean(axis=1)}")
# [avg(90,85), avg(75,92), avg(88,80)]

print(f"ํ•™์ƒ๋ณ„ ์ตœ๊ณ ์ : {scores_2d.max(axis=1)}")
# [max(90,85), max(75,92), max(88,80)]
์›๋ณธ ์ ์ˆ˜ ๋ฐฐ์—ด (Shape: (3, 2)):
[[90 85]
 [75 92]
 [88 80]]

์ „์ฒด ์ด์ : 510
์ „์ฒด ํ‰๊ท : 85.00

--- axis=0 (๊ณผ๋ชฉ๋ณ„ ํ†ต๊ณ„) ---
๊ณผ๋ชฉ๋ณ„ ์ด์ : [253 257]
๊ณผ๋ชฉ๋ณ„ ํ‰๊ท : [84.33333333 85.66666667]
๊ณผ๋ชฉ๋ณ„ ์ตœ๊ณ ์ : [90 92]

--- axis=1 (ํ•™์ƒ๋ณ„ ํ†ต๊ณ„) ---
ํ•™์ƒ๋ณ„ ์ด์ : [175 167 168]
ํ•™์ƒ๋ณ„ ํ‰๊ท : [87.5 83.5 84. ]
ํ•™์ƒ๋ณ„ ์ตœ๊ณ ์ : [90 92 88]

4. Useful Array Creation Functions

# An array containing the numbers 0 through 9
range_array = np.arange(10)
print(f"np.arange(10):\n{range_array}")

# An array filled with 0, with shape (2, 3)
zeros_array = np.zeros((2, 3))
print(f"\nnp.zeros((2, 3)):\n{zeros_array}")

# An array filled with 1, with shape (3, 2)
ones_array = np.ones((3, 2))
print(f"\nnp.ones((3, 2)):\n{ones_array}")
np.arange(10):
[0 1 2 3 4 5 6 7 8 9]

np.zeros((2, 3)):
[[0. 0. 0.]
 [0. 0. 0.]]

np.ones((3, 2)):
[[1. 1.]
 [1. 1.]
 [1. 1.]]