![](/img/trans.png)
[英]How to hand-engineer features of TfidfVectorizer in Scikit-learn?
[英]How can I create a scikit-learn tree by hand?
為了測試一些代碼,我希望能夠手動創建一個 sklearn.tree._tree.Tree,而不是通過擬合一些數據。
為了具體起見,假設我想要一棵樹,將實線中的點分類為間隔(-infinity, 5], (5,6] or (6,infinity))。我想要樹的形狀像
----0----
| |
| ---2---
| | |
1 3 4
其中節點 0 在 5 處分割實線,節點 2 在 6 處分割實線。
這該怎么做? 我看到樹有一個__setstate__
方法,並且查看__getstate__
的輸出,看起來我需要類似的東西
state = {
'n_features_': 1,
'max_depth': 2,
'node_count': 5,
'nodes': np.array([(1 , 2, 0, 5., 0.375, 3, 3.),
(-1, -1, 0, -2., 0. , 1, 1.),
(3 , 4, 0, 6., 0., , 2, 2.),
(-1, -1, 0, -2., 0., , 1, 1.),
(-1, -1, 0, -2., 0., , 1, 1.),
],
dtype=[('left_child', '<i8'), ('right_child', '<i8'), ('feature', '<i8'),('threshold', '<f8'), ('impurity', '<f8'), ('n_node_samples', '<i8'), ('weighted_n_node_samples', '<f8')]),
}
但是我真的不明白這些參數是什么意思,無論如何我一開始都不知道如何用這種狀態初始化一棵樹。
經過幾個小時的嘗試手動更改節點。 我找到了解決方案。 確實,你是對的。 通過使用setstate,您可以進行樹定制。 “節點”鍵必須如下:
-1(左/右孩子)和 -2(特征)代表葉子。
訓練分類器時,您將擁有另一個鍵:“值”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.