2015-10-19 14 views
8

Ich möchte Python Scikit lernen Modelle in PMML exportieren.Export Python Scikit lernen Modelle in Pmml

Welches Python-Paket ist am besten geeignet?

Ich lese über Augustus, aber ich konnte kein Beispiel mit scikit-learn-Modellen finden.

+0

Sie Scikit-Learn-Modelle und Transformatoren zu PMML umwandeln kann die [sklearn2pmml] (https://github.com mit/jpmml/sklearn2pmml) Paket. – user1808924

+0

JPMML-SkLearn unterstützt Python 2.7 auch, aber es wird im Moment nicht beworben. – user1808924

+0

Das jpmml-sklearn-Paket unterstützt von Python 3.4. Gibt es eine Alternative, die Python unterstützt 2.7 – Selva

Antwort

8

SkLearn2PMML ist

eine dünne Hülle um die JPMML-SkLearn Befehlszeilenanwendung. Eine Liste der unterstützten Scikit-Learn Estimator- und Transformer-Typen finden Sie in der Dokumentation des JPMML-SkLearn-Projekts.

Wie @ user1808924 bemerkt, unterstützt es Python 2.7 oder 3.4+. Es erfordert auch Java 1.7+

Installiert über: (erfordert git)

pip install git+https://github.com/jpmml/sklearn2pmml.git 

Beispiel dafür, wie ein Klassifikator Baum zu PMML exportieren. Zuerst wächst den Baum:

# example tree & viz from http://scikit-learn.org/stable/modules/tree.html 
from sklearn import datasets, tree 
iris = datasets.load_iris() 
clf = tree.DecisionTreeClassifier() 
clf = clf.fit(iris.data, iris.target) 

Es gibt zwei Teile zu einer SkLearn2PMML Umwandlung, ein Schätzer (unsere clf) und ein Mapper (für Vorverarbeitungsschritte wie Diskretisierung oder PCA). Unser Mapper ist ziemlich einfach, da wir keine Transformationen durchführen.

from sklearn_pandas import DataFrameMapper 
default_mapper = DataFrameMapper([(i, None) for i in iris.feature_names + ['Species']]) 

from sklearn2pmml import sklearn2pmml 
sklearn2pmml(estimator=clf, 
      mapper=default_mapper, 
      pmml="D:/workspace/IrisClassificationTree.pmml") 

Es ist möglich (wenn auch nicht dokumentiert) mapper=None passieren, aber Sie werden die Prädiktor Namen verloren (Rückkehr x1 nicht sepal length etc.) sehen, dass.

Blick Lassen Sie sich auf der .pmml Datei:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<PMML xmlns="http://www.dmg.org/PMML-4_3" version="4.3"> 
    <Header> 
     <Application name="JPMML-SkLearn" version="1.1.1"/> 
     <Timestamp>2016-09-26T19:21:43Z</Timestamp> 
    </Header> 
    <DataDictionary> 
     <DataField name="sepal length (cm)" optype="continuous" dataType="float"/> 
     <DataField name="sepal width (cm)" optype="continuous" dataType="float"/> 
     <DataField name="petal length (cm)" optype="continuous" dataType="float"/> 
     <DataField name="petal width (cm)" optype="continuous" dataType="float"/> 
     <DataField name="Species" optype="categorical" dataType="string"> 
      <Value value="setosa"/> 
      <Value value="versicolor"/> 
      <Value value="virginica"/> 
     </DataField> 
    </DataDictionary> 
    <TreeModel functionName="classification" splitCharacteristic="binarySplit"> 
     <MiningSchema> 
      <MiningField name="Species" usageType="target"/> 
      <MiningField name="sepal length (cm)"/> 
      <MiningField name="sepal width (cm)"/> 
      <MiningField name="petal length (cm)"/> 
      <MiningField name="petal width (cm)"/> 
     </MiningSchema> 
     <Output> 
      <OutputField name="probability_setosa" dataType="double" feature="probability" value="setosa"/> 
      <OutputField name="probability_versicolor" dataType="double" feature="probability" value="versicolor"/> 
      <OutputField name="probability_virginica" dataType="double" feature="probability" value="virginica"/> 
     </Output> 
     <Node id="1"> 
      <True/> 
      <Node id="2" score="setosa" recordCount="50.0"> 
       <SimplePredicate field="petal width (cm)" operator="lessOrEqual" value="0.8"/> 
       <ScoreDistribution value="setosa" recordCount="50.0"/> 
       <ScoreDistribution value="versicolor" recordCount="0.0"/> 
       <ScoreDistribution value="virginica" recordCount="0.0"/> 
      </Node> 
      <Node id="3"> 
       <SimplePredicate field="petal width (cm)" operator="greaterThan" value="0.8"/> 
       <Node id="4"> 
        <SimplePredicate field="petal width (cm)" operator="lessOrEqual" value="1.75"/> 
        <Node id="5"> 
         <SimplePredicate field="petal length (cm)" operator="lessOrEqual" value="4.95"/> 
         <Node id="6" score="versicolor" recordCount="47.0"> 
          <SimplePredicate field="petal width (cm)" operator="lessOrEqual" value="1.6500001"/> 
          <ScoreDistribution value="setosa" recordCount="0.0"/> 
          <ScoreDistribution value="versicolor" recordCount="47.0"/> 
          <ScoreDistribution value="virginica" recordCount="0.0"/> 
         </Node> 
         <Node id="7" score="virginica" recordCount="1.0"> 
          <SimplePredicate field="petal width (cm)" operator="greaterThan" value="1.6500001"/> 
          <ScoreDistribution value="setosa" recordCount="0.0"/> 
          <ScoreDistribution value="versicolor" recordCount="0.0"/> 
          <ScoreDistribution value="virginica" recordCount="1.0"/> 
         </Node> 
        </Node> 
        <Node id="8"> 
         <SimplePredicate field="petal length (cm)" operator="greaterThan" value="4.95"/> 
         <Node id="9" score="virginica" recordCount="3.0"> 
          <SimplePredicate field="petal width (cm)" operator="lessOrEqual" value="1.55"/> 
          <ScoreDistribution value="setosa" recordCount="0.0"/> 
          <ScoreDistribution value="versicolor" recordCount="0.0"/> 
          <ScoreDistribution value="virginica" recordCount="3.0"/> 
         </Node> 
         <Node id="10"> 
          <SimplePredicate field="petal width (cm)" operator="greaterThan" value="1.55"/> 
          <Node id="11" score="versicolor" recordCount="2.0"> 
           <SimplePredicate field="sepal length (cm)" operator="lessOrEqual" value="6.95"/> 
           <ScoreDistribution value="setosa" recordCount="0.0"/> 
           <ScoreDistribution value="versicolor" recordCount="2.0"/> 
           <ScoreDistribution value="virginica" recordCount="0.0"/> 
          </Node> 
          <Node id="12" score="virginica" recordCount="1.0"> 
           <SimplePredicate field="sepal length (cm)" operator="greaterThan" value="6.95"/> 
           <ScoreDistribution value="setosa" recordCount="0.0"/> 
           <ScoreDistribution value="versicolor" recordCount="0.0"/> 
           <ScoreDistribution value="virginica" recordCount="1.0"/> 
          </Node> 
         </Node> 
        </Node> 
       </Node> 
       <Node id="13"> 
        <SimplePredicate field="petal width (cm)" operator="greaterThan" value="1.75"/> 
        <Node id="14"> 
         <SimplePredicate field="petal length (cm)" operator="lessOrEqual" value="4.8500004"/> 
         <Node id="15" score="virginica" recordCount="2.0"> 
          <SimplePredicate field="sepal width (cm)" operator="lessOrEqual" value="3.1"/> 
          <ScoreDistribution value="setosa" recordCount="0.0"/> 
          <ScoreDistribution value="versicolor" recordCount="0.0"/> 
          <ScoreDistribution value="virginica" recordCount="2.0"/> 
         </Node> 
         <Node id="16" score="versicolor" recordCount="1.0"> 
          <SimplePredicate field="sepal width (cm)" operator="greaterThan" value="3.1"/> 
          <ScoreDistribution value="setosa" recordCount="0.0"/> 
          <ScoreDistribution value="versicolor" recordCount="1.0"/> 
          <ScoreDistribution value="virginica" recordCount="0.0"/> 
         </Node> 
        </Node> 
        <Node id="17" score="virginica" recordCount="43.0"> 
         <SimplePredicate field="petal length (cm)" operator="greaterThan" value="4.8500004"/> 
         <ScoreDistribution value="setosa" recordCount="0.0"/> 
         <ScoreDistribution value="versicolor" recordCount="0.0"/> 
         <ScoreDistribution value="virginica" recordCount="43.0"/> 
        </Node> 
       </Node> 
      </Node> 
     </Node> 
    </TreeModel> 
</PMML> 

Die erste Split (Node 1) auf Blütenblatt Breite bei 0,8. Knoten 2 (Blütenblattbreite < = 0,8) fängt alle Setosa ein, mit nichts anderem.

Sie können die PMML Ausgabe an den graphviz Ausgang vergleichen:

from sklearn.externals.six import StringIO 
import pydotplus # this might be pydot for python 2.7 
dot_data = StringIO() 
tree.export_graphviz(clf, 
        out_file=dot_data, 
        feature_names=iris.feature_names, 
        class_names=iris.target_names, 
        filled=True, rounded=True, 
        special_characters=True) 
graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 
graph.write_pdf("D:/workspace/iris.pdf") 
# for in-line display, you can also do: 
# from IPython.display import Image 
# Image(graph.create_png()) 

enter image description here

+0

Gibt es eine Möglichkeit, die Prädiktor-Namen zu erhalten, wenn Sie keinen Mapper verwenden? Ich muss sie wirklich auf der Evaluator-Seite kennen, aber einen Mapper nur dafür zu konstruieren, ist zu viel Overkill. – KidCrippler

+0

@K Ich konnte nicht herausfinden, wie man die Prädiktorennamen ohne Mapper erhält. Sie könnten versuchen, die Frage zu posten. – C8H10N4O2

+3

Die Antwort scheint veraltet zu sein: 'sklearn2pmml' verwendet jetzt' PMMLPipeline'. – sds

Verwandte Themen