sklearn.tree.ExtraTreeClassifier?

class sklearn.tree.ExtraTreeClassifier(*, criterion='gini', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, ccp_alpha=0.0)

[源碼]

極為隨機的樹分類器。

額外樹在構建方式上與經典決策樹不同。當尋找最佳分割以將節點的樣本分為兩組時,將為每個max_features隨機選擇的特征繪制隨機分割,然后在這些特征中選擇最佳分割。當 max_features設置為1時,這等于建立一個完全隨機的決策樹。

警告:額外樹只能在集成方法中使用。

用戶指南中閱讀更多內容。

參數 說明
criterion {“gini”, “entropy”}, default=”gini”
該函數用來測量分割的質量。支持的標準基尼不純度為“基尼系數”和信息增益為“熵系數”。
splitter {“random”, “best”}, default=”random”
用于在每個節點選擇分割的策略。支持的策略是“最佳”選擇最佳分割和“隨機”選擇最佳隨機分割。
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 int, float, {“auto”, “sqrt”, “log2”} or None, default=”auto”
當尋找最好的分裂時考慮的特征的數量:
- 如果是整數,則每次使用max_features個特征切分
- 如果是float,則max_features是一個分數,并且每次使用int(max_features * n_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的特性。
random_state int, RandomState instance, default=None
用于隨機選擇每個拆分中使用的max_features。 有關詳細信息,請參見詞匯表
max_leaf_nodes int, default=None
以最佳優先的方式生成具有max_leaf_nodes的樹。最佳節點定義為雜質的相對減少。如果沒有,則無限的葉節點數。
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為右子節點的樣本數。
如果sample_weight被傳遞,N、N_t、N_t_R和N_t_L都指向加權和。
min_impurity_split float, (default=0)
樹木停止生長的閾值。如果一個節點的不純度超過閾值,那么它就會分裂,否則它就是葉子。
警告: 從版本0.19開始被棄用:min_impurity_split在0.19中被棄用,轉而支持min_impurity_decrease。min_impurity_split的默認值在0.23中從1e-7更改為0,在0.25中將被刪除。使用min_impurity_decrease代替。
class_weight dict, list of dict or “balanced”, default=None
{class_label: weight}的形式表示與類別關聯的權重。如果取值None,所有分類的權重為1。對于多輸出問題,可以按照y的列的順序提供一個字典列表。
注意多輸出(包括多標簽) ,應在其自己的字典中為每一列的每個類別定義權重。例如:對于四分類多標簽問題, 權重應為[{0:1、1:1:1],{0:1、1:5},{0:1、1:1:1},{0:1、1: 1}],而不是[{1:1},{2:5},{3:1},{4:1}]
“平衡”模式使用y的值自動將權重與輸入數據中的類頻率成反比地調整為n_samples /(n_classes * np.bincount(y))
對于多輸出,y的每一列的權重將相乘。
請注意,如果指定了sample_weight,則這些權重將與sample_weight(通過fit方法傳遞)相乘。
ccp_alpha non-negative float, default=0.0
用于最小化成本復雜性修剪的復雜性參數。 將選擇成本復雜度最大且小于ccp_alpha的子樹。 默認情況下,不執行修剪。 有關詳細信息,請參見最小成本復雜性修剪。

0.22版中的新功能。
屬性 說明
classes_ ndarray of shape (n_classes,) or list of ndarray
類標簽(單一輸出問題)或類標簽數組列表(多輸出問題)。
max_features_ int
max_features的推斷值。
n_classes_ int or list of int
類的數量(用于單個輸出問題),或包含每個輸出的類數量的列表(用于多輸出問題)。
feature_importances_ ndarray of shape (n_features,)
返回特征重要性
n_features_ int
模型訓練過程特征數量
n_outputs_ int
模型訓練時輸出的數量。
tree_ Tree
底層樹對象。請參考幫助(sklearn.tree._tree.Tree)了解樹對象的屬性和了解這些屬性的基本用法的決策樹結構。

另見

控制樹大小的參數的默認值(例如max_depth, min_samples_leaf等)會導致完全生長和未修剪的樹,在某些數據集上可能會非常大。為了減少內存消耗,應該通過設置這些參數值來控制樹的復雜性和大小。

參考

P. Geurts, D. Ernst., and L. Wehenkel, “Extremely randomized trees”, Machine Learning, 63(1), 3-42, 2006.

示例

>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.ensemble import BaggingClassifier
>>> from sklearn.tree import ExtraTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(
...    X, y, random_state=0)
>>> extra_tree = ExtraTreeClassifier(random_state=0)
>>> cls = BaggingClassifier(extra_tree, random_state=0).fit(
...    X_train, y_train)
>>> cls.score(X_test, y_test)
0.8947...

方法

方法 說明
apply(X[, check_input]) 返回每個樣本預測為葉的葉索引。
cost_complexity_pruning_path(X, y[, …]) 在最小代價復雜度剪枝過程中計算剪枝路徑。
decision_path(X[, check_input]) 返回樹中的決策路徑.
fit(X, y[, sample_weight, check_input, …]) 從訓練集(X, y)構建決策樹分類器。
get_depth() 返回決策樹的深度。
get_n_leaves() 返回決策樹的葉節點數。
get_params([deep]) 獲取這個估計器的參數。
predict(X[, check_input]) 預測X的類或回歸值。
predict_log_proba(X) 預測輸入樣本X的類別對數概率。
predict_proba(X[, check_input]) 預測輸入樣本X的類概率。
score(X, y[, sample_weight]) 返回給定測試數據和標簽的平均精度。
set_params(**params) 設置這個估計器的參數。
__init__(*, criterion='gini', splitter='random', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, class_weight=None, ccp_alpha=0.0)

[源碼]

初始化自身實例。請參閱幫助(type(self))以獲得準確的簽名。

apply(X, check_input=True)

[源碼]

返回每個樣本預測為葉的葉索引。

在 0.17 版中新增

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
check_input bool, default=True
允許繞過多個輸入檢查。 除非您知道自己要做什么,否則不要使用此參數。
返回值 說明
X_leaves array-like of shape (n_samples,)
對于X中的每個數據點x,返回x結尾的葉子的索引。葉子編號在[0; self.tree_.node_count),可能在編號上有間隔。
cost_complexity_pruning_path(X, y, sample_weight=None)

[源碼]

在最小代價復雜度剪枝過程中計算剪枝路徑。

有關修剪過程的詳細信息,請參見最小成本復雜性修剪

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
目標值(類標簽)為整數或字符串。
sample_weight array-like of shape (n_samples,), default=None
樣本權重。如果為None,那么樣本的權重相等。當在每個節點中搜索分割時,將忽略創建具有凈零權值或負權值的子節點的分割。如果分割會導致任何一個類在任一子節點中具有負權值,那么分割也將被忽略。
返回值 說明
ccp_path Bunch
類字典的對象,具有以下屬性。
ccp_alphas : ndarray
修剪期間子樹的有效Alpha。

impurities : ndarray
ccp_alphas中對應alpha值的子樹葉子的不純度之和。
decision_path(X, check_input=True)

[源碼]

返回樹中的決策路徑。

0.18.版中新增功能

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
check_input bool, default=True
允許繞過幾個輸入檢查。除非您知道自己在做什么,否則不要使用此參數。
返回值 說明
indicator sparse matrix of shape (n_samples, n_nodes)
返回一個節點指示器CSR矩陣,其中非零元素表示樣本經過節點。
property feature_importances_

返回特性的重要性。

一個特征的重要性被計算為該特征所帶來的標準的(歸一化)總約簡。它也被稱為基尼重要性。

警告:針對高基數特性(許多唯一值),基于雜質的特性重要性可能會引起誤解。 另請參見sklearn.inspection.permutation_importance

返回值 說明
feature_importances_ ndarray of shape (n_features,)
按特征(基尼重要性)標準的標準化總約簡。
fit(X, y, sample_weight=None, check_input=True, X_idx_sorted=None)

[源碼]

從訓練集(X, y)構建決策樹分類器。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
y array-like of shape (n_samples,) or (n_samples, n_outputs)
目標值(類標簽)為整數或字符串。
sample_weight array-like of shape (n_samples,), default=None
樣本權重。如果為None,則對樣本進行平均加權。 在每個節點中搜索拆分時,將忽略創建凈凈值為零或負權重的子節點的拆分。 如果拆分會導致任何單個類在任一子節點中都負負重,則也將忽略拆分。
check_input bool, default=True
允許繞過多個輸入檢查。 除非您知道自己要做什么,否則不要使用此參數。
X_idx_sorted array-like of shape (n_samples, n_features), default=None
排序后的訓練輸入樣本的索引。 如果在同一數據集上生長了許多樹,則可以在樹之間緩存順序。 如果為None,則將在此處對數據進行排序。 除非您知道要做什么,否則不要使用此參數。
返回值 說明
self DecisionTreeClassifier
訓練出來的模型
get_depth()

[源碼]

返回決策樹的深度。

一棵樹的深度是根與任何葉子之間的最大距離。

參數 說明
self.tree_.max_depth int
樹的最大深度。
get_n_leaves()

[源碼]

返回決策樹的葉子數目。

返回值 說明
self.tree_.n_leaves int
葉子數量
get_params(deep=True)

[源碼]

? 獲取此模型的參數

參數 說明
deep bool, default=True
如果為True,則將返回此估算器以及估算器所包含子對象的參數。
返回值 說明
params mapping of string to any
參數名與值的映射
predict(X, check_input=True)

[源碼]

預測X的類或回歸值。

對于分類模型,返回X中每個樣本的預測類。 對于回歸模型,將返回基于X的預測值。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
check_input bool, default=True
允許繞過多個輸入檢查。 除非知道自己要做什么,否則不要使用此參數。
返回值 說明
y array-like of shape (n_samples,) or (n_samples, n_outputs)
預測的類或預測值。
predict_log_proba(X)

[源碼]

預測輸入樣本X的類別對數概率。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
返回值 說明
proba ndarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1
輸入樣本的類別對數概率。 類的順序與屬性classes_中的順序相對應。
predict_proba(X, check_input=True)

[源碼]

預測輸入樣本X的類別概率。

預測的類別概率是葉子中相同類別的樣本的分數。

參數 說明
X {array-like, sparse matrix} of shape (n_samples, n_features)
訓練輸入樣本。在內部,它將被轉換為' dtype=np 并且如果傳入的是稀疏矩陣則轉換為csr_matrix矩陣。
check_input bool, default=True
允許繞過多個輸入檢查。 除非知道自己要做什么,否則不要使用此參數。
返回值 說明
proba ndarray of shape (n_samples, n_classes) or list of n_outputs such arrays if n_outputs > 1
輸入樣本的分類概率。 類的順序與屬性classes_中的順序相對應。
score(X, y, sample_weight=None)

[源碼]

返回給定測試數據與標簽的平均準確度。

在多標簽分類中,這是子集準確度,這是一個苛刻的指標,因為您需要為每個樣本正確預測每個標簽集。

參數 說明
X array-like of shape (n_samples, n_features)
測試樣本
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
真實值與與測試值的平均準確度。
set_params(**params)

[源碼]

設置此估算器的參數。

該方法適用于簡單的估計器以及嵌套對象(例如管道)。 后者的參數格式為<component> __ <parameter>,以便可以更新嵌套對象的每個組件。

參數 說明
**params dict
估計器參數。
返回值 說明
self object
估計器實例。