如何访问Spark RandomForest中的各个预测?

时间:2016-02-20 12:11:03

标签: python apache-spark pyspark apache-spark-mllib random-forest

我希望使用pyspark.mllib.tree.RandomForest模块来获取观察的邻近矩阵。

到目前为止,我的数据足够小,可以直接加载到内存中。因此,我使用sklearn.ensemble.RandomForestClassifier以下列方式获取邻近矩阵:假设X是包含要素的矩阵,Y是包含标签的向量。我训练随机森林来区分标签为“0”和标签为“1”的对象。拥有经过训练的随机森林,我想通过计算两个观测结果得到相同的最终节点(=叶子)的决策树数量来获得我的数据集中每对观测值之间的接近程度。因此,对于100个决策树,两个观测值之间的接近度量可以是0(从不落在同一个最终叶子中)和100(在所有决策树中已落到相同的最终叶子)。 python的实现:

import numpy
from sklearn import ensemble

## data
print X.shape, Y.shape # X is a matrix that holds the 4281 features and contains 8562 observations and Y contains 8562 labels
>> (8562, 4281) (8562,)

## train the tree
n_trees = 100
rand_tree = sklearn.ensemble.RandomForestClassifier(n_estimators=n_tress)
rand_tree.fit(X, Y)

## get proximity matrix
apply_mat = rand_tree.apply(X)
obs_num = len(apply_mat)
sim_mat = numpy.eye(obs_num) * len(apply_mat[0]) # max values that they can be similar at = N estimators

for i in xrange(obs_num):
    for j in xrange(i, obs_num):
        vec_i = apply_mat[i]
        vec_j = apply_mat[j]
        sim_val = len(vec_i[vec_i==vec_j])
        sim_mat[i][j] = sim_val
        sim_mat[j][i] = sim_val

sim_mat_norm = sim_mat / len(apply_mat[0])
print sim_mat_norm.shape
>> (8562, 8562)

现在,我处理的数据太大而无法容纳在内存中,因此我决定使用Spark。我能够加载数据并使其适合但我没有找到一种方法将随机森林“应用”到数据中以获得邻近矩阵。有没有办法得到它? (我使用与Spark文档中相同的实现:https://spark.apache.org/docs/1.2.0/mllib-ensembles.html#classification):

from pyspark.mllib.tree import RandomForest
from pyspark.mllib.util import MLUtils

# Load and parse the data file into an RDD of LabeledPoint.
data = MLUtils.loadLibSVMFile(sc, 'data/mllib/sample_libsvm_data.txt')
# Split the data into training and test sets (30% held out for testing)
(trainingData, testData) = data.randomSplit([0.7, 0.3])

model = RandomForest.trainClassifier(trainingData, numClasses=2, categoricalFeaturesInfo={},
                                 numTrees=3, featureSubsetStrategy="auto",
                                 impurity='gini', maxDepth=4, maxBins=32)

我也很乐意听到其他可以解决我问题的想法。 谢谢!

1 个答案:

答案 0 :(得分:4)

PySpark MLlib模型不提供访问此信息的直接方法。从理论上讲,您可以尝试直接提取模型并单独预测每棵树:

from pyspark.mllib.tree import DecisionTreeMode

numTrees = 3
trees = [DecisionTreeModel(model._java_model.trees()[i])
    for i in range(numTrees)]

predictions = [t.predict(testData) for t in trees]

但是使用ML模型可能更好:

from pyspark.ml.feature import StringIndexer
from pyspark.ml.classification import RandomForestClassifier

df = sqlContext.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")

indexer = StringIndexer(inputCol="label", outputCol="indexed").fit(df)
df_indexed = indexer.transform(df)

model = RandomForestClassifier(
    numTrees=3, maxDepth=2, labelCol="indexed", seed=42
).fit(df_indexed)

并使用rawPredictionprobability列:

model.transform(df).select("rawPrediction", "probability").show(5, False)

## +---------------------------------------+-----------------------------------------+
## |rawPrediction                          |probability                              |
## +---------------------------------------+-----------------------------------------+
## |[0.0,3.0]                              |[0.0,1.0]                                |
## |[2.979591836734694,0.02040816326530612]|[0.9931972789115647,0.006802721088435374]|
## |[2.979591836734694,0.02040816326530612]|[0.9931972789115647,0.006802721088435374]|
## |[2.979591836734694,0.02040816326530612]|[0.9931972789115647,0.006802721088435374]|
## |[2.979591836734694,0.02040816326530612]|[0.9931972789115647,0.006802721088435374]|
## +---------------------------------------+-----------------------------------------+

注意:如果您认为您的数据需要Spark,那么构建全距离/相似度矩阵不太可能是个好主意。只是说。