【问题标题】:How to score a linear model using PMML file and Augustus on Python如何在 Python 上使用 PMML 文件和 Augustus 对线性模型进行评分
【发布时间】:2013-11-28 15:08:36
【问题描述】:

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

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

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

使用 R 中的汽车数据生成示例 PMML 文件。其中“dist”是相关变量,“speed”是自变量。现在,每当我从等式(即 dist = -17.5790948905109 + speed*3.93240875912408)中收到速度数据时,我想每次都预测 dist。我知道它可以在 R 中使用 predict 函数轻松完成,但问题是我在后端没有 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>

【问题讨论】:

    标签: python r xsd data-mining pmml


    【解决方案1】:

    您可以使用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}
    

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 2014-01-30
      • 2020-04-08
      • 2014-12-13
      • 2019-02-22
      • 2020-03-03
      • 2019-11-24
      • 2016-07-06
      • 1970-01-01
      相关资源
      最近更新 更多