sklearn 決策迴歸樹擬合正弦曲線

# 先構造正弦函數 + 噪音
import numpy as np

rng = np.random.RandomState(1)
X = np.sort(5 * rng.rand(80, 1), axis=0)  # 生成 0~5之間隨機數據,並排序,因爲 sklearn用二維特徵數據,所以定義數據時候直接定義爲二維
y = np.sin(X).ravel()  # 對數據進行降維,sklearn標籤數據是一維的
y[::5] += 3 * (0.5 - rng.rand(16))  # 添加噪音
# 畫出圖像
import matplotlib.pyplot as plt

plt.scatter(X, y)
<matplotlib.collections.PathCollection at 0x24fda557b08>

png

# 訓練兩個不同深度的迴歸模型
from sklearn.tree import DecisionTreeRegressor

regr1 = DecisionTreeRegressor(max_depth=2)
regr2 = DecisionTreeRegressor(max_depth=10)
regr1.fit(X, y)
regr2.fit(X, y)
DecisionTreeRegressor(criterion='mse', max_depth=10, max_features=None,
                      max_leaf_nodes=None, min_impurity_decrease=0.0,
                      min_impurity_split=None, min_samples_leaf=1,
                      min_samples_split=2, min_weight_fraction_leaf=0.0,
                      presort=False, random_state=None, splitter='best')
# 構建測試數據集
X_test = np.arange(0, 5.0, 0.01)[:, np.newaxis]  # 數據轉換爲 二維
y_1 = regr1.predict(X_test)
y_2 = regr2.predict(X_test)
# 畫出兩個模型擬合的圖像
plt.figure(num='title1', figsize=(16, 4))
plt.scatter(X_test, y_1, c='red')
plt.title('y1', size=15)
plt.xlabel('x', size=15)
plt.ylabel('y', size=15)

plt.figure(num='title2', figsize=(16, 4))
plt.scatter(X_test, y_2, c='green')
plt.title('y2', size=15)
plt.xlabel('x', size=15)
plt.ylabel('y', size=15)
Text(0, 0.5, 'y')

png

png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章