-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for the 'ObliqueRandomForestClassifier' model type
- Loading branch information
Showing
13 changed files
with
2,304 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
pmml-sklearn-extension/src/main/java/sktree/ensemble/forest/ObliqueForestUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2024 Villu Ruusmann | ||
* | ||
* This file is part of JPMML-SkLearn | ||
* | ||
* JPMML-SkLearn 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-SkLearn 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-SkLearn. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package sktree.ensemble.forest; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.dmg.pmml.MiningFunction; | ||
import org.dmg.pmml.mining.MiningModel; | ||
import org.dmg.pmml.mining.Segmentation; | ||
import org.dmg.pmml.tree.TreeModel; | ||
import org.jpmml.converter.ModelUtil; | ||
import org.jpmml.converter.Schema; | ||
import org.jpmml.converter.mining.MiningModelUtil; | ||
import sklearn.Estimator; | ||
import sklearn.HasEstimatorEnsemble; | ||
import sklearn.tree.HasTree; | ||
|
||
public class ObliqueForestUtil { | ||
|
||
private ObliqueForestUtil(){ | ||
} | ||
|
||
static | ||
public <E extends Estimator & HasEstimatorEnsemble<T>, T extends Estimator & HasTree> MiningModel encodeBaseObliqueForest(E estimator, MiningFunction miningFunction, Segmentation.MultipleModelMethod multipleModelMethod, Schema schema){ | ||
List<? extends T> estimators = estimator.getEstimators(); | ||
|
||
Schema segmentSchema = schema.toAnonymousSchema(); | ||
|
||
Function<T, TreeModel> function = new Function<T, TreeModel>(){ | ||
|
||
@Override | ||
public TreeModel apply(T estimator){ | ||
int i = estimators.indexOf(estimator); | ||
|
||
if(i < 0){ | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
return (TreeModel)estimator.encode((i + 1), segmentSchema); | ||
} | ||
}; | ||
|
||
List<TreeModel> treeModels = estimators.stream() | ||
.map(function) | ||
.collect(Collectors.toList()); | ||
|
||
MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(schema.getLabel())) | ||
.setSegmentation(MiningModelUtil.createSegmentation(multipleModelMethod, Segmentation.MissingPredictionTreatment.RETURN_MISSING, treeModels)); | ||
|
||
return miningModel; | ||
|
||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...sklearn-extension/src/main/java/sktree/ensemble/forest/ObliqueRandomForestClassifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2024 Villu Ruusmann | ||
* | ||
* This file is part of JPMML-SkLearn | ||
* | ||
* JPMML-SkLearn 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-SkLearn 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-SkLearn. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package sktree.ensemble.forest; | ||
|
||
import java.util.List; | ||
|
||
import org.dmg.pmml.DataType; | ||
import org.dmg.pmml.MiningFunction; | ||
import org.dmg.pmml.mining.MiningModel; | ||
import org.dmg.pmml.mining.Segmentation; | ||
import org.jpmml.converter.CategoricalLabel; | ||
import org.jpmml.converter.Schema; | ||
import sklearn.Classifier; | ||
import sklearn.HasEstimatorEnsemble; | ||
import sktree.tree.ObliqueDecisionTreeClassifier; | ||
|
||
public class ObliqueRandomForestClassifier extends Classifier implements HasEstimatorEnsemble<ObliqueDecisionTreeClassifier> { | ||
|
||
public ObliqueRandomForestClassifier(String module, String name){ | ||
super(module, name); | ||
} | ||
|
||
@Override | ||
public DataType getDataType(){ | ||
return DataType.FLOAT; | ||
} | ||
|
||
@Override | ||
public MiningModel encodeModel(Schema schema){ | ||
CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); | ||
|
||
MiningModel miningModel = ObliqueForestUtil.encodeBaseObliqueForest(this, MiningFunction.CLASSIFICATION, Segmentation.MultipleModelMethod.AVERAGE, schema); | ||
|
||
encodePredictProbaOutput(miningModel, DataType.DOUBLE, categoricalLabel); | ||
|
||
return miningModel; | ||
} | ||
|
||
@Override | ||
public List<ObliqueDecisionTreeClassifier> getEstimators(){ | ||
return getList("estimators_", ObliqueDecisionTreeClassifier.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.