Course materials Β· Introduction to Machine Learning

[Week 3] First Steps toward Machine Learning: Python Basics and Data Representation 🐍

English translation

Hello! Welcome to Week 3.

Over the past 2 weeks, we have seen the remarkable possibilities of Machine Learning.

For the next 4 weeks, we will learn the most important tool for building these models ourselves,

We will learn the Python programming language.


In this first session, we will explore why Python is essential in Machine Learning,

We will learn how to represent and store data in Python.

🌐 Module 1: Why Use Python for Machine Learning?

πŸ“¦ Module 2: Variables, the Smallest Units of Data

# Store 'one student's' data in variables.

# 1. Store the floating-point (float) value 10.5 in a variable labeled 'study hours'
study_hours = 10.5

# 2. Store the integer (int) value 92 in a variable labeled 'exam score'
exam_score = 92

# Check the stored values
print("곡뢀 μ‹œκ°„:", study_hours)
print("μ‹œν—˜ 점수:", exam_score)

# Check variable data types using type()
print("\n'study_hours' λ³€μˆ˜μ˜ μžλ£Œν˜•:", type(study_hours))
print("'exam_score' λ³€μˆ˜μ˜ μžλ£Œν˜•:", type(exam_score))
곡뢀 μ‹œκ°„: 10.5
μ‹œν—˜ 점수: 92

'study_hours' λ³€μˆ˜μ˜ μžλ£Œν˜•: <class 'float'>
'exam_score' λ³€μˆ˜μ˜ μžλ£Œν˜•: <class 'int'>
# Store additional student information

# Store the string (str) value "Kim" in a variable labeled 'student name'
student_name = "Kim"

# Store the Boolean (bool) value True in a variable labeled 'pass status'
is_passed = True

# Check the stored values
print("학생 이름:", student_name)
print("합격 μ—¬λΆ€:", is_passed)

# Check data types
print("\n'student_name' λ³€μˆ˜μ˜ μžλ£Œν˜•:", type(student_name))
print("'is_passed' λ³€μˆ˜μ˜ μžλ£Œν˜•:", type(is_passed))
학생 이름: Kim
합격 μ—¬λΆ€: True

'student_name' λ³€μˆ˜μ˜ μžλ£Œν˜•: <class 'str'>
'is_passed' λ³€μˆ˜μ˜ μžλ£Œν˜•: <class 'bool'>
# Variables can also be used in calculations.
# For example, suppose this student answered a 5-point bonus question correctly.

print("기쑴 점수:", exam_score)

# Add 5 to the existing score and update it
final_score = exam_score + 5

print("μ΅œμ’… 점수:", final_score)
기쑴 점수: 92
μ΅œμ’… 점수: 97

🧺 Module 3: Data Structures, Containers for Datasets

How can we handle multiple students' data at once?

We need a larger container, a data structure, that can hold multiple values in one variable.

1. Lists

# Create lists of 'study hours' and 'exam scores' for multiple students.
study_hours_list = [2.0, 4.5, 6.0, 8.5, 10.0]
exam_scores_list = [65, 78, 85, 95, 98]

print("곡뢀 μ‹œκ°„ 리슀트:", study_hours_list)
print("μ‹œν—˜ 점수 리슀트:", exam_scores_list)

# Lists have an order, so specific values can be accessed by index.
# Python starts counting at 0!
print("\n첫 번째 ν•™μƒμ˜ 곡뢀 μ‹œκ°„:", study_hours_list[0])
print("μ„Έ 번째 ν•™μƒμ˜ μ‹œν—˜ 점수:", exam_scores_list[2])

# Use len() to check how many data items a list contains.
print("\n총 학생 수:", len(study_hours_list))
곡뢀 μ‹œκ°„ 리슀트: [2.0, 4.5, 6.0, 8.5, 10.0]
μ‹œν—˜ 점수 리슀트: [65, 78, 85, 95, 98]

첫 번째 ν•™μƒμ˜ 곡뢀 μ‹œκ°„: 2.0
μ„Έ 번째 ν•™μƒμ˜ μ‹œν—˜ 점수: 85

총 학생 수: 5
# Suppose a new student's data have been added.
# Use .append() to add a value to the end of a list.

print("μΆ”κ°€ μ „ 곡뢀 μ‹œκ°„ 리슀트:", study_hours_list)

study_hours_list.append(12.0)
exam_scores_list.append(100)

print("μΆ”κ°€ ν›„ 곡뢀 μ‹œκ°„ 리슀트:", study_hours_list)
print("μΆ”κ°€ ν›„ 총 학생 수:", len(study_hours_list))
μΆ”κ°€ μ „ 곡뢀 μ‹œκ°„ 리슀트: [2.0, 4.5, 6.0, 8.5, 10.0]
μΆ”κ°€ ν›„ 곡뢀 μ‹œκ°„ 리슀트: [2.0, 4.5, 6.0, 8.5, 10.0, 12.0]
μΆ”κ°€ ν›„ 총 학생 수: 6

2. Dictionaries

# Create a dictionary containing all information about one student.
student_A_info = {
    'name': 'Park',
    'study_hours': 8.5,
    'exam_score': 95,
    'is_passed': True
}

print("학생 A의 정보:", student_A_info)

# A dictionary accesses values using Keys rather than positions (indices).
print("\n학생 A의 이름:", student_A_info['name'])
print("학생 A의 μ‹œν—˜ 점수:", student_A_info['exam_score'])
학생 A의 정보: {'name': 'Park', 'study_hours': 8.5, 'exam_score': 95, 'is_passed': True}

학생 A의 이름: Park
학생 A의 μ‹œν—˜ 점수: 95

Final form: Combining lists and dictionaries

# Group multiple students' information (dictionaries) in one list to represent the full dataset
full_dataset = [
    { 'name': 'Kim', 'study_hours': 2.0, 'exam_score': 65, 'is_passed': True },
    { 'name': 'Lee', 'study_hours': 4.5, 'exam_score': 78, 'is_passed': True },
    { 'name': 'Park', 'study_hours': 8.5, 'exam_score': 95, 'is_passed': True },
    { 'name': 'Choi', 'study_hours': 1.5, 'exam_score': 55, 'is_passed': False }
]

# Access the first student's ('Kim') information
first_student = full_dataset[0]
print("첫 번째 ν•™μƒμ˜ 정보:", first_student)

# Access only the first student's 'exam score'
first_student_score = full_dataset[0]['exam_score']
print("첫 번째 ν•™μƒμ˜ μ‹œν—˜ 점수:", first_student_score)
첫 번째 ν•™μƒμ˜ 정보: {'name': 'Kim', 'study_hours': 2.0, 'exam_score': 65, 'is_passed': True}
첫 번째 ν•™μƒμ˜ μ‹œν—˜ 점수: 65