简体   繁体   中英

Using scikit-learn DecisionTreeClassifier to cluster

When using sklearn.tree.DecisionTreeClassifier, the classifier has methods for predicting probability and class.

Is there a way to use the same tree for clustering: for a given input vector x, simply tell which leaf x belongs to?

I found the answer to my own question - leaving it here as reference for the next time someone looks for it:

import numpy as np
import sklearn.tree
clf = sklearn.tree.DecisionTreeClassifier()
clf.fit(X,y)
clf.tree_.apply(np.asfortranarray(X.astype(sklearn.tree._tree.DTYPE)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM