我试图使用随机森林在Python中执行集群。在随机森林的R实现中,可以设置一个标志来获得邻近矩阵。我似乎在python版本的随机森林中找不到类似的东西。有人知道python版本是否有等效的计算吗?
发布于 2013-09-10 12:42:32
我们还没有在Scikit学习中实现邻近矩阵。
但是,这可以通过依赖决策树实现中提供的apply
函数来实现。也就是说,对于数据集中的所有对样本,迭代林中的决策树(通过forest.estimators_
)并计算它们落在同一片叶子中的次数,即apply
为这两个样本提供相同节点id的次数。
希望这能有所帮助。
发布于 2017-12-20 14:10:54
根据吉勒·卢佩的回答,我写了一个函数。我不知道它是否有效,但有效。诚挚的问候。
def proximityMatrix(model, X, normalize=True):
terminals = model.apply(X)
nTrees = terminals.shape[1]
a = terminals[:,0]
proxMat = 1*np.equal.outer(a, a)
for i in range(1, nTrees):
a = terminals[:,i]
proxMat += 1*np.equal.outer(a, a)
if normalize:
proxMat = proxMat / nTrees
return proxMat
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
train = load_breast_cancer()
model = RandomForestClassifier(n_estimators=500, max_features=2, min_samples_leaf=40)
model.fit(train.data, train.target)
proximityMatrix(model, train.data, normalize=True)
## array([[ 1. , 0.414, 0.77 , ..., 0.146, 0.79 , 0.002],
## [ 0.414, 1. , 0.362, ..., 0.334, 0.296, 0.008],
## [ 0.77 , 0.362, 1. , ..., 0.218, 0.856, 0. ],
## ...,
## [ 0.146, 0.334, 0.218, ..., 1. , 0.21 , 0.028],
## [ 0.79 , 0.296, 0.856, ..., 0.21 , 1. , 0. ],
## [ 0.002, 0.008, 0. , ..., 0.028, 0. , 1. ]])
发布于 2017-04-25 21:23:54
目前还没有在python中实现这一点。我第一次尝试了,here。如果有人有兴趣将这些方法添加到scikit中,那就太好了。
https://stackoverflow.com/questions/18703136
复制相似问题