- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我希望使用 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)
我也很乐意听到可以解决我的问题的其他想法。谢谢!
最佳答案
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)
并使用rawPrediction
或probability
列:
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,那么构建全距离/相似度矩阵不太可能是个好主意。只是说。
关于python - 如何访问 Spark RandomForest 中的单个预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523065/
setwd("D:/Santander") ## import train dataset train0.9999] train2<-train[,! colnames(train) %in% c(e
当我运行这样的代码时,我对获得的不同结果感到困惑: set.seed(100) test1<-randomForest(BinaryY~., data=Xvars, trees=51, mtry=5,
当我通过拆分单个数据帧创建训练集和测试集并使用 randomForest 包构建随机森林时,对于训练数据中不存在的某些因子级别, predict() 函数仍然会抛出一个输出。虽然这没有给出错误(这是我
当尝试在因子水平低于我的训练数据的新测试数据上测试我的训练模型时,predict()返回以下内容: Type of predictors in new data do not match that o
我正在尝试使用 randomForest 进行分类,但我反复收到一条错误消息,似乎没有明显的解决方案(randomForest 在过去对我进行回归时效果很好)。我在下面粘贴了我的代码。 “成功”是一个
我想训练我的模型并选择最佳的树数量。代码在这里 from sklearn.ensemble import RandomForestClassifier tree_dep = [3,5,6] tree_
有谁知道 R randomForest 包用来解决分类关系的机制是什么 - 即当树最终在两个或多个类别中获得相同的投票时? 文档说领带是随机断开的。但是,当您在一组数据上训练模型,然后使用一组验证数据
我在使用 RandomForest 拟合函数时遇到问题 这是我的训练集 P1 Tp1 IrrPOA Gz Drz2 0
在 randomForest 包中实现的 R 随机森林算法对数字类和整数类的变量的行为是否相同? 最佳答案 是的,randomForest 实现确实对待整数和数字( double )类似。在 RF 模
重要性图: 我想将 y 轴文本向右对齐,并且还想根据不同的变量组为变量着色。例如 Limonene 和 Valencane,a-Selinene 和 g-Selinen 分别属于同一组。 但我在 "r
R 3.0.0 的新特性之一是引入了长向量。但是, .C() 和 .Fortran() 不接受长向量输入。在 R-bloggers我发现: This is a precaution as it is
我一直在用 R 对 700 万行数据(41 个特征)训练 randomForest 模型。这是一个示例调用: myModel <- randomForest(RESPONSE~., data=myda
我一直在尝试安装 R 包“randomForest”,为此我运行了通常的安装命令 install.packages("randomForest", dependencies = T) 这给了我以下错误
我正在尝试构建一个在线随机森林分类器。在 for 循环中,我遇到了一个错误,但找不到原因。 clf = RandomForestClassifier(n_estimators=1, warm_star
如果我运行 randomForest(y ~ x, data = df) 模型,x 会得到超过 53 个级别的因子变量 Error in randomForest.default(m, y, ...)
我在 R 中使用 randomForest 包来构建几个物种分布模型。我的响应变量是二元的(0 - 缺席或 1 存在),并且非常不平衡 - 对于某些物种,缺席:存在的比率是 37:1。这种不平衡(或零
我刚刚开始使用 Weka,当我的决策树太深时我无法理解。我有一组 423 个特征,据我所知,这些特征是为每个目标随机选择的。因此,这些特征子集生成带有决策叶的分支或流程,这些决策叶似乎不通用,实际上它
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我的数据高度不平衡,想要对少数类进行上采样以提高准确性(少数类是感兴趣的对象)。 我尝试在“randomForest”函数中使用“sampsize”选项 - 但它只允许下采样。我在某个地方读到,可以使
我有一个包含 10,000 行和两列的数据框、段(具有 32 个值的因子)和目标(具有两个值"is"和“否”的因子,每个值 5,000 个)。我正在尝试使用随机森林来使用分段作为特征对目标进行分类。
我是一名优秀的程序员,十分优秀!