使用Python放入PMML [英] Using Python to PUT PMML

查看:124
本文介绍了使用Python放入PMML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 PMML 文件,我想将该文件PUT到评分服务器.这是curl调用:

I have a simple PMML file that I would like to PUT to a scoring server. Here is the curl call:

curl -X PUT --data-binary @DecisionTreeIris.pmml -H "Content-type: text/xml" http://localhost:8080/openscoring/model/DecisionTreeIris

这是PMML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PMML xmlns="http://www.dmg.org/PMML-4_1" version="4.1">
    <Header copyright="Copyright (c) 2013 Vfed" description="RPart Decision Tree Model">
        <Extension extender="Rattle/PMML" name="user" value="Vfed"/>
        <Application name="Rattle/PMML" version="1.2.34r27"/>
        <Timestamp>2013-05-15 22:02:28</Timestamp>
    </Header>
    <DataDictionary numberOfFields="5">
        <DataField name="Species" optype="categorical" dataType="string">
            <Value value="setosa"/>
            <Value value="versicolor"/>
            <Value value="virginica"/>
        </DataField>
        <DataField name="Sepal.Length" optype="continuous" dataType="double"/>
        <DataField name="Sepal.Width" optype="continuous" dataType="double"/>
        <DataField name="Petal.Length" optype="continuous" dataType="double"/>
        <DataField name="Petal.Width" optype="continuous" dataType="double"/>
    </DataDictionary>
    <TreeModel modelName="RPart_Model" functionName="classification" algorithmName="rpart" missingValueStrategy="defaultChild" splitCharacteristic="binarySplit">
        <MiningSchema>
            <MiningField name="Species" usageType="predicted"/>
            <MiningField name="Sepal.Length" usageType="active"/>
            <MiningField name="Sepal.Width" usageType="active"/>
            <MiningField name="Petal.Length" usageType="active"/>
            <MiningField name="Petal.Width" usageType="active"/>
        </MiningSchema>
        <Output>
            <OutputField name="Predicted_Species" optype="categorical" dataType="string" feature="predictedValue"/>
            <OutputField name="Probability_setosa" optype="continuous" dataType="double" feature="probability" value="setosa"/>
            <OutputField name="Probability_versicolor" optype="continuous" dataType="double" feature="probability" value="versicolor"/>
            <OutputField name="Probability_virginica" optype="continuous" dataType="double" feature="probability" value="virginica"/>
            <!-- Custom output field -->
            <OutputField name="Node_Id" optype="categorical" dataType="string" feature="entityId"/>
        </Output>
        <Node id="1" score="setosa" recordCount="150.0" defaultChild="3">
            <True/>
            <ScoreDistribution value="setosa" recordCount="50.0" confidence="0.333333333333333"/>
            <ScoreDistribution value="versicolor" recordCount="50.0" confidence="0.333333333333333"/>
            <ScoreDistribution value="virginica" recordCount="50.0" confidence="0.333333333333333"/>
            <Node id="2" score="setosa" recordCount="50.0">
                <CompoundPredicate booleanOperator="surrogate">
                    <SimplePredicate field="Petal.Length" operator="lessThan" value="2.45"/>
                    <SimplePredicate field="Petal.Width" operator="lessThan" value="0.8"/>
                    <SimplePredicate field="Sepal.Length" operator="lessThan" value="5.45"/>
                    <SimplePredicate field="Sepal.Width" operator="greaterOrEqual" value="3.35"/>
                </CompoundPredicate>
                <ScoreDistribution value="setosa" recordCount="50.0" confidence="1.0"/>
                <ScoreDistribution value="versicolor" recordCount="0.0" confidence="0.0"/>
                <ScoreDistribution value="virginica" recordCount="0.0" confidence="0.0"/>
            </Node>
            <Node id="3" score="versicolor" recordCount="100.0" defaultChild="7">
                <CompoundPredicate booleanOperator="surrogate">
                    <SimplePredicate field="Petal.Length" operator="greaterOrEqual" value="2.45"/>
                    <SimplePredicate field="Petal.Width" operator="greaterOrEqual" value="0.8"/>
                    <SimplePredicate field="Sepal.Length" operator="greaterOrEqual" value="5.45"/>
                    <SimplePredicate field="Sepal.Width" operator="lessThan" value="3.35"/>
                </CompoundPredicate>
                <ScoreDistribution value="setosa" recordCount="0.0" confidence="0.0"/>
                <ScoreDistribution value="versicolor" recordCount="50.0" confidence="0.5"/>
                <ScoreDistribution value="virginica" recordCount="50.0" confidence="0.5"/>
                <Node id="6" score="versicolor" recordCount="54.0">
                    <CompoundPredicate booleanOperator="surrogate">
                        <SimplePredicate field="Petal.Width" operator="lessThan" value="1.75"/>
                        <SimplePredicate field="Petal.Length" operator="lessThan" value="4.75"/>
                        <SimplePredicate field="Sepal.Length" operator="lessThan" value="6.15"/>
                        <SimplePredicate field="Sepal.Width" operator="lessThan" value="2.95"/>
                    </CompoundPredicate>
                    <ScoreDistribution value="setosa" recordCount="0.0" confidence="0.0"/>
                    <ScoreDistribution value="versicolor" recordCount="49.0" confidence="0.907407407407407"/>
                    <ScoreDistribution value="virginica" recordCount="5.0" confidence="0.0925925925925926"/>
                </Node>
                <Node id="7" score="virginica" recordCount="46.0">
                    <CompoundPredicate booleanOperator="surrogate">
                        <SimplePredicate field="Petal.Width" operator="greaterOrEqual" value="1.75"/>
                        <SimplePredicate field="Petal.Length" operator="greaterOrEqual" value="4.75"/>
                        <SimplePredicate field="Sepal.Length" operator="greaterOrEqual" value="6.15"/>
                        <SimplePredicate field="Sepal.Width" operator="greaterOrEqual" value="2.95"/>
                    </CompoundPredicate>
                    <ScoreDistribution value="setosa" recordCount="0.0" confidence="0.0"/>
                    <ScoreDistribution value="versicolor" recordCount="1.0" confidence="0.0217391304347826"/>
                    <ScoreDistribution value="virginica" recordCount="45.0" confidence="0.978260869565217"/>
                </Node>
            </Node>
        </Node>
    </TreeModel>
</PMML>

不确定是否重要,但是我正在使用 Openscoring PMML评分服务器.

Not sure that it matters but I am using the Openscoring PMML scoring server.

推荐答案

我建议使用Kenneth Reitz(文档).

I'd recommend using the requests library by Kenneth Reitz (GitHub and Docs).

具体来说,有一个示例关于如何发布文件.使用它来构造您需要的东西.

Specifically, there's an example on how to POST files. Use that to construct what you would need.

我只是假设在这里,但我会尝试以下操作:

I'm just assuming here but I would try the following:

import requests

url = 'http://localhost:8080/openscoring/model/DecisionTreeIris'
files = {'file': open('/path/to/file/DecisionTreeIris.pmml', 'rb')}

response = requests.post(url, files=files)

您还可以设置标题或您需要的其他任何内容.请求非常简单易用,这给Python社区带来了福音.该文档非常出色,通常您可以轻松地通过Google/Bing/DuckDuckGo搜索找到示例.

You can also set the headers or anything else you need. requests is dead simple to use and a boon to the Python community. The documentation is excellent and you can usually find examples with a Google/Bing/DuckDuckGo search easily.

希望对您有帮助!

这篇关于使用Python放入PMML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆