2017-08-15 90 views
0

我是新的火花,我想用它随机森林分类器。 我使用libsvm格式的Iris数据来构建模型。火花随机森林分类器 - 获取标签为字符串

我的问题是 - 我怎样才能将标签作为字符串? (在这种情况下 - 标签是鸢尾花的类型)。

当数据转换为libsvm格式时,每个标签都会得到一个代表它的整数,但我不知道如何返回到字符串标签。

是否有可能与libsvm?或者我应该使用另一种格式?

这里是我的代码:

public PipelineModel runRandomForestAlgorithm(String dataPath) { 

System.setProperty("hadoop.home.dir", "C:/hadoop"); 
SparkSession spark = 
    SparkSession.builder().appName("JavaRandomForestClassifierExample").master("local[*]").getOrCreate(); 

/* Load and parse the data file, converting it to a DataFrame. */ 
DataFrameReader dataFrameReader = spark.read().format("libsvm"); 
Dataset<Row> data = dataFrameReader.load(dataPath); 

/* Index labels, adding metadata to the label column. 
    Fit on whole dataset to include all labels in index. */ 
StringIndexerModel 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. */ 
VectorIndexerModel featureIndexer = 
    new VectorIndexer().setInputCol("features").setOutputCol("indexedFeatures").setMaxCategories(4).fit(data); 

/* Split the data into training and test sets (30% held out for testing) */ 
Dataset<Row>[] splits = data.randomSplit(new double[]{0.9, 0.1}); 
Dataset<Row> trainingData = splits[0]; 
testData = splits[1]; 

/* Train a RandomForest model. */ 
RandomForestClassifier rf = 
    new RandomForestClassifier().setLabelCol("indexedLabel").setFeaturesCol("indexedFeatures").setNumTrees(10); 

/* Convert indexed labels back to original labels. */ 
IndexToString labelConverter = 
    new IndexToString().setInputCol("prediction").setOutputCol("predictedLabel").setLabels(labelIndexer.labels()); 

/* Chain indexers and forest in a Pipeline */ 
Pipeline pipeline = new Pipeline().setStages(new PipelineStage[]{labelIndexer, featureIndexer, rf, labelConverter}); 

/* Train model. This also runs the indexers. */ 
PipelineModel model = pipeline.fit(trainingData); 

/* Make predictions. */ 
Dataset<Row> predictions = model.transform(testData); 

/* Select example rows to display. */ 
List<Row> predictionAsRows = 
    predictions.select("predictedLabel", "label", "features", "rawPrediction", "probability").collectAsList(); 

predictionAsRows.forEach(row -> { 
    System.out.println("predictedLabel: " + row.get(0) + " , " + "label: " + row.get(1) + " , " + "features: " + row.get(2) + " , " + 
     "predictions: " + row.get(3) + " , " + "probabilities: " + row.get(4)); 
}); 

这里是输出:

predictedLabel: 1.0 , label: 1.0 , features: (4,[0,1,2,3], 
    [-0.833333,0.333333,-1.0,-0.916667]) , predictions: [10.0,0.0,0.0] , 
    probabilities: [1.0,0.0,0.0] 
    predictedLabel: 1.0 , label: 1.0 , features: (4,[0,1,2,3],         
    [-0.555556,0.166667,-0.830508,-0.916667]) , predictions: [10.0,0.0,0.0] 
    , probabilities: [1.0,0.0,0.0] 
    predictedLabel: 2.0 , label: 2.0 , features: (4,[0,1,2,3], 
    [-0.333333,-0.75,0.0169491,-4.03573E-8]) , predictions: [0.0,0.0,10.0] , 
    probabilities: [0.0,0.0,1.0] 
    predictedLabel: 2.0 , label: 2.0 , features: (4,[0,1,2,3], 
    [-0.166667,-0.416667,-0.0169491,-0.0833333]) , predictions: 
    [0.0,0.0,10.0] , probabilities: [0.0,0.0,1.0] 
    predictedLabel: 2.0 , label: 2.0 , features: (4,[0,1,2,3], 
    [0.166667,-0.25,0.118644,-4.03573E-8]) , predictions: [0.0,0.0,10.0] , 
    probabilities: [0.0,0.0,1.0] 
    predictedLabel: 2.0 , label: 2.0 , features: (4,[0,1,2,3], 
    [0.277778,-0.166667,0.152542,0.0833333]) , predictions: [0.0,0.0,10.0] , 
    probabilities: [0.0,0.0,1.0] 
    predictedLabel: 2.0 , label: 2.0 , features: (4,[0,2,3], 
    [0.5,0.254237,0.0833333]) , predictions: [0.0,0.0,10.0] , probabilities: 
    [0.0,0.0,1.0] 
    predictedLabel: 3.0 , label: 3.0 , features: (4,[0,1,2,3], 
    [-0.166667,-0.416667,0.38983,0.5]) , predictions: [0.0,9.875,0.125] ,   
    probabilities: [0.0,0.9875,0.0125] 
    predictedLabel: 3.0 , label: 3.0 , features: (4,[0,1,2,3], 
    [0.555555,-0.166667,0.661017,0.666667]) , predictions: [0.0,10.0,0.0] , 
    probabilities: [0.0,1.0,0.0] 
    predictedLabel: 3.0 , label: 3.0 , features: (4,[0,1,2,3], 
    [0.833333,-0.166667,0.898305,0.666667]) , predictions: [0.0,10.0,0.0] , 
    probabilities: [0.0,1.0,0.0] 
    predictedLabel: 3.0 , label: 3.0 , features: (4,[0,2,3], 
    [0.222222,0.38983,0.583333]) , predictions: [0.0,10.0,0.0] , 
    probabilities: [0.0,1.0,0.0] 
    predictedLabel: 3.0 , label: 3.0 , features: (4,[0,2,3], 
    [0.388889,0.661017,0.833333]) , predictions: [0.0,10.0,0.0] , probabilities: [0.0,1.0,0.0] 

回答

0

使用SVM的格式,你只能得到每个类的整数,所以你不能得到一个字符串从那里的类标签。

您可以通过使用setLabels()方法来使用IndexToString()转换器。只需输入您拥有的标签数组。为此,您应该删除StringIndexerModel()(因为类是数字而不是字符串,所以不需要)。例如:

String[] labels = {"Setosa", "Versicolor", "Virginica"}; 
IndexToString labelConverter = new IndexToString().setInputCol("prediction").setOutputCol("pred‌​ictedLabel").setLabe‌​ls(labels); 

或者,您可以在那里你映射整数到字符串标签创建一个单独的Map。对于虹膜数据集可能是这样的:

Map labels = new HashMap(); 
labels.put(1, "Setosa"); 
labels.put(2, "Versicolour"); 
labels.put(3, "Virginica"); 

然后你就可以使用这个Map得到串标记所有Spark转换完成后。

希望它有帮助。

+0

地图可能非常有用,但我不知道如何将此地图加入Spark对象。我添加了这些线条,并且帮助了很多:'String [] labels = new String [] {“Iris-Setosa”,“Iris-versicolor”,“Iris-virginica”}; IndexToString stringConverter = new IndexToString()。setLabels(labels); /*将索引标签转换回原始标签。 */ IndexToString labelConverter = new IndexToString()。setInputCol(“prediction”)。setOutputCol(“predictedLabel”)。setLabels(stringConverter.getLabels());' – Shimrit

+0

@Shimrit'Map'只能在完成所有转换后单独使用,因此,'IndexToString()'更喜欢。我会更新答案以反映这一点。请考虑通过点击复选标记来接受答案,如果它对你有帮助。 :) – Shaido