1

我使用 bigml.com 生成了 iris 数据集的决策树模型。我已将此决策树模型下载为 PMML,并希望将其用于本地计算机中的预测。

来自 bigml 的 PMML 模型

<?xml version="1.0" encoding="utf-8"?>
<PMML version="4.2" xmlns="http://www.dmg.org/PMML-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Header description="Generated by BigML"/>
    <DataDictionary>
        <DataField dataType="double" displayName="Sepal length" name="000001" optype="continuous"/>
        <DataField dataType="double" displayName="Sepal width" name="000002" optype="continuous"/>
        <DataField dataType="double" displayName="Petal length" name="000003" optype="continuous"/>
        <DataField dataType="double" displayName="Petal width" name="000004" optype="continuous"/>
        <DataField dataType="string" displayName="Species" name="000005" optype="categorical">
            <Value value="Iris-setosa"/>
            <Value value="Iris-versicolor"/>
            <Value value="Iris-virginica"/>
        </DataField>
    </DataDictionary>
    <TreeModel algorithmName="mtree" functionName="classification" modelName="">
        <MiningSchema>
            <MiningField name="000001"/>
            <MiningField name="000002"/>
            <MiningField name="000003"/>
            <MiningField name="000004"/>
            <MiningField name="000005" usageType="target"/>
        </MiningSchema>
        <Node recordCount="150" score="Iris-setosa">
            <True/>
            <ScoreDistribution recordCount="50" value="Iris-setosa"/>
            <ScoreDistribution recordCount="50" value="Iris-versicolor"/>
            <ScoreDistribution recordCount="50" value="Iris-virginica"/>
            <Node recordCount="100" score="Iris-versicolor">
                <SimplePredicate field="000003" operator="greaterThan" value="2.45"/>
                <ScoreDistribution recordCount="50" value="Iris-versicolor"/>
                <ScoreDistribution recordCount="50" value="Iris-virginica"/>
                <Node recordCount="46" score="Iris-virginica">
                    <SimplePredicate field="000004" operator="greaterThan" value="1.75"/>
                    <ScoreDistribution recordCount="45" value="Iris-virginica"/>
                    <ScoreDistribution recordCount="1" value="Iris-versicolor"/>
                    <Node recordCount="43" score="Iris-virginica">
                        <SimplePredicate field="000003" operator="greaterThan" value="4.85"/>
                        <ScoreDistribution recordCount="43" value="Iris-virginica"/>
                    </Node>
                    <Node recordCount="3" score="Iris-virginica">
                        <SimplePredicate field="000003" operator="lessOrEqual" value="4.85"/>
                        <ScoreDistribution recordCount="2" value="Iris-virginica"/>
                        <ScoreDistribution recordCount="1" value="Iris-versicolor"/>
                        <Node recordCount="1" score="Iris-versicolor">
                            <SimplePredicate field="000002" operator="greaterThan" value="3.1"/>
                            <ScoreDistribution recordCount="1" value="Iris-versicolor"/>
                        </Node>
                        <Node recordCount="2" score="Iris-virginica">
                            <SimplePredicate field="000002" operator="lessOrEqual" value="3.1"/>
                            <ScoreDistribution recordCount="2" value="Iris-virginica"/>
                        </Node>
                    </Node>
                </Node>
                <Node recordCount="54" score="Iris-versicolor">
                    <SimplePredicate field="000004" operator="lessOrEqual" value="1.75"/>
                    <ScoreDistribution recordCount="49" value="Iris-versicolor"/>
                    <ScoreDistribution recordCount="5" value="Iris-virginica"/>
                    <Node recordCount="6" score="Iris-virginica">
                        <SimplePredicate field="000003" operator="greaterThan" value="4.95"/>
                        <ScoreDistribution recordCount="4" value="Iris-virginica"/>
                        <ScoreDistribution recordCount="2" value="Iris-versicolor"/>
                        <Node recordCount="3" score="Iris-versicolor">
                            <SimplePredicate field="000004" operator="greaterThan" value="1.55"/>
                            <ScoreDistribution recordCount="2" value="Iris-versicolor"/>
                            <ScoreDistribution recordCount="1" value="Iris-virginica"/>
                            <Node recordCount="1" score="Iris-virginica">
                                <SimplePredicate field="000003" operator="greaterThan" value="5.45"/>
                                <ScoreDistribution recordCount="1" value="Iris-virginica"/>
                            </Node>
                            <Node recordCount="2" score="Iris-versicolor">
                                <SimplePredicate field="000003" operator="lessOrEqual" value="5.45"/>
                                <ScoreDistribution recordCount="2" value="Iris-versicolor"/>
                            </Node>
                        </Node>
                        <Node recordCount="3" score="Iris-virginica">
                            <SimplePredicate field="000004" operator="lessOrEqual" value="1.55"/>
                            <ScoreDistribution recordCount="3" value="Iris-virginica"/>
                        </Node>
                    </Node>
                    <Node recordCount="48" score="Iris-versicolor">
                        <SimplePredicate field="000003" operator="lessOrEqual" value="4.95"/>
                        <ScoreDistribution recordCount="47" value="Iris-versicolor"/>
                        <ScoreDistribution recordCount="1" value="Iris-virginica"/>
                        <Node recordCount="1" score="Iris-virginica">
                            <SimplePredicate field="000004" operator="greaterThan" value="1.65"/>
                            <ScoreDistribution recordCount="1" value="Iris-virginica"/>
                        </Node>
                        <Node recordCount="47" score="Iris-versicolor">
                            <SimplePredicate field="000004" operator="lessOrEqual" value="1.65"/>
                            <ScoreDistribution recordCount="47" value="Iris-versicolor"/>
                        </Node>
                    </Node>
                </Node>
            </Node>
            <Node recordCount="50" score="Iris-setosa">
                <SimplePredicate field="000003" operator="lessOrEqual" value="2.45"/>
                <ScoreDistribution recordCount="50" value="Iris-setosa"/>
            </Node>
        </Node>
    </TreeModel>
</PMML>

我通常将 R 用于机器学习,并希望在我的系统中加载并使用此模型进行预测。R 本身有一个 pmml 包,但似乎无法将其用于预测。有没有其他方法可以在 R 中使用这个 PMML 模型进行预测。如果不可能,这个 PMML 模型可以与其他语言(如 python 或 weka)一起使用吗?如果是,我该怎么做(需要代码)。

来自 bigml 的 python 模型

def predict_species(sepal_width=None,
                    petal_length=None,
                    petal_width=None):
    """ Predictor for Species from 

        This is perhaps the best known database to be found in the pattern recognition literature. Fisher's paper is a classic 
        in the field and is referenced frequently to this day. (See Duda & Hart, for example.) The data set contains 3 classes 
        of 50 instances each, where each class refers to a type of iris plant.
        Source
        Iris Data Set[*]
        Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository[*]. Irvine, CA: University of California, School of Information and Computer Science.

        [*]Iris Data Set: http://archive.ics.uci.edu/ml/datasets/Iris
        [*]UCI Machine Learning Repository: http://archive.ics.uci.edu/ml
    """
    if (petal_length is None):
        return u'Iris-setosa'
    if (petal_length > 2.45):
        if (petal_width is None):
            return u'Iris-versicolor'
        if (petal_width > 1.75):
            if (petal_length > 4.85):
                return u'Iris-virginica'
            if (petal_length <= 4.85):
                if (sepal_width is None):
                    return u'Iris-virginica'
                if (sepal_width > 3.1):
                    return u'Iris-versicolor'
                if (sepal_width <= 3.1):
                    return u'Iris-virginica'
        if (petal_width <= 1.75):
            if (petal_length > 4.95):
                if (petal_width > 1.55):
                    if (petal_length > 5.45):
                        return u'Iris-virginica'
                    if (petal_length <= 5.45):
                        return u'Iris-versicolor'
                if (petal_width <= 1.55):
                    return u'Iris-virginica'
            if (petal_length <= 4.95):
                if (petal_width > 1.65):
                    return u'Iris-virginica'
                if (petal_width <= 1.65):
                    return u'Iris-versicolor'
    if (petal_length <= 2.45):
        return u'Iris-setosa'
4

1 回答 1

2

使用 BigML 执行本地预测的最简单方法是直接通过 API 调用下载模型(集成、集群、异常检测器等)。

例如,将BigML 的 Python 绑定用于分类或回归模型,您将执行以下操作:

from bigml.model import Model
model = Model('model/570f4b6e84622c5ed10095a9')
model.predict({'feature_1': 1, 'feature_2': 2})

要使用本地集群找到最近的质心:

from bigml.cluster import Cluster
cluster = Cluster('cluster/572500b849c4a15c9d00451f')
cluster.centroid({'feature_1': 1, 'feature_2': 2})

要使用局部异常检测器对新数据点进行评分:

from bigml.anomaly import Anomaly
anomaly_detector = Anomaly('anomaly/570f4c333bbd21090101e79f')
anomaly_detector.anomaly_score({'feature_1': 1, 'feature_2': 2})

上面的类(Model、Cluster 和 Anomaly)将下载定义每个模型的 JSON PML 代码并将其具体化为本地函数(在本例中为 python)。由于您可能不想使用 R 来实现实际应用程序,因此最好使用您将用于应用程序的语言执行预测:python、node.js、java 等。BigML 提供开放式所有这些的源绑定。

于 2016-04-30T20:44:54.293 回答