5번) 이상치 탐지하기

- IQR = Q3 - Q1 (Q3 : 백분율 상위 25%, Q1 : 백분율 하위 25%)
- 상한 이상치 = Q3 + 1.5 * IQR
- 하한 이상치 = Q1 - 1.5 * IQR
heights = [157, 160, 153, 100, 186, 159, 169, 181, 179, 189, 300, 207, 157, 153, 166, 186, 189, 169, 180, 164]
import numpy as np
def calculate_iqr(data): #numpy 이용해 분위수 계산
data= np.array(data)
q1 = np.percentile(data, 25) # 25th percentile
q2 = np.percentile(data, 50) # 50th percentile
q3 = np.percentile(data, 75) # 75th percentile
iqr = q3-q1
upper_bound = q3 + 1.5 * iqr
lower_bound = q1 - 1.5 * iqr
outlier = []
for i in data:
if i >= upper_bound: # 상한 이상치
outlier.append(i)
elif i <= lower_bound: # 하한 이상치
outlier.append(i)
else:
pass # 둘 다 아닌 경우는 넘어가기
return q1, q2, q3, lower_bound, upper_bound, outlier
calculate_iqr(heights)
6번) 고객 데이터 관리 시스템

class Customer(): # 첫 글자는 대문자
def __init__(self):
self.name = ''
self.email = ''
self.points = 0
def join_customer(self, name, email, point):
self.name = name
self.email = email
self.points = point
def add_points(self, amount):
self.points = self.points + amount
print(f'정상적으로 추가되었습니다. 현재 포인트는 {self.points}')
def reduce_points(self, amount):
if amount > self.points:
self.points = 0
print(f'포인트를 모두 사용하였습니다. 현재 포인트는 {self.points}')
else:
self.points = self.points = amount
print(f'정상적으로 차감되었습니다. 현재 포인트는 {self.points}')
#확인코드
customer1 = Customer()
customer1.join_customer("Alice", "alice@example.com", 100)
customer1.add_points(50)
customer1.reduce_points(20)
customer1.reduce_points(150) # 포인트 부족 상황 테스트
'Python > 문제' 카테고리의 다른 글
| 파이썬 | 한 번만 등장한 문자, 인덱스 바꾸기 (0) | 2025.01.09 |
|---|---|
| 파이썬 | 합성수 찾기, 문자열 정렬하기(1) (0) | 2025.01.08 |
| 파이썬 과제 (0) | 2025.01.07 |
| 파이썬 과제 (0) | 2025.01.06 |