sklearn.ensemble.RandomForestRegressor?
class sklearn.ensemble.RandomForestRegressor(n_estimators=100, *, criterion='mse', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, ccp_alpha=0.0, max_samples=None)
隨機森林回歸因子。
隨機森林是一種元估計器,它在數據集的不同子樣本上匹配多個分類決策樹,并使用均值來提高預測精度和控制過擬合。如果bootstrap=True
(默認),則使用max_samples
參數控制子樣本的大小,否則將使用整個數據集來構建每棵樹。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
n_estimators | int, default=100 森林中樹木的數量。 在版本0.22中更改:默認值 n_estimators 在0.22中從10更改為100。 |
criterion | {“mse”, “mae”}, default=”mse” 該函數用來測量分割的質量。支持的準則為均方誤差的“mse”,等于方差減少作為特征選擇準則,支持的準則為平均絕對誤差的“mae”。 版本0.18中的新功能:平均絕對誤差(MAE)標準。 |
max_depth | int, default=None 樹的最大深度。如果為None,則將節點展開,直到所有葉子都是純凈的,或者直到所有葉子都包含少于min_samples_split個樣本。 |
min_samples_split | int or float, default=2 拆分內部節點所需的最少樣本數: - 如果為int,則認為 min_samples_split 是最小值。- 如果為float, min_samples_split 則為分數, 是每個拆分的最小樣本數。ceil(min_samples_split * n_samples) 在版本0.18中更改:添加了分數的浮點值。 |
min_samples_leaf | int or float, default=1 在葉節點處需要的最小樣本數。僅在任何深度的分裂點在 min_samples_leaf 左分支和右分支中的每個分支上至少留下訓練樣本時才會被考慮。同時,這種情況可能具有平滑模型的效果,尤其是在回歸中。- 如果為int,則認為 min_samples_leaf 是最小值。- 如果為float, min_samples_leaf 則為分數, 是每個節點的最小樣本數。ceil(min_samples_leaf * n_samples) 在版本0.18中更改:添加了分數的浮點值。 |
min_weight_fraction_leaf | float, default=0.0 一個葉節點上所需的(所有輸入樣本的)總權重的最小加權分數。如果未提供sample_weight,則樣本的權重相等。 |
max_features | {“auto”, “sqrt”, “log2”}, int or float, default=”auto” 尋找最佳分割時要考慮的功能數量: - 如果為int,則 max_features 在每個分割處考慮特征。- 如果為float, max_features 則為小數,并 在每次拆分時考慮要素。- 如果為auto,則為 max_features=sqrt(n_features) 。- 如果是sqrt,則 max_features=sqrt(n_features) 。- 如果為log2,則為 max_features=log2(n_features) 。- 如果為None,則 max_features=n_features 。注意:直到找到至少一個有效的節點樣本分區,分割的搜索才會停止,即使它需要有效檢查多于 max_features 個數的要素也是如此。 |
max_leaf_nodes | int, default=Nonemax_leaf_nodes 以最好的方式進行“種樹”。雜質的相對減少的節點被當作最佳節點。如果為None,則葉節點數不受限制。 |
min_impurity_decrease | float, default=0.0 如果節點分裂會導致雜質的減少大于或等于該值,則該節點將被分裂。 加權減少雜質的方程式如下: N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity) 其中, N 是樣本總數,N_t 是當前節點上N_t_L 的樣本數,是左子節點中的樣本N_t_R 數,是右子節點中的樣本數。N ,N_t ,N_t_R 并且N_t_L 都指的是加權和,如果sample_weight 獲得通過。版本0.19中的新功能。 |
min_impurity_split | float, default=None 樹提升提前停止的閾值。如果節點的雜質高于閾值,則該節點將分裂,否則為葉。 - 從版本0.19 min_impurity_split 開始不推薦使用:在版本0.19中不再推薦使用 min_impurity_decrease 。的默認值 min_impurity_split 在0.23中從1e-7更改為0,并將在0.25中刪除。使用min_impurity_decrease 代替。 |
bootstrap | bool, default=False 創建樹時是否使用引導程序樣本。如果為False,則將整個數據集用于構建每棵樹。 |
oob_score | bool, default=False 是否使用袋外樣本估計泛化精度。 |
n_jobs | int, default=None 要并行運行的作業的數量。 fit , predict , decision_path 和 apply 都在樹中并行化。除非在一個joblib.parallel_backend 的內容中,否則None 在joblib 中的表示是1。-1表示使用所有處理器。有關更多詳細信息,請參見Glossary。 |
random_state | int, RandomState, default=None 控制3個隨機性來源: - 構建樹木時使用的示例的引導程序(如果 bootstrap=True )- 在每個節點上尋找最佳分割時要考慮的特征采樣(如果 max_features < n_features )- 繪制每個 max_features 的分割有關更多詳細信息,請參見Glossary。 |
verbose | int, default=0 在擬合和預測時控制冗余程度。 |
warm_start | bool, default=False 當設置為True時,重用前面調用的解決方案來適應并向集成添加更多的評估器,否則,只會擬合完整的新森林。有關更多詳細信息,請參見Glossary。 |
ccp_alpha | non-negative float, default=0.0 復雜度參數用于最小代價復雜度剪枝。將選擇代價復雜度最大且小于ccp_alpha的子樹。默認情況下,不執行修剪。 有關更多詳細信息,請參見Minimal Cost-Complexity Pruning。 0.22版中的新功能。 |
max_samples | int or float, default=None 如果bootstrap為真,則需要從X中抽取樣本來訓練每個基估計量。 - 如果為 None (默認),則繪制X.shape[0] 樣本。- 如果為 int ,則繪制 max_samples 樣本。- 如果為 float , 則繪制 max_samples * X.shape[0] 樣本。因此,max_samples應該在區間(0,1)內。0.22版本新增功能 |
屬性 | 說明 |
---|---|
base_estimator_ | DecisionTreeRegressor 子估計器模板,用于創建適合的子估計器的集合。 |
estimators_ | list of DecisionTreeRegressor 擬合的次估計值的集合。 |
feature_importances_ | ndarray of shape (n_features,) 基于雜質的特性重要性。 |
n_features_ | int 執行 fit 時的特征數。 |
n_outputs_ | int 執行 fit 時輸出的數量。 |
oob_score_ | float 使用袋外估計獲得的訓練數據集的得分。該屬性僅在 oob_score 為True 時存在。 |
oob_prediction_ | ndarray of shape (n_samples,) 用訓練集的袋外估計進行預測。該屬性僅在 oob_score 為真時存在。 |
另見
DecisionTreeRegressor
,ExtraTreesRegressor
注意
控制樹大小的參數的默認值(例如max_depth
, min_samples_leaf
等)會導致完全生長和未修剪的樹,在某些數據集上可能會非常大。為了減少內存消耗,應該通過設置這些參數值來控制樹的復雜性和大小。
在每次分割時,特性總是隨機地進行分配。因此,即使訓練數據相同,max_features=n_features
, bootstrap=False
,在搜索最佳分割的過程中,如果枚舉的幾個分割對準則的改進相同,那么找到的最佳分割也會有所不同。為了在擬合過程中獲得確定性行為,必須修復random_state
。
默認值max_features="auto"
使用的是n_features
而不是n_features / 3
。后者最初是在[1]
中提出的,而前者最近在[2]
中得到了經驗證明。
參考文獻
Breiman, “Random Forests”, Machine Learning, 45(1), 5-32, 2001.
P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.
實例
>>> from sklearn.ensemble import RandomForestRegressor
>>> from sklearn.datasets import make_regression
>>> X, y = make_regression(n_features=4, n_informative=2,
... random_state=0, shuffle=False)
>>> regr = RandomForestRegressor(max_depth=2, random_state=0)
>>> regr.fit(X, y)
RandomForestRegressor(...)
>>> print(regr.predict([[0, 0, 0, 0]]))
[-8.32987858]
方法
方法 | 說明 |
---|---|
apply (X) |
將森林中的樹應用于X,返回葉子索引。 |
decision_path (X) |
返回森林中的決策路徑。 |
fit (X, y[, sample_weight]) |
從訓練集(X, y)構建一個樹的森林。 |
get_params ([deep]) |
獲取此估計器的參數。 |
predict (X) |
為X預測回歸目標。 |
score (X, y[, sample_weight]) |
返回預測的決定系數R^2 |
set_params (**params) |
設置此估算器的參數。 |
__init__(n_estimators=100, *, criterion='mse', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, ccp_alpha=0.0, max_samples=None)
初始化的self。請參閱幫助(type(self))以獲得準確的簽名。
初始化self。有關準確的簽名,請參見help(type(self))
。
apply(X)
將森林中的樹應用于X,返回葉子索引。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 輸入樣本。在內部,它的 dtype 將被轉換為dtype=np.float32 。如果提供了一個稀疏矩陣,它將被轉換為一個csr_matrix 。 |
返回值 | 說明 |
---|---|
X_leaves | ndarray of shape (n_samples, n_estimators) 對于X中的每個數據點x和森林中的每棵樹,返回x最終所在的葉子的索引。 |
decision_path(X)
返回森林中的決策路徑。
版本0.18中的新功能。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 輸入樣本。在內部,它的 dtype 將被轉換為dtype=np.float32 。如果提供了一個稀疏矩陣,它將被轉換為一個csr_matrix 。 |
返回值 | 說明 |
---|---|
indicator | sparse matrix of shape (n_samples, n_nodes) 返回一個節點指示符矩陣,其中非零元素表示樣本經過節點。矩陣為CSR格式。 |
n_nodes_ptr | ndarray of shape (n_estimators + 1,) 列元素來自指示符 [n_nodes_ptr[i]:n_nodes_ptr[i+1]] 給出第i個估計器的指示值。 |
property feature_importances_
基于雜質的功能的重要性。
越高,功能越重要。特征的重要性計算為該特征帶來的標準的(標準化)總縮減。這也被稱為基尼重要性。
警告:基于雜質的特征重要性可能會誤導高基數特征(許多唯一值)。另見 sklearn.inspection.permutation_importance
。
返回值 | 說明 |
---|---|
feature_importances_ | ndarray of shape (n_features,) 除非所有樹都是僅由根節點組成的單節點樹,否則此數組的值總計為1,在這種情況下,它將是零數組。 |
fit(X, y, sample_weight = None)
根據訓練集(X, y)建立樹木森林。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 輸入樣本。在內部,它的 dtype 將被轉換為dtype=np.float32 。如果提供了一個稀疏矩陣,它將被轉換為一個csr_matrix 。 |
y | array-like of shape (n_samples,) or (n_samples, n_outputs) 目標值(分類中的類標簽,回歸中的實數)。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。如果沒有,那么樣本的權重相等。當在每個節點中搜索分割時,將忽略創建具有凈零權值或負權值的子節點的分割。在分類的情況下,如果分割會導致任何一個類在任一子節點中具有負權值,那么分割也將被忽略。 |
返回值 | 說明 |
---|---|
self | object |
get_params(deep=True)
獲取此估計器的參數。
參數 | 說明 |
---|---|
deep | bool, default=True 如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名稱與其值相對應。 |
predict(X)
預測X的分類。
輸入樣本的預測類是森林中樹的投票,由它們的概率估計加權。也就是說,預測的類是樹中概率估計均值最高的類。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix} of shape (n_samples, n_features) 輸入樣本。在內部,它的 dtype 將被轉換為dtype=np.float32 。如果提供了一個稀疏矩陣,它將被轉換為一個csr_matrix 。 |
返回值 | 說明 |
---|---|
y | ndarray of shape (n_samples,) or (n_samples, n_outputs) 被預測的分類 |
score(X, y, sample_weight=None)
返回預測的決定系數R^2。
決定系數R^2為(1 - u/v),其中u為((y_true - y_pred) ** 2).sum()的殘差平方和,v為(y_true - y_true.mean()) ** 2).sum()的平方和。最好的可能的分數是1.0,它可能是負的(因為模型可以任意地更糟)。常數模型總是預測y的期望值,而不考慮輸入特征,得到的R^2得分為0.0。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 對于某些估計器,這可能是一個預先計算的內核矩陣或一列通用對象,而不是 shape= (n_samples, n_samples_fitting) ,其中n_samples_fitting 是用于擬合估計器的樣本數量。 |
y | array-like of shape (n_samples,) or (n_samples, n_outputs) X的正確值。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
返回值 | 說明 |
---|---|
score | float self.predict(X) 關于y的準確率。 |
注意:
調用回歸變器的score
時使用的R2 score,與0.23版本的multioutput='uniform_average'中r2_score
的默認值保持一致。這影響了所有多輸出回歸的score
方法(除了MultiOutputRegressor
)。
set_params(**params)
設置該估計器的參數。
該方法適用于簡單估計器和嵌套對象(如pipline)。后者具有形式為<component>_<parameter>
的參數,這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估計器參數 |
返回值 | 說明 |
---|---|
self | object 估計實例。 |