Python中基于PMML和Augustus的评分回归模型

2024-06-24 12:07:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个PMML文件(如下)从我的同事的R线性模型生成,它将用于预测基于5个特性的项目的成本。我试图使用Python中的Augustus来使用这个模型并做出这些预测。我已经成功地让Augustus加载了PMML文件,但是我没有得到预期的值。在

我已经看过很多奥古斯都的Model abstraction和搜索堆栈和谷歌的例子,但我还没有找到任何成功使用线性回归的例子。有一个similar question asked previously,但一直没有得到正确的回答。我也尝试过其他的example regression PMML files,得到了类似的结果。在

如何使用Python中的Augustus(或其他库)运行回归并获得预测?

PMML代码:线性_模型.xml在

<?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) 2016 root" description="Linear Regression Model">
  <Extension name="user" value="root" extender="Rattle/PMML"/>
  <Application name="Rattle/PMML" version="1.4"/>
  <Timestamp>2016-02-02 19:20:59</Timestamp>
 </Header>
 <DataDictionary numberOfFields="6">
  <DataField name="cost" optype="continuous" dataType="double"/>
  <DataField name="quantity" optype="continuous" dataType="double"/>
  <DataField name="total_component_weight" optype="continuous" dataType="double"/>
  <DataField name="quantity_cost_mean" optype="continuous" dataType="double"/>
  <DataField name="mat_quantity_cost_mean" optype="continuous" dataType="double"/>
  <DataField name="solid_volume" optype="continuous" dataType="double"/>
 </DataDictionary>
 <RegressionModel modelName="Linear_Regression_Model" functionName="regression" algorithmName="least squares" targetFieldName="cost">
  <MiningSchema>
   <MiningField name="cost" usageType="predicted"/>
   <MiningField name="quantity" usageType="active"/>
   <MiningField name="total_component_weight" usageType="active"/>
   <MiningField name="quantity_cost_mean" usageType="active"/>
   <MiningField name="mat_quantity_cost_mean" usageType="active"/>
   <MiningField name="solid_volume" usageType="active"/>
  </MiningSchema>
  <Output>
   <OutputField name="Predicted_cost" feature="predictedValue"/>
  </Output>
  <RegressionTable intercept="-5.18924891969128">
   <NumericPredictor name="quantity" exponent="1" coefficient="0.0128484453941352"/>
   <NumericPredictor name="total_component_weight" exponent="1" coefficient="12.0357979395919"/>
   <NumericPredictor name="quantity_cost_mean" exponent="1" coefficient="0.500814050845585"/>
   <NumericPredictor name="mat_quantity_cost_mean" exponent="1" coefficient="0.556822746464491"/>
   <NumericPredictor name="solid_volume" exponent="1" coefficient="0.000197314943339284"/>
  </RegressionTable>
 </RegressionModel>
</PMML>

Python代码:

^{pr2}$

(输出)

#  | quantity   | total_comp | quantity_c | mat_quanti | solid_volu
---+------------+------------+------------+------------+-----------
0  | 1.0        | 0.018      | 32.2903337 | 20.4437141 | 1723.48653
1  | 2.0        | 0.018      | 17.2369194 | 12.0418426 | 1723.48653
2  | 5.0        | 0.018      | 10.8846412 | 7.22744702 | 1723.48653
3  | 10.0       | 0.018      | 6.82802948 | 4.3580642  | 1723.48653
4  | 25.0       | 0.018      | 4.84356482 | 3.09218161 | 1723.48653
5  | 50.0       | 0.018      | 4.43703495 | 2.74377648 | 1723.48653
6  | 100.0      | 0.018      | 4.22259101 | 2.5990824  | 1723.48653
7  | 250.0      | 0.018      | 4.1087198  | 2.53432422 | 1723.48653
8  | 1.0        | 0.018      | 32.2903337 | 20.4437141 | 1723.48653
9  | 2.0        | 0.018      | 17.2369194 | 12.0418426 | 1723.48653

从表中可以看到,只显示输入值,没有“成本”值。如何预测成本?在

我使用的是python2.7、augustus0.6(也尝试过0.5)、osx10.11


Tags: namemeanquantitypmmlactivecontinuousdoublecost
1条回答
网友
1楼 · 发布于 2024-06-24 12:07:17

您可以使用PyPMML在Python中为PMML模型评分,以您的模型为例:

import pandas as pd
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) 2016 root" description="Linear Regression Model">
  <Extension name="user" value="root" extender="Rattle/PMML"/>
  <Application name="Rattle/PMML" version="1.4"/>
  <Timestamp>2016-02-02 19:20:59</Timestamp>
 </Header>
 <DataDictionary numberOfFields="6">
  <DataField name="cost" optype="continuous" dataType="double"/>
  <DataField name="quantity" optype="continuous" dataType="double"/>
  <DataField name="total_component_weight" optype="continuous" dataType="double"/>
  <DataField name="quantity_cost_mean" optype="continuous" dataType="double"/>
  <DataField name="mat_quantity_cost_mean" optype="continuous" dataType="double"/>
  <DataField name="solid_volume" optype="continuous" dataType="double"/>
 </DataDictionary>
 <RegressionModel modelName="Linear_Regression_Model" functionName="regression" algorithmName="least squares" targetFieldName="cost">
  <MiningSchema>
   <MiningField name="cost" usageType="predicted"/>
   <MiningField name="quantity" usageType="active"/>
   <MiningField name="total_component_weight" usageType="active"/>
   <MiningField name="quantity_cost_mean" usageType="active"/>
   <MiningField name="mat_quantity_cost_mean" usageType="active"/>
   <MiningField name="solid_volume" usageType="active"/>
  </MiningSchema>
  <Output>
   <OutputField name="Predicted_cost" feature="predictedValue"/>
  </Output>
  <RegressionTable intercept="-5.18924891969128">
   <NumericPredictor name="quantity" exponent="1" coefficient="0.0128484453941352"/>
   <NumericPredictor name="total_component_weight" exponent="1" coefficient="12.0357979395919"/>
   <NumericPredictor name="quantity_cost_mean" exponent="1" coefficient="0.500814050845585"/>
   <NumericPredictor name="mat_quantity_cost_mean" exponent="1" coefficient="0.556822746464491"/>
   <NumericPredictor name="solid_volume" exponent="1" coefficient="0.000197314943339284"/>
  </RegressionTable>
 </RegressionModel>
</PMML>''')
data = pd.DataFrame({
    'quantity': [1.0,2.0,5.0,10.0,25.0,50.0,100.0,250.0,1.0,2.0],
    'total_component_weight': [0.018, 0.018, 0.018, 0.018, 0.018, 0.018, 0.018, 0.018, 0.018, 0.018],
    'quantity_cost_mean': [32.2903337,17.2369194,10.8846412,6.82802948,4.84356482,4.43703495,4.22259101,4.1087198,32.2903337,17.2369194],
    'mat_quantity_cost_mean': [20.4437141,12.0418426,7.22744702,4.3580642 ,3.09218161,2.74377648,2.5990824 ,2.53432422,20.4437141,12.0418426],
    'solid_volume': [1723.48653,1723.48653,1723.48653,1723.48653,1723.48653,1723.48653,1723.48653,1723.48653,1723.48653,1723.48653]
})
result = model.predict(data)

结果是:

^{pr2}$

相关问题 更多 >