python - 如何在 Python 上使用 PMML 文件和 Augustus 对线性模型进行评分

标签 python r xsd data-mining pmml

我是 python、PMML 和 augustus 的新手,所以这个问题有点新手。我有一个 PMML 文件,我想在每次新的数据迭代后从中评分。我必须使用 Python 和 Augustus 来完成这个练习。我读过各种文章,其中一些值得一提,因为它们很好。

(http://augustusdocs.appspot.com/docs/v06/model_abstraction/augustus_and_pmml.htmlhttp://augustus.googlecode.com/svn-history/r191/trunk/augustus/modellib/regression/producer/Producer.py )

我已阅读与评分相关的 augustus 文档以了解其工作原理,但我无法解决此问题。

示例 PMML 文件是使用 R 中的汽车数据生成的。其中“距离”是因变量,“速度”是自变量。现在,每当我从方程中收到速度数据时,我想预测 dist (即 dist = -17.5790948905109 + speed*3.93240875912408) 。我知道它可以在 R 中使用预测函数轻松完成,但问题是我后端没有 R,只有 python 与 augustus 一起评分。非常感谢任何帮助,并提前致谢。

示例 PMML 文件:

     <?xml version="1.0"?>
     <PMML version="4.1" xmlns="http://www.dmg.org/PMML-4_1"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dmg.org/PMML-4_1 http://www.dmg.org/v4-1/pmml-4-1.xsd">
         <Header copyright="Copyright (c) 2013 user" description="Linear Regression Model">
          <Extension name="user" value="user" extender="Rattle/PMML"/>
          <Application name="Rattle/PMML" version="1.4"/>
          <Timestamp>2013-11-07 09:24:06</Timestamp>
         </Header>
        <DataDictionary numberOfFields="2">
         <DataField name="dist" optype="continuous" dataType="double"/>
         <DataField name="speed" optype="continuous" dataType="double"/>
        </DataDictionary>
        <RegressionModel modelName="Linear_Regression_Model" functionName="regression"   algorithmName="least squares">
         <MiningSchema>
          <MiningField name="dist" usageType="predicted"/>
          <MiningField name="speed" usageType="active"/>
         </MiningSchema>
         <Output>
          <OutputField name="Predicted_dist" feature="predictedValue"/>
         </Output>
         <RegressionTable intercept="-17.5790948905109">
          <NumericPredictor name="speed" exponent="1" coefficient="3.93240875912408"/>
         </RegressionTable>
        </RegressionModel>
     </PMML>

最佳答案

您可以使用PyPMML用Python对PMML模型进行评分,例如:

from pypmml import Model

model = Model.fromString('''<?xml version="1.0"?>
     <PMML version="4.1" xmlns="http://www.dmg.org/PMML-4_1"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dmg.org/PMML-4_1 http://www.dmg.org/v4-1/pmml-4-1.xsd">
         <Header copyright="Copyright (c) 2013 user" description="Linear Regression Model">
          <Extension name="user" value="user" extender="Rattle/PMML"/>
          <Application name="Rattle/PMML" version="1.4"/>
          <Timestamp>2013-11-07 09:24:06</Timestamp>
         </Header>
        <DataDictionary numberOfFields="2">
         <DataField name="dist" optype="continuous" dataType="double"/>
         <DataField name="speed" optype="continuous" dataType="double"/>
        </DataDictionary>
        <RegressionModel modelName="Linear_Regression_Model" functionName="regression"   algorithmName="least squares">
         <MiningSchema>
          <MiningField name="dist" usageType="predicted"/>
          <MiningField name="speed" usageType="active"/>
         </MiningSchema>
         <Output>
          <OutputField name="Predicted_dist" feature="predictedValue"/>
         </Output>
         <RegressionTable intercept="-17.5790948905109">
          <NumericPredictor name="speed" exponent="1" coefficient="3.93240875912408"/>
         </RegressionTable>
        </RegressionModel>
     </PMML>''')
result = model.predict({'speed': 1.0})

结果是一个包含 Predicted_dist 的字典:

{'Predicted_dist': -13.646686131386819}

关于python - 如何在 Python 上使用 PMML 文件和 Augustus 对线性模型进行评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997433/

相关文章:

python - 排序列表输出: UTF-8

r - 在 R 中创建唯一的随机组 ID

r - 如何创建模型来对特定组的数据进行分类

xml - XSD 架构 - 命名空间

java - 具有相对路径的 Jaxb2 目录文件

python - 在另一个类中使用实例方法作为装饰器

python - 如何安静 SimpleHTTPServer?

从一个字符串列表创建的 Python 字符串列表

r - 根据 R 中第一个数据集的值,向量化来自不同数据集的变量的新变量添加

java - 带有枚举的 XML 模式