Skip to content

Commit

Permalink
Fixed the lookup of target category values for empty NodeClassificati…
Browse files Browse the repository at this point in the history
…onMap

If the winning Node element does not have any ScoreDistribution child
elements, then the method CategoricalResultFeature#getCategoryValues()
must return a singleton set that contains the value of the "score"
attribute.
  • Loading branch information
vruusmann committed Apr 23, 2015
1 parent 33c48ea commit d0b1dd8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.jpmml.evaluator;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -54,6 +55,13 @@ public String getResult(){

@Override
public Set<String> getCategoryValues(){

if(isEmpty()){
Node node = getEntity();

return Collections.singleton(node.getScore());
}

return keySet();
}

Expand All @@ -68,6 +76,15 @@ void putConfidence(String value, Double confidence){

@Override
public Double getProbability(String value){

if(isEmpty()){
Node node = getEntity();

if(value != null && (value).equals(node.getScore())){
return 1d;
}
}

return getFeature(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2015 Villu Ruusmann
*
* This file is part of JPMML-Evaluator
*
* JPMML-Evaluator is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JPMML-Evaluator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with JPMML-Evaluator. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jpmml.evaluator;

import com.google.common.collect.Sets;
import org.dmg.pmml.Node;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class NodeClassificationMapTest {

@Test
public void getProbability(){
Node node = new Node()
.withScore("ham");

NodeClassificationMap classificationMap = new NodeClassificationMap(node);

assertTrue(classificationMap.isEmpty());

assertEquals(Sets.newHashSet("ham"), classificationMap.getCategoryValues());

assertEquals((Double)1d, classificationMap.getProbability("ham"));
assertEquals((Double)0d, classificationMap.getProbability("spam"));

classificationMap.put("ham", 0.75d);
classificationMap.put("spam", 0.25d);

assertFalse(classificationMap.isEmpty());

assertEquals(Sets.newHashSet("ham", "spam"), classificationMap.getCategoryValues());

assertEquals((Double)0.75d, classificationMap.getProbability("ham"));
assertEquals((Double)0.25d, classificationMap.getProbability("spam"));
}
}

0 comments on commit d0b1dd8

Please sign in to comment.