# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import matplotlib.pyplot as plt

# +
from sklearn import datasets

X, y = datasets.make_moons(n_samples=500, noise=0.3, random_state=42)
# -

plt.scatter(X[y==0, 0], X[y==0, 1])
plt.scatter(X[y==1, 0], X[y==1, 1])
plt.show()

# +
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
# -

# ### AdaBoosting
#

# +
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier

ada_clf = AdaBoostClassifier(base_estimator = DecisionTreeClassifier(max_depth=2), n_estimators=500, random_state=666)
ada_clf.fit(X_train, y_train)
# -

ada_clf.score(X_test, y_test)

# ### Gradient Boosting
#
# > 1. 训练一个模型m1, 产生错误e1
# > 2. 针对e1训练第二个模型m2, 产生错误e2
# > 3. 以此类推，最终结果是 m1 + m2 + ...

# +
from sklearn.ensemble import GradientBoostingClassifier

gb_clf = GradientBoostingClassifier(max_depth=2, n_estimators=30, random_state=666)
gb_clf.fit(X_train, y_train)
# -

gb_clf.score(X_test, y_test)



StarcoderdataJupyterScriptsDedupFiltered