在Windows下将python中scikit learn的模型转化为PMML文件

来源:互联网 发布:数据库软件免费下载 编辑:程序博客网 时间:2024/06/04 22:27

最近项目需要将python中训练好的模型转移到Java中使用,所以在网上查到了PMML可以实现这个功能,那么本文将介绍在Windows下如何将python中scikit learn的模型转化为PMML文件,从而方便将训练好的模型供其他语言使用。

何为PMML?

首先,我们得明白什么是PMML?

PMML 是一种事实标准语言,用于呈现数据挖掘模型。预测分析模型 和数据挖掘模型 是指代数学模型的术语,这些模型采用统计技术了解大量历史数据中隐藏的模式。预测分析模型采用定型过程中获取的知识来预测新数据中是否有已知模式。PMML 允许您在不同的应用程序之间轻松共享预测分析模型。因此,您可以在一个系统中定型一个模型,在 PMML 中对其进行表达,然后将其移动到另一个系统中,并在该系统中使用上述模型预测机器失效的可能性等。

简单来说,PMML就是在一个平台训练好模型,然后封装成PMML文件,然后再在另一个平台中可以直接使用训练好的模型。

PMML 是数据挖掘群组的产物,该群组是一个由供应商领导的委员会,由各种商业和开放源码分析公司组成。因此,现在的大部分领先数据挖掘工具都可以导出或导入 PMML。作为一个已发展 10 多年的成熟标准,PMML 既可以呈现用于从数据中了解模型的统计技术(如人工神经网络和决策树),也可以呈现原始输入数据的预处理以及模型输出的后处理(参见 图 1)。

这里写图片描述

图 1. PMML 包含数据预处理和数据后处理以及预测模型本身

在python中安装sklearn2pmml

现在讲解如何在python中安装sklearn2pmml。

  • 安装pip

    先从官网上下载pip,下载地址是:https://pypi.python.org/pypi/pip#downloads :

    下载完成之后,解压到一个文件夹,用CMD控制台进入解压目录,输入:

    python setup.py install

    最后,在添加环境变量:在PATH最后添加:C:\Python34\Scripts;

    测试pip是否安装成功:在cmd中输入pip,出现下图则表示成功:

这里写图片描述

  • 安装git

    先从官网上下载git,网址是https://git-scm.com/download/win

    再安装教程进行安装,网址是http://blog.csdn.net/renfufei/article/details/41647875/

    测试git是否安装成功,在cmd中输入git,出现下图则表示成功:

这里写图片描述

  • 安装sklearn2pmml

    在cmd命令行下输入

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

这里写图片描述

测试案例

下面,将用python训练一个简单的模型,并将其封装为PMML文件。

  • 首先训练模型
# example tree & viz from http://scikit-learn.org/stable/modules/tree.htmlfrom sklearn import datasets, treeiris = datasets.load_iris()clf = tree.DecisionTreeClassifier() clf = clf.fit(iris.data, iris.target)

下面,将训练好的模型转化为PMML文件,SkLearn2PMML将包含两部分,estimator(our clf) 和 a mapper (for preprocessing steps),这里,我们的预处理没有做任何操作。

from sklearn_pandas import DataFrameMapperdefault_mapper = DataFrameMapper([(i, None) for i in iris.feature_names + ['Species']])from sklearn2pmml import sklearn2pmmlsklearn2pmml(estimator=clf,              mapper=default_mapper,              pmml="D:/workspace/IrisClassificationTree.pmml")

下面,则是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】https://www.ibm.com/developerworks/cn/opensource/ind-PMML1/

【2】https://stackoverflow.com/questions/33221331/export-python-scikit-learn-models-into-pmml

原创粉丝点击