Re:제로부터 시작하는 갓생
[Python] 25/01/21 베이직 실습5 본문
문제 1번
Seaborn 라이브러리에서 제공하는 Diamond 데이터셋을 활용하여 다음을 수행하세요:
1) x, y, z 중 하나라도 0인 데이터를 삭제하세요
x, y, z 를 곱해 'volume' 칼럼을 생성하세요.
범주형 변수인 cut, color, clarity를 머신러닝 모델에 활용할 수 있도록 수치형으로 변환합니다.
cut: Fair = 1, Good = 2, Very Good = 3, Premium = 4, Ideal = 5로 인코딩하세요.
color: E, I, J, H, F, G, D를 각각 1부터 7로 인코딩하세요.
clarity:
SI2와 SI1 → 1 (S 그룹),
VS1와 VS2 → 2 (VS 그룹),
VVS2와 VVS1 → 3 (VVS 그룹),
I1과 IF → 4 (I 그룹).
Skeleton code
diamonds = sns.load_dataset("diamonds") # Categorical -> String 변환 diamonds["cut"] = diamonds["cut"].astype(str) diamonds["color"] = diamonds["color"].astype(str) diamonds["clarity"] = diamonds["clarity"].astype(str) """ 코드 입력 """ # 결과 출력 print(diamonds.shape) print(diamonds.head())
출력결과
''' (53920, 11) carat cut color clarity depth table price x y z volume 0 0.23 5 1 1 61.5 55.0 326 3.95 3.98 2.43 38.202030 1 0.21 4 1 1 59.8 61.0 326 3.89 3.84 2.31 34.505856 2 0.23 2 1 2 56.9 65.0 327 4.05 4.07 2.31 38.076885 3 0.29 4 2 2 62.4 58.0 334 4.20 4.23 2.63 46.724580 4 0.31 2 3 1 63.3 58.0 335 4.34 4.35 2.75 51.917250 '''
* 정답
* 시도
더보기
2번까지는 괜찮은 듯 싶었다...
# 최종취합 시도 코드
diamonds = sns.load_dataset("diamonds")
# Categorical -> String 변환
diamonds["cut"] = diamonds["cut"].astype(str)
diamonds["color"] = diamonds["color"].astype(str)
diamonds["clarity"] = diamonds["clarity"].astype(str)
# 1) 0이 아닌 데이터 표시
diamonds = diamonds[(diamonds['x']!= 0) & (diamonds['y']!= 0) & (diamonds['z']!= 0) ]
# 2)
diamonds['volume'] = (diamonds['x'] * diamonds['y'] * diamonds['z'])
# 3)
diamonds['cut'] = {'Fair':1, 'Good':2, 'Very Good':3, 'Premium':4, 'Ideal':5}
diamonds['color'] = {'E':1, 'I':2, 'J':3, 'H':4, 'F':5, 'G':6, 'D':7}
diamonds['clarity'] = {'SI2':1,'SI1':1,'VS1':2,'VS2':2,'VVS2':3,'VVS1':3,'I1':4,'IF':5}
# 결과 출력
print(diamonds.shape)
print(diamonds.head())
문제 2번
다음 요구사항에 맞게 3개의 함수를 작성하세요.
학생들의 이름과 성적을 딕셔너리에 저장하고, 출력하는 함수를 작성하세요.
(add_student(grades, name, score))
성적 평균을 반환하는 함수를 작성하세요. (calculate_average(grades))
최고 점수 학생을 반환하는 프로그램 작성하세요. (find_top_students(grades))
출력결과를 참고하여, 같은 양식으로 출력되도록 함수를 작성하세요.
힌트: 딕셔너리 자료형에서 max와 min값 구하는 방법에 대해 필요한 경우, 아래 링크를 참고하세요.
https://note.nkmk.me/en/python-dict-value-max-min/
Skeleton code
def add_student(grades, name, score): """ 코드 작성 """ def calculate_average(grades): """ 코드 작성 """ def find_top_student(grades): """ 코드 작성 """ # Example usage grades = {} add_student(grades, "Alice", 85) add_student(grades, "Bob", 92) add_student(grades, "Charlie", 78) print(f"Average score: {calculate_average(grades):.2f}") top_student, top_score = find_top_student(grades) print(f"Top student: {top_student} with score {top_score}")
출력결과
""" Added Alice with score 85. Added Bob with score 92. Added Charlie with score 78. Average score: 85.00 Top student: Bob with score 92 """
* 정답
* 시도
배운점
'CodeKata > Task' 카테고리의 다른 글
[Python] 25/01/20 베이직 실습4 (0) | 2025.01.20 |
---|---|
[Python] 25/01/15 베이직 실습3 (0) | 2025.01.15 |
[Python] 25/01/13 베이직 실습2 (0) | 2025.01.13 |
[Python] 25/01/09 베이직 실습1 (0) | 2025.01.09 |