[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?
- Scenario
- Increase the exam scores of two student groups by 5 points each.
- Limitations of Python lists
- Cannot directly perform the simple operation of adding 5 to every number in a list
[80, 90, 70] + 5โ Error!- A list and a number cannot be added
- Solution
- Use a
forloop to take each number and add 5, - then put the results in a new list.
- Use a
- This becomes very cumbersome and slow as the data grows!
# 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
- What Is NumPy?
- Short for Numerical Python
- A library for processing large amounts of numerical data quickly and efficiently
- NumPy Array (ndarray)
- A multidimensional array (list) used in NumPy
- Constraint: Stores only data of the same type
- Numbers together or text together
- This constraint enables very fast operations!
- Vectorized Operations
- Apply an operation to all array elements at once without a loop.
- The most important feature!
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
- What is shape?
- Information about how many rows and columns an array has.
- A fundamental and important part of understanding data structure in Machine Learning.
- 1-dimensional array: Vector
- e.g., scores of 5 students
- 2-dimensional array: Matrix
- e.g., scores of 5 students in 2 subjects, Korean and English
- 3-dimensional array: Tensor
- e.g., a color image with pixels indexed by (width, height, color)
# 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 columns1D ๋ฐฐ์ด: [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)
reshape()- Use this to keep the data contents and total count unchanged
- while changing only the array shape.
- Using -1
- A wildcard meaning "calculate the remaining dimension for me".
array.reshape(2, -1)- "There are 6 elements, and I want 2 rows. Calculate the columns for me."
- โ Reshape to (2, 3)
array.reshape(-1, 3)- "There are 6 elements, and I want 3 columns. Calculate the rows for me."
- โ Reshape to (2, 3)
- Note: An error occurs if the total element count is not divisible by the specified dimension.
- e.g., 6 elements cannot form 4 rows
# 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
- What is an axis?
- Specifies the direction of an operation in a multidimensional array.
- Visualize It (in 2 Dimensions)
axis=0: Vertical direction (โ)- Operate on each column.
- Imagine compressing multiple rows down into one row.
axis=1: Horizontal direction (โ)- Operate on each row.
- Imagine compressing multiple columns into one column.
- If
axisis not specified, the operation applies to all elements of the array.
# 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
np.arange(n): Creates an array containing numbers from 0 to n-1np.zeros(shape): Create an array filled with 0 (useful for initialization).np.ones(shape): Create an array filled with 1
# 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.]]