我有一个 PMML 文件(如下),它是从我同事的 R 线性模型生成的,用于根据 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
最佳答案
您可以使用 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)
结果是:
Predicted_cost
0 22.935291
1 10.730825
2 4.907295
3 1.342192
4 -0.163801
5 -0.240186
6 0.214271
7 2.048450
8 22.935291
9 10.730825
关于python - 在 Python 中使用 PMML 和 Augustus 对回归模型进行评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194007/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t