그래디언트 부스팅
Coddy Machine Learning 여정의 지도 학습 섹션에 포함된 레슨. 58개 중 43번째.
숲에서는 나무들이 나란히 자랍니다. 그래디언트 부스팅은 나무들을 하나씩 차례로 키웁니다. 새 나무는 이전 나무들이 여전히 틀리는 것을 학습합니다.
여기서는 스무디 바의 매출을 트리별로 예측합니다.
분류의 경우 모델은 GradientBoostingClassifier입니다:
from sklearn.ensemble import GradientBoostingClassifier
boost = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1, random_state=0)
boost.fit(X_train, y_train)learning_rate는 각 수정 사항을 얼마나 반영할지 설정합니다. 작은 단계는 신중하므로 더 많은 트리가 필요합니다.
챌린지
중급난초 재배자는 어떤 식물이 꽃을 피울지 알고 싶어 합니다. 시작 코드는 orchids.csv를 분할하고, 랜덤 포레스트의 점수를 계산하며 트리 개수를 읽습니다.
rates의 각 학습률에 대해 해당 개수의 트리로 부스팅을 학습하고(random_state=0) 테스트 정확도를 출력하세요.- 가장 좋은 학습률을 출력하세요. 동점이면 먼저 나온 학습률을 유지하세요.
다른 재배자와 20을 대상으로:
Forest: 0.860
Boosting at rate 0.01: 0.600
Boosting at rate 0.1: 0.860
Boosting at rate 1.0: 0.900
Best rate for 20 trees: 1.0그런 다음 트리 개수를 몇 가지 바꿔 보세요. 부스팅이 랜덤 포레스트보다 언제 더 좋은 성능을 보이나요?
직접 해보기
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
# 재배자의 선반에 있는 난초: 올해 꽃이 피었으면 (1), 피지 않았으면 (0)
orchids = pd.read_csv("orchids.csv")
X = orchids.drop(columns="bloomed")
y = orchids["bloomed"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
trees = int(input())
rates = [0.01, 0.1, 1.0]
# 아래 줄은 변경하지 마세요: 비교 대상인 랜덤 포레스트
forest = RandomForestClassifier(n_estimators=100, random_state=0)
forest.fit(X_train, y_train)
print(f"Forest: {forest.score(X_test, y_test):.3f}")
# 아래에 코드를 작성하세요
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
지도 학습의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러