新聞中心
集成方法是將機(jī)器學(xué)習(xí)模型組合成更強(qiáng)大的機(jī)器學(xué)習(xí)模型的方法。 隨機(jī)森林是決策樹(shù)的集合,就是其中之一。 它比單一決策樹(shù)好,因?yàn)樵诒A纛A(yù)測(cè)能力的同時(shí),通過(guò)平均結(jié)果可以減少過(guò)度擬合。 在這里,我們將在 scikit 學(xué)習(xí)癌癥數(shù)據(jù)集上實(shí)施隨機(jī)森林模型。

創(chuàng)新互聯(lián),為您提供重慶網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站營(yíng)銷(xiāo)推廣、網(wǎng)站開(kāi)發(fā)設(shè)計(jì),對(duì)服務(wù)陽(yáng)臺(tái)護(hù)欄等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場(chǎng)的競(jìng)爭(zhēng)激烈,認(rèn)真對(duì)待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
導(dǎo)入必要的軟件包 -
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
import matplotlib.pyplot as plt
import numpy as np
現(xiàn)在,需要按照以下方式提供數(shù)據(jù)集
cancer = load_breast_cancer()
X_train, X_test, y_train,
y_test = train_test_split(cancer.data, cancer.target, random_state = 0)
在提供數(shù)據(jù)集之后,需要擬合可以如下完成的模型 -
forest = RandomForestClassifier(n_estimators = 50, random_state = 0)
forest.fit(X_train,y_train)
現(xiàn)在,獲得訓(xùn)練以及測(cè)試子集的準(zhǔn)確性:如果增加估計(jì)器的數(shù)量,那么測(cè)試子集的準(zhǔn)確性也會(huì)增加。
print('Accuracy on the training subset:(:.3f)',format(forest.score(X_train,y_train)))
print('Accuracy on the training subset:(:.3f)',format(forest.score(X_test,y_test)))
上面代碼,輸出結(jié)果如下所示 -
Accuracy on the training subset:(:.3f) 1.0
Accuracy on the training subset:(:.3f) 0.965034965034965
現(xiàn)在,與決策樹(shù)一樣,隨機(jī)森林具有 feature_importance 模塊,它將提供比決策樹(shù)更好的特征權(quán)重視圖。 它可以如下繪制和可視化 -
n_features = cancer.data.shape[1]
plt.barh(range(n_features),forest.feature_importances_, align='center')
plt.yticks(np.arange(n_features),cancer.feature_names)
plt.xlabel('Feature Importance')
plt.ylabel('Feature')
plt.show()
執(zhí)行上面代碼,得到以下輸出結(jié)果 -
當(dāng)前題目:創(chuàng)新互聯(lián)AI教程:AI人工智能隨機(jī)森林分類器
鏈接地址:http://fisionsoft.com.cn/article/dpgcjoc.html


咨詢
建站咨詢
