Randomw Forest算法 python實現(xiàn),該系列文章主要是對常見的機器學習算法的實現(xiàn)。
完整的筆記和代碼以上傳到Github,地址為(覺得有用的話,歡迎Fork,請給作者個Star):

https://github.com/Vambooo/lihang-dl
隨機森林 Random Forest
隨機森林是對多棵樹組合對樣本訓練預測的一種分類器,它是Bagging方法的最流行的版本之一。
可以理解為隨機森林是個體模型為決策樹的Bagging算法。
隨機森林由Breiman提出的一種分類算法,它使用Bootstrap重采樣技術,從原始訓練樣本集中有放回的重復隨機抽取n個樣本生成新的樣本集合,以此作為訓練集來訓練決策樹。然后按照上述步驟生成m棵決策樹組合而成隨機森林。
隨機森林算法

Random Forest算法案例 python實現(xiàn)
(代碼可以左右滑動看)
第一步:構建數(shù)據(這里用make_blobs()來構建聚類數(shù)據)
X, y = make_blobs(n_samples=3000, centers=2, random_state=42, cluster_std=1.0)
n_samples是待生成的樣本的總數(shù);
n_features是每個樣本的特征數(shù);
centers表示類別數(shù);
cluster_std表示每個類別的方差。
from sklearn.datasets import make_blobs X, y = make_blobs(n_samples=300, centers=4, random_state=0, cluster_std=1.0)plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='rainbow');

定義樹的可視化方法
def visualize_tree(estimator, X, y, boundaries=True, xlim=None, ylim=None, ax=None): ax = ax or plt.gca() # 繪制訓練點 ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap='viridis', clim=(y.min(), y.max()), zorder=3) ax.axis('tight') ax.axis('off') if xlim is None: xlim = ax.get_xlim() if ylim is None: ylim = ax.get_ylim() # 擬合估計器 estimator.fit(X, y) xx, yy = np.meshgrid(np.linspace(*xlim, num=200), np.linspace(*ylim, num=200)) Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()]) # 將結果放入到帶顏色的圖中 n_classes = len(np.unique(y)) Z = Z.reshape(xx.shape) contours = ax.contourf(xx, yy, Z, alpha=0.3, levels=np.arange(n_classes + 1) - 0.5, cmap='viridis',zorder=1) ax.set(xlim=xlim, ylim=ylim) # 繪制決策邊界 def plot_boundaries(i, xlim, ylim): if i >= 0: tree = estimator.tree_ if tree.feature[i] == 0: ax.plot([tree.threshold[i], tree.threshold[i]], ylim, '-k', zorder=2) plot_boundaries(tree.children_left[i], [xlim[0], tree.threshold[i]], ylim) plot_boundaries(tree.children_right[i], [tree.threshold[i], xlim[1]], ylim) elif tree.feature[i] == 1: ax.plot(xlim, [tree.threshold[i], tree.threshold[i]], '-k', zorder=2) plot_boundaries(tree.children_left[i], xlim, [ylim[0], tree.threshold[i]]) plot_boundaries(tree.children_right[i], xlim, [tree.threshold[i], ylim[1]]) if boundaries: plot_boundaries(0, xlim, ylim)
定義分類器的可視化方法
def visualize_classifier(model, X, y, ax=None, cmap='rainbow'):
ax = ax or plt.gca()
# 繪制訓練點 ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cmap,
clim=(y.min(), y.max()), zorder=3) ax.axis('tight')
ax.axis('off') xlim = ax.get_xlim() ylim = ax.get_ylim()
# 擬合估計器 model.fit(X, y)
xx, yy = np.meshgrid(np.linspace(*xlim, num=200),
np.linspace(*ylim, num=200))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)
# 將擬合結果繪制在帶顏色的圖中
n_classes = len(np.unique(y))
contours = ax.contourf(xx, yy, Z, alpha=0.3,
levels=np.arange(n_classes + 1) - 0.5,
cmap=cmap,
zorder=1)
ax.set(xlim=xlim, ylim=ylim)
#定義可設置深度的決策樹分類器def depth_tree(depth=5): clf = DecisionTreeClassifier(max_depth=depth, random_state=0) visualize_tree(clf, X, y)
深度為1的決策樹分類器,分類效果
from sklearn.tree import DecisionTreeClassifiertree = DecisionTreeClassifier().fit(X, y)visualize_classifier(DecisionTreeClassifier(), X, y)

深度為5的決策樹分類器,分類效果
depth_tree(depth=5)
深度為10的決策樹分類器,分類效果
depth_tree(depth=10)

深度為15的決策樹分類器,分類效果
depth_tree(depth=15)

如上圖,當決策樹的深度不斷增加時,會出現(xiàn)不同的分類區(qū)域,比如當depth=10時,在黃色和藍色之間存在一條紫色區(qū)域,這塊數(shù)據應該是噪聲或者特定采樣的結果,這塊不能歸為紫色一類,這種現(xiàn)象其實就是過擬合。
可以通過組合多個分類器(這里是決策樹分類器)來減少這個種過擬合的影響。這也是Bagging的思想。
下面就是使用Bagging來組合100個DecisionTreeClassifier來進行測試。其中使用80%的數(shù)據來隨機化數(shù)據
from sklearn.tree import DecisionTreeClassifierfrom sklearn.ensemble import BaggingClassifier tree = DecisionTreeClassifier()bag = BaggingClassifier(tree, n_estimators=100, max_samples=0.8, random_state=1) bag.fit(X, y)visualize_classifier(bag, X, y)
也可以直接使用Scikit-learn中已定義好的RandomForestClassifier來實現(xiàn)
from sklearn.ensemble import RandomForestClassifier model = RandomForestClassifier(n_estimators=100, random_state=0)visualize_classifier(model, X, y);
-
機器學習
+關注
關注
66文章
8541瀏覽量
136208 -
python
+關注
關注
57文章
4856瀏覽量
89529 -
隨機森林
+關注
關注
1文章
22瀏覽量
4414
原文標題:機器學習筆記系列(十四) | Random Forest算法 python實現(xiàn)
文章出處:【微信號:AI_class_vip,微信公眾號:人工智能學研社】歡迎添加關注!文章轉載請注明出處。
發(fā)布評論請先 登錄
Python的Apriori算法和FP-Growth算法是什么
Python實現(xiàn)k-近鄰算法
BP神經網絡算法 python實現(xiàn)
大數(shù)據分析到底需要多少種工具_大數(shù)據分析總結
蟻群算法python編程實現(xiàn)
Python基礎教程之《Python機器學習—預測分析核心算法》免費下載
Python實現(xiàn)所有算法-基本牛頓法
基于Python實現(xiàn)隨機森林算法

Random Forest算法 python實現(xiàn)案例分析
評論