λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
ꡐ윑/슀파λ₯΄νƒ€

[TIL] 내일배움캠프 AI 9κΈ° - 2회

by gomdeng 2024. 11. 26.

🐢 ν•™μŠ΅ λ²”μœ„

  • κ°•μ’Œ: μΈκ³΅μ§€λŠ₯을 μœ„ν•œ 파이썬 (3μ£Όμ°¨ ~ 4μ£Όμ°¨)
    • 3-5. 파이썬 νŒ¨ν‚€μ§€
    • 3-6. μ˜ˆμ™Έμ²˜λ¦¬
    • 4.* 클래슀, 객체 ..

 

🐢 κ³΅λΆ€λ‚΄μš©

✨ νŒ¨ν‚€μ§€ 

🚩 μš”μ•½
 - numpy, requirements.txt ..
    
🚩 λ‚΄μš©
1. NumPy (Numerical Python의 μ•½μž)
 > 톡계, μ„ ν˜•λŒ€μˆ˜ λ“±μ˜ μˆ˜ν•™μ  연산을 μˆ˜ν–‰ν•  수 μžˆλŠ” κ΄‘λ²”μœ„ν•œ ν•¨μˆ˜ 포함
 > λŒ€κ·œλͺ¨ 닀차원 λ°°μ—΄κ³Ό 행렬을 효과적으둜 닀루기 μœ„ν•œ λ‹€μ–‘ν•œ κΈ°λŠ₯을 제곡
 > λ²‘ν„°ν™”λœ 연산을 톡해 루프 λŒ€μ‹  전체 배열에 λŒ€ν•΄ μˆ˜ν•™μ  연산을 λΉ λ₯΄κ²Œ μˆ˜ν–‰
 
2. requirements.txt
 > 파이썬 ν”„λ‘œμ νŠΈμ— λŒ€ν•œ λͺ¨λ“  νŒ¨ν‚€μ§€μ™€ κ·Έ μ •ν™•ν•œ 버전을 λͺ…μ‹œ
 > λ‹€λ₯Έ 개발 ν™˜κ²½μ—μ„œλ„ λ™μΌν•œ ν™˜κ²½μ„ μ‰½κ²Œ μ„€μ • κ°€λŠ₯
 > 개발자 κ°„μ˜ ν˜Έν™˜μ„± 문제λ₯Ό μ΅œμ†Œν™”
 
 # μ˜ˆμ‹œ
 numpy == 1.18.5
 pandas == 1.1.3
 
 # 버전 λͺ…μ‹œλ°©λ²• 
 1. ==: μ •ν™•ν•œ 버전
 2. >=: μ§€μ •λœ 버전 이상
 3. <=: μ§€μ •λœ 버전 μ΄ν•˜
 4. >, <: μ§€μ •λœ 버전보닀 ν¬κ±°λ‚˜ μž‘μŒ

🚩 μ‚¬μš©λ²•
1. NumPy
    import numpy as np
	
    # λ°°μ—΄ 생성
    a = np.array([1, 2, 3])

    # 배열에 λŒ€ν•œ μ—°μ‚° μˆ˜ν–‰
    print(a + 2)  # [3, 4, 5]
    print(a * 2)  # [2, 4, 6]

    # 닀차원 λ°°μ—΄ 생성
    b = np.array([[1, 2, 3], [4, 5, 6]])

    # ν–‰λ ¬ κ³±μ…ˆ
    c = np.dot(b, a)  # [1*1 + 2*2 + 3*3, 4*1 + 5*2 + 6*3]
    print(c)  # [14, 32]

2. requirements.txt
    # 파일 생성
    pip freeze > requirements.txt
    
    # requirements.txt 을 μ΄μš©ν•œ νŒ¨ν‚€μ§€ μ„€μΉ˜
    pip install -r requirements.txt

 

✨ μ˜ˆμ™Έμ²˜λ¦¬

🚩 μš”μ•½
 - μ˜ˆμ™Έμ²˜λ¦¬
    
🚩 λ‚΄μš©
1. κΈ°λ³Έ 문법
 > try, except, else, finally

🚩 μ‚¬μš©λ²•
    # 닀쀑 μ˜ˆμ™Έ 처리
    try:
        # μ˜ˆμ™Έκ°€ λ°œμƒν•  수 μžˆλŠ” μ½”λ“œ
        result = 10 / 0
    except ZeroDivisionError:
        # νŠΉμ • μ˜ˆμ™Έ 처리 : ZeroDivisionError λ°œμƒ μ‹œ μ²˜λ¦¬ν•  μ½”λ“œ
        print("0으둜 λ‚˜λˆŒ 수 μ—†μŠ΅λ‹ˆλ‹€.")
    except Exception as e:
        # λͺ¨λ“  μ˜ˆμ™Έ 처리
        print("μ—λŸ¬μž…λ‹ˆλ‹€.")
    else:
        # μ˜ˆμ™Έκ°€ λ°œμƒ μ•ˆν•  μ‹œ, μ‹€ν–‰ν•  μ½”λ“œ
        print("κ²°κ³ΌλŠ”", result)
    finally:
        # μ˜ˆμ™Έ λ°œμƒ 여뢀와 상관없이 항상 싀행될 μ½”λ“œ
        print("싀행을 μ’…λ£Œν•©λ‹ˆλ‹€.")
        
    # νŠΉμ • μ‘°κ±΄μ—μ„œ μ˜ˆμ™Έ λ°œμƒμ‹œν‚€κΈ° (raise)
    x = -1
    if x < 0:
        raise ValueError("μŒμˆ˜λŠ” ν—ˆμš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.")

 

✨ 클래슀

🚩 μš”μ•½
 - 객체지ν–₯, 클래슀
    
🚩 λ‚΄μš©
1. 클래슀
 > 객체λ₯Ό λ§Œλ“€κΈ° μœ„ν•œ ꡬ쑰체
 > 속성과 λ©”μ†Œλ“œλ‘œ ꡬ성
  - 속성 : 클래슀의 μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ³€μˆ˜
  - λ©”μ†Œλ“œ : 클래슀의 λ™μž‘μ„ μ •μ˜
 > μž¬μ‚¬μš© κ°€λŠ₯ν•œ λ‹¨μœ„λ‘œ μΊ‘μŠν™”
 > μ½”λ“œμ˜ λͺ¨λ“ˆμ„± 및 μž¬μ‚¬μš©μ„± ν–₯상
 
🚩 μ‚¬μš©λ²•
1. 클래슀
    # 클래슀 생성
    class Student:
        # μ΄ˆκΈ°ν™” λ©”μ„œλ“œ
        def __init__(self, name, age):
            self.name = name  # μΈμŠ€ν„΄μŠ€ λ³€μˆ˜
            self.age = age    # μΈμŠ€ν„΄μŠ€ λ³€μˆ˜

        # λ‚΄μž₯ν•¨μˆ˜ (str: μ‚¬μš©μž μΉœν™”μ )
        def __str__(self):
            return f"Student(name={self.name}, age={self.age})"      
            
        # λ‚΄μž₯ν•¨μˆ˜ (repr: 개발자 디버깅, λ‘œκΉ… 유용)
        def __repr__(self):
            return f"Student('{self.name}', {self.age})"
            
        # λ‚΄μž₯ν•¨μˆ˜ (add: κ°μ²΄κ°„μ˜ μ—°μ‚° κ°€λŠ₯)
        def __add__(self, other):
        	return f"{self.name + other.name}, {self.age + other.age}"

        # λ©”μ„œλ“œ
        def intro(self):
            return f"{self.name} is {self.age} years old"

        # λ©”μ„œλ“œ
        def speak(self, sound):
            return f"{self.name} says {sound}"
  
    ❗ __init__
      > μ΄ˆκΈ°ν™” (=μƒμ„±μž) ν•¨μˆ˜
      > μΈμŠ€ν„΄μŠ€ 생성 μ‹œ, μžλ™μ‹€ν–‰ (μΈμŠ€ν„΄μŠ€ λ§€κ°œλ³€μˆ˜ μ„€μ •)
      > __init__ 을 λ§€κ°œλ³€μˆ˜κ°€ λ‹€λ₯΄κ²Œ λ‹€μ€‘μœΌλ‘œ μž‘μ„±ν•  경우, 
        λ§ˆμ§€λ§‰ __init__만 싀행됨 (인터프리터 μ–Έμ–΄μ΄λ―€λ‘œ)
    
    ❗ self
      > 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό κ°€λ¦¬ν‚€λŠ” μ°Έμ‘° λ³€μˆ˜

    πŸ’£ 문제
     > ex. intro ν•¨μˆ˜μ—μ„œ selfλ₯Ό λΉΌκ³  student.intro() μ‹€ν–‰ν•˜λ©΄ μ—λŸ¬
      - TypeError: Student.intro() takes 0 positional arguments but 1 was given
      - νŒŒλΌλ―Έν„°λ₯Ό μ•ˆ λ³΄λƒˆλŠ”λ°, 1κ°œκ°€ λ„˜μ–΄κ°”λ‹€κ³  함
      - μΈμŠ€ν„΄μŠ€μ—μ„œ 호좜 μ‹œ, 객체 자체λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ„˜κΉ€ (받을 λ³€μˆ˜ ν•„μš”: self)
        
2. μΈμŠ€ν„΄μŠ€
    # μΈμŠ€ν„΄μŠ€ 생성
    student = Student("Sparta", 30)
    
    # μΈμŠ€ν„΄μŠ€ 호좜
    print(student.intro()) # Sparta is 30 years old
    print(student.speak("Hello")) # Sparta says Hello
    
3. λ‚΄μž₯ν•¨μˆ˜ (μΌλΆ€λ§Œ 정리..)
    # __str__
     > 객체의 λ¬Έμžμ—΄ ν‘œν˜„μ„ μ •μ˜
     > μ‹€μ œ μ‚¬μš©μžλ“€μ΄ μ΄ν•΄ν•˜κΈ° νŽΈν•œ ν˜•νƒœλ‘œ κ°€κ³΅ν•˜μ—¬ 제곡
     > 호좜 : print(student) or print(str(student))

    # __repr__
     > 객체의 곡식적인 λ¬Έμžμ—΄ ν‘œν˜„μ„ μ •μ˜ν•˜λŠ”λ° μ‚¬μš©
     > κ°œλ°œμžκ°€ 객체의 상세 정보λ₯Ό λ‘œκΉ…ν•˜κ±°λ‚˜ 디버깅할 λ•Œ 유용
     > 호좜 : print(repr(student))
     
    # __add__
     > κ°μ²΄κ°„μ˜ add μ—°μ‚° κ°€λŠ₯
     > λ§€κ°œλ³€μˆ˜λ‘œ self와 otherλ₯Ό λ°›μŒ
      - student1 = Student("Sparta1", 30)
      - student2 = Student("Sparta2", 31)
     > 호좜 : print(student1 + student2) # 'Sparta1Sparta2', 61

 

 

🐢 λŠλ‚€μ 

νŒŒμ΄μ¬μ„ κ³΅λΆ€ν•˜λ©΄μ„œ, λ‚˜λ„ λͺ¨λ₯΄κ²Œ μžλ°”μ™€ λΉ„κ΅ν•˜λ©΄μ„œ ν•˜λ‹€λ³΄λ‹ˆ 쑰금 ν—·κ°ˆλ¦¬λŠ” 뢀뢄듀이 λ°œμƒν–ˆλ‹€.
κ·Έλž˜μ„œ 였늘 νŠœν„°λ‹˜λ“€μ˜ 도움을 톡해  ν—·κ°ˆλ¦¬λŠ” 뢀뢄을 잘 ν•΄μ†Œν•  수 μžˆμ–΄ μ’‹μ•˜λ˜ 것 κ°™λ‹€.
κ·Έλ‚˜μ €λ‚˜.. 12μ‹œκ°„ κ½€λ‚˜ λˆˆμ•Œμ΄λž‘ 머리 μ•„ν”„λ‹€..