在Windows下将python中scikit learn的模型转化为PMML文件
2017-08-08 15:49
691 查看
最近项目需要将python中训练好的模型转移到Java中使用,所以在网上查到了PMML可以实现这个功能,那么本文将介绍在Windows下如何将python中scikit learn的模型转化为PMML文件,从而方便将训练好的模型供其他语言使用。
PMML 是一种事实标准语言,用于呈现数据挖掘模型。预测分析模型 和数据挖掘模型 是指代数学模型的术语,这些模型采用统计技术了解大量历史数据中隐藏的模式。预测分析模型采用定型过程中获取的知识来预测新数据中是否有已知模式。PMML 允许您在不同的应用程序之间轻松共享预测分析模型。因此,您可以在一个系统中定型一个模型,在 PMML 中对其进行表达,然后将其移动到另一个系统中,并在该系统中使用上述模型预测机器失效的可能性等。
简单来说,PMML就是在一个平台训练好模型,然后封装成PMML文件,然后再在另一个平台中可以直接使用训练好的模型。
PMML 是数据挖掘群组的产物,该群组是一个由供应商领导的委员会,由各种商业和开放源码分析公司组成。因此,现在的大部分领先数据挖掘工具都可以导出或导入 PMML。作为一个已发展 10 多年的成熟标准,PMML 既可以呈现用于从数据中了解模型的统计技术(如人工神经网络和决策树),也可以呈现原始输入数据的预处理以及模型输出的后处理(参见 图 1)。
图 1. PMML 包含数据预处理和数据后处理以及预测模型本身
安装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命令行下输入
首先训练模型
下面,将训练好的模型转化为PMML文件,SkLearn2PMML将包含两部分,estimator(our clf) 和 a mapper (for preprocessing steps),这里,我们的预处理没有做任何操作。
下面,则是PMML文件中的内容:
参考文献
【1】https://www.ibm.com/developerworks/cn/opensource/ind-PMML1/
【2】https://stackoverflow.com/questions/33221331/export-python-scikit-learn-models-into-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.html from sklearn import datasets, tree iris = 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 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")
下面,则是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
相关文章推荐
- python scikit learn 常用模型汇总
- 64位windows下的python+numpy+scipy+scikit-learn安装
- 【Python学习系列十八】基于scikit-learn库逻辑回归训练模型(delta比赛代码3)
- XGBoost模型文件转化为PMML
- Python scikit-learn 学习笔记—鸢尾花模型
- windows下用Python把pdf文件转化为图片(png格式)
- windows下安装python numpy+scipy+matlotlib+scikit-learn等流行库
- Windows下 python快速安装numpy、matplotlib、scikit-learn等库方法总结
- python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
- Windows下安装Python常用工具包(numpy、scipy、scikit-learn)
- windows 安装python3.6(numpy,scipy,pandas,matplotlib,scikit-learn)
- windows 安装python3.6(numpy,scipy,pandas,matplotlib,scikit-learn)
- 【Python学习系列十七】基于scikit-learn库逻辑回归训练模型(delta比赛代码2)
- windows 安装python3.6(numpy,scipy,pandas,matplotlib,scikit-learn)
- windows 安装python3.6(numpy,scipy,pandas,matplotlib,scikit-learn)
- windows 64位机器安装python机器学习环境scikit_learn
- 在windows上安装python的机器学习包numpy scipy scikit_learn matplotlib
- 【Python】scikit-learn机器学习(一)——一元回归模型
- python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
- 【Python学习系列二十】scikit-learn库模型持久化