【发布时间】:2016-05-13 15:37:24
【问题描述】:
我有一个从同事的 R 线性模型生成的 PMML 文件(如下),该文件用于根据 5 个特征预测商品的成本。我正在尝试在 Python 中使用 Augustus 来使用这个模型并做出这些预测。我已成功获取 Augustus 加载的 PMML 文件,但未能获取预测值。
我已经浏览了 Augustus 的 Model abstraction 中的许多示例,并通过搜索 Stack 和 Google,但我还没有找到任何成功使用线性回归的示例。有一个similar question asked previously,但从未得到正确回答。我也尝试过其他example regression PMML files,结果相似。
如何在 Python 中使用 Augustus(或其他库)运行回归并获得预测?
PMML 代码:linear_model.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 代码:
import pandas as pd
from augustus.strict import *
train_full_df = pd.read_csv('train_data.csv', low_memory=False)
model = modelLoader.loadXml('linear_model.xml')
dataTable = model.calc({'quantity': train_full_df.quantity[:10],
'total_component_weight': train_full_df.total_component_weight[:10],
'quantity_cost_mean': train_full_df.quantity_cost_mean[:10],
'mat_quantity_cost_mean': train_full_df.mat_quantity_cost_mean[:10],
'solid_volume': train_full_df.solid_volume[:10],
})
dataTable.look()
(输出)
# | 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
从表中可以看出,仅显示输入值,没有“成本”值。如何获得要预测的成本?
我正在使用 Python 2.7、Augustus 0.6(也尝试过 0.5)、OS X 10.11
【问题讨论】:
-
scikit-learn 是广泛用于机器学习模型的 Python 库。它非常易于使用且直截了当。 scikit-learn.org/stable/modules/linear_model.html
-
是的,scikit-learn 很棒。我已经在几个项目中使用过它。我在这里遇到的问题是其他人正在生成我想在 Python 中使用的 R 模型。我知道的唯一桥梁是 R 模型生成 PMML 和我的 Python 代码使用 Augustus 使用它。 scikit-learn 是否允许我阅读 PMML?
-
我还没有做过这样的事情。但是您是否需要通过训练数据的成本来训练回归模型?然后传递一部分训练数据来交叉验证模型?
-
我尝试在调用 model.calc() 时传递这些项目的已知成本值,但它只是显示给定的成本值(而不是预测值)
-
我目前在使用 Knime 生成的模型时遇到了同样的问题。如果我能让它工作,我会发布一个答案。我认为这很简单。文档质量不高,因此可以想象所需的内容只是晦涩难懂。
标签: python xml xsd linear-regression pmml