Data Analysis
Medical Insurance 비용 변화 분석 - Python(함수 활용)
hyungminjeon
2025. 4. 1. 19:06
의료 보험 비용을 예측하는 Python 코드를 함수(calculate_insurance_cost)로 만들어 실습해보았다.
이 함수는 연령, 성별, 체질량지수(BMI), 자녀 수, 흡연 여부를 입력값으로 받아 보험료를 계산하고 출력한다.
calculate_insurance_cost() 함수 구현
# 보험 비용을 계산하는 함수 정의
def calculate_insurance_cost(age, sex, bmi, num_of_children, smoker, name):
estimated_cost = (250 * age) - (128 * sex) + (370 * bmi) + (425 * num_of_children) + (24000 * smoker) - 12500
print("The estimated insurance cost for " + name + " is " + str(estimated_cost) + " dollars.")
return estimated_cost
함수 설명
- 입력값
- age: 나이
- sex: 성별 (0 = 여성, 1 = 남성)
- bmi: 체질량지수 (Body Mass Index)
- num_of_children: 자녀 수
- smoker: 흡연 여부 (0 = 비흡연자, 1 = 흡연자)
- name: 이름
- 출력값
- 예상 보험 비용(달러 단위)
- 수식 해석
이 수식은 연령, 성별, BMI, 자녀 수, 흡연 여부에 따라 보험 비용을 계산하는 간단한 모델이다.
- 나이가 많을수록 보험료가 증가
- 남성(sex = 1)일 경우 보험료가 감소 (-128의 영향을 받음)
- BMI가 높을수록 보험료 증가
- 자녀 수가 많을수록 보험료 증가
- 흡연자의 경우 보험료가 급격히 증가 (+24,000)
- 기본 비용(-12500)을 설정하여 초기 값을 보정
calculate_insurance_cost() 함수 실행
이제 두 명의 사용자 Maria와 Omar의 보험료를 계산해보자.
Maria의 보험 비용 계산
maria_insurance_cost = calculate_insurance_cost(28, 0, 26.2, 3, 0, "Maria")
결과 출력
The estimated insurance cost for Maria is 5469.0 dollars.
Maria는 28세 여성, BMI 26.2, 자녀 3명, 비흡연자로 예상 보험료는 5,469달러이다.
Omar의 보험 비용 계산
omar_insurance_cost = calculate_insurance_cost(35, 1, 22.2, 0, 1, "Omar")
결과 출력
The estimated insurance cost for Omar is 20791.0 dollars.
Omar는 35세 남성, BMI 22.2, 자녀 없음, 흡연자이다.
흡연자로 인해 보험료가 크게 증가하여 20,791달러가 산출되었다.
결론
이번 실습을 통해 함수를 활용한 보험료 계산 자동화를 구현했다.
이 함수는 입력값만 변경하면 손쉽게 다양한 경우의 보험료를 예측할 수 있다.