- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用带有 MLLib 的 Spark 1.5.1。我使用 MLLib 构建了一个随机森林模型,现在使用该模型进行预测。我可以使用 .predict 函数找到预测类别(0.0 或 1.0)。但是,我找不到检索概率的功能(请参阅随附的屏幕截图)。我认为 spark 1.5.1 随机森林会提供概率,我在这里遗漏了什么吗?
最佳答案
很遗憾,该功能在旧版 Spark MLlib 1.5.1 中不可用。
但是,您可以在 Spark MLlib 2.x 中最近的 Pipeline API 中找到它作为 RandomForestClassifier
:
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.RandomForestClassifier
import org.apache.spark.ml.feature.{IndexToString, StringIndexer, VectorIndexer}
import org.apache.spark.mllib.util.MLUtils
// Load and parse the data file, converting it to a DataFrame.
val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt").toDF
// Index labels, adding metadata to the label column.
// Fit on whole dataset to include all labels in index.
val labelIndexer = new StringIndexer()
.setInputCol("label")
.setOutputCol("indexedLabel").fit(data)
// Automatically identify categorical features, and index them.
// Set maxCategories so features with > 4 distinct values are treated as continuous.
val featureIndexer = new VectorIndexer()
.setInputCol("features")
.setOutputCol("indexedFeatures")
.setMaxCategories(4).fit(data)
// Split the data into training and test sets (30% held out for testing)
val Array(trainingData, testData) = data.randomSplit(Array(0.7, 0.3))
// Train a RandomForest model.
val rf = new RandomForestClassifier()
.setLabelCol(labelIndexer.getOutputCol)
.setFeaturesCol(featureIndexer.getOutputCol)
.setNumTrees(10)
// Convert indexed labels back to original labels.
val labelConverter = new IndexToString()
.setInputCol("prediction")
.setOutputCol("predictedLabel")
.setLabels(labelIndexer.labels)
// Chain indexers and forest in a Pipeline
val pipeline = new Pipeline()
.setStages(Array(labelIndexer, featureIndexer, rf, labelConverter))
// Fit model. This also runs the indexers.
val model = pipeline.fit(trainingData)
// Make predictions.
val predictions = model.transform(testData)
// predictions: org.apache.spark.sql.DataFrame = [label: double, features: vector, indexedLabel: double, indexedFeatures: vector, rawPrediction: vector, probability: vector, prediction: double, predictedLabel: string]
predictions.show(10)
// +-----+--------------------+------------+--------------------+-------------+-----------+----------+--------------+
// |label| features|indexedLabel| indexedFeatures|rawPrediction|probability|prediction|predictedLabel|
// +-----+--------------------+------------+--------------------+-------------+-----------+----------+--------------+
// | 0.0|(692,[124,125,126...| 1.0|(692,[124,125,126...| [0.0,10.0]| [0.0,1.0]| 1.0| 0.0|
// | 0.0|(692,[124,125,126...| 1.0|(692,[124,125,126...| [1.0,9.0]| [0.1,0.9]| 1.0| 0.0|
// | 0.0|(692,[129,130,131...| 1.0|(692,[129,130,131...| [1.0,9.0]| [0.1,0.9]| 1.0| 0.0|
// | 0.0|(692,[154,155,156...| 1.0|(692,[154,155,156...| [1.0,9.0]| [0.1,0.9]| 1.0| 0.0|
// | 0.0|(692,[154,155,156...| 1.0|(692,[154,155,156...| [1.0,9.0]| [0.1,0.9]| 1.0| 0.0|
// | 0.0|(692,[181,182,183...| 1.0|(692,[181,182,183...| [1.0,9.0]| [0.1,0.9]| 1.0| 0.0|
// | 1.0|(692,[99,100,101,...| 0.0|(692,[99,100,101,...| [4.0,6.0]| [0.4,0.6]| 1.0| 0.0|
// | 1.0|(692,[123,124,125...| 0.0|(692,[123,124,125...| [10.0,0.0]| [1.0,0.0]| 0.0| 1.0|
// | 1.0|(692,[124,125,126...| 0.0|(692,[124,125,126...| [10.0,0.0]| [1.0,0.0]| 0.0| 1.0|
// | 1.0|(692,[125,126,127...| 0.0|(692,[125,126,127...| [10.0,0.0]| [1.0,0.0]| 0.0| 1.0|
// +-----+--------------------+------------+--------------------+-------------+-----------+----------+--------------+
// only showing top 10 rows
注意:这个例子来自Spark MLlib的官方文档ML - Random forest classifier .
这里是一些输出列的一些解释:
predictionCol
表示预测的标签。rawPredictionCol
表示长度为 # 个类别的向量,其中包含进行预测的树节点处的训练实例标签计数(仅适用于分类)。probabilityCol
表示长度 # 个类别等于 rawPrediction
的概率向量,归一化为多项分布(仅适用于分类)。关于scala - Spark 1.5.1,MLLib 随机森林概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33401437/
使用 Spark1.6.0 MLLib,我将构建一个模型(如 RandomForest)并保存到 hdfs,然后可以从 hdfs 加载随机森林模型以在没有 SparkContext 的情况下进行预测。
我在某处读到 MLlib 本地向量/矩阵目前正在包装 Breeze 实现,但是将 MLlib 转换为 Breeze 向量/矩阵的方法是 org.apache.spark.mllib 范围内的私有(pr
我正在尝试在 Web 项目中使用 spark mllib.jar。我下载了 spark-1.1.0-bin-hadoop2.4 并解压。找到如下jar: datanucleus-api-jdi-3.2
在尝试使用 Python 使用 Spark mllib 的 LinearRegressionWithSGD 进行线性回归时,我一直得到非常糟糕的结果。 我调查了类似的问题,如下所示: Spark -
我使用 mllib 创建了 Apache Spark 机器学习管道。评估器结果是一个带有“概率”列的 DataFrame,它是概率的 mllib 向量(类似于 scikit-learn 中的 pred
我正在尝试使用 Spark 的 MLlib 在 Java 上实现 KMeans,我偶然发现了一个问题,那就是,尽管我导入了正确的 jar,但我的编译器无法识别这一行 // Cluster the da
我正在使用 Scala 对其进行编程,但语言在这里并不重要。 在这种情况下,隐式反馈协作过滤器 (ALS.trainImplicit) 的输入是产品的 View : Rating("user1", "
假设我有一个Array[RDD]类型的对象data。我想学习此对象中每个 RDD 上的独立机器学习模型。例如,对于随机森林: data.map{ d => RandomForest.trainRegr
我想运行 SVM 回归,但输入格式有问题。现在,我为一位客户设置的训练和测试集如下所示: 1 '12262064 |f offer_quantity:1 has_bought_brand_compan
我在 Spark 控制台中尝试了以下代码 import org.apache.spark.mllib.linalg.{Matrix, Matrices, DenseMatrix} val dm: De
Spark 2.0.0 中是否有任何预构建的异常值检测算法/四分位距识别方法?我在这里找到了一些代码,但我认为这在spark2.0.0中尚不可用 谢谢 最佳答案 如果您没有找到预构建的方法,您可以执行
我正在使用 pySpark MLlib 和开箱即用的 ALS 方法进行协同过滤。只是想知道,Spark 是否提供了其他一些进行过滤(用于计算距离)的方法,例如 Pearson 或 Cosine 的?可
您好,我是 spark mllib 的新手。我已经有一个 r 模型。我正在尝试使用 spark mllib 的相同模型。这里是 R 模型代码。 R 代码。 delhi data = sc.textF
我正在学习如何将机器学习与 Spark MLLib 结合使用,目的是对推文进行情感分析。我从这里得到了一个情绪分析数据集: http://thinknook.com/wp-content/upload
我有一个具有这种结构的小文件“naivebayetest.txt” 10 1:1 20 1:2 20 1:2 根据这些数据,我试图对 vector (1) 进行分类。如果我正确理解贝叶斯 (1) 的标
“spark mllib”提供的机器学习算法,如 naive byes、random forest 能否在 spark 集群中以并行模式运行?或者我们需要更改代码?请提供一个并行运行的例子?不确定 M
我正在使用 Spark 1.5.0 MLlib 随机森林算法(Scala 代码)进行二分类。由于我使用的数据集高度不平衡,因此多数类以 10% 的采样率进行下采样。 是否可以在 Spark 随机森林训
我正在使用 Spark MLlib 1.4.1 创建决策树模型。现在我想从决策树中提取规则。 如何提取规则? 最佳答案 您可以通过调用 model.toDebugString() 以字符串形式获取完整
我正在尝试使用 MlLib 进行协作过滤。 我在 Apache Spark 1.0.0 中运行 Scala 程序时遇到以下错误。 14/07/15 16:16:31 WARN NativeCod
我正在尝试在 Spark 中实现的协作过滤算法,并遇到以下问题: 假设我用以下数据训练模型: u1|p1|3 u1|p2|3 u2|p1|2 u2|p2|3 现在,如果我用以下数据测试它: u1|p1
我是一名优秀的程序员,十分优秀!