我想将python scikit-learn模型导出到PMML。
哪个python软件包最合适?
我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。
python大神给出的解决方案
SkLearn2PMML
是
JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和Transformer类型的列表,请参阅JPMML-SkLearn项目的文档。
如@ user1808924所述,它支持Python 2.7或3.4+。它还需要Java 1.7+
通过以下方式安装:(需要git)
pip install git+https://github.com/jpmml/sklearn2pmml.git
如何将分类树导出到PMML的示例。首先种树:
# 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)
SkLearn2PMML转换由两部分组成:一个估计器(我们的clf
)和一个映射器(用于诸如离散化或PCA之类的预处理步骤)。我们的映射器非常基础,因为我们没有进行任何转换。
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")
可以(虽然没有记录)通过mapper=None
,但是您将看到预测变量名称丢失(返回x1
而不是sepal length
等)。
让我们看一下.pmml
文件:
<?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>
第一个分割(节点1)的花瓣宽度为0.8。节点2(花瓣宽度<= 0.8)捕获了所有刚毛,没有其他东西。
您可以将pmml输出与graphviz
输出进行比较:
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())
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
字符串文字中的正斜杠表现异常 - python为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…
Python numpy数据指针地址无需更改即可更改 - python编辑经过一些摆弄之后,到目前为止,我已经隔离了以下状态:一维数组在直接输入变量时提供两个不同的地址,而在使用print()时仅提供一个地址2D数组(或矩阵)在直接输入变量时提供三个不同的地址,在使用print()时提供两个地址3D数组在直接输入变量时提供两个不同的地址,而在使用print()时仅给出一个(显然与一维数组相同)像这样:>>> …
尝试在Spring MVC中使用OAuth保护资源 - java我们已经有在Spring MVC上用Java编写的REST Web服务,我一直在尝试保护它们。OAuth服务器是在另一个处理访问令牌登录和创建的网站中实现的。因此,在向用户授予对Web服务的访问权限之前,我需要验证访问令牌是否正确。但是,带有OAuth的Spring Security的文档似乎真的很差,并且示例代码实际上并未解释其作用!我什至不确定是否应该为…
DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…