作为机器学习部署项目的一部分,我构建了一个概念证明,其中我使用R的glm
函数和python的scikit-learn
为二进制分类任务创建了两个简单的逻辑回归模型。之后,我使用R中的PMML
函数和Python中的pmml
函数将那些训练有素的简单模型转换为from sklearn2pmml.pipeline import PMMLPipeline
。
接下来,我在KNIME中打开了一个非常简单的工作流程,看是否可以将这两个PMML
付诸实践。基本上,此概念验证的目标是测试IT是否可以使用我直接交给他们的PMML
来对新数据进行评分。该练习必须产生概率,就像原始的逻辑回归一样。
在KNIME中,我使用CSV Reader
节点读取了仅4行的测试数据,使用PMML
节点读取了PMML Reader
,最后使用该模型对测试数据进行了评分PMML Predictor
节点。问题在于预测不是我想要的最终概率,而是在此之前的最后一步(系数之和乘以独立变量值,我猜是XBETA)。请在下图中查看工作流程和预测:
要获得最终的概率,需要通过sigmoid函数运行这些数字。因此基本上,对于第一条记录,我需要1/(1+exp(-2.654)) = 0.93
而不是2.654。我确定PMML
文件包含使KNIME(或任何其他类似平台)能够为我执行此S型操作的必需信息,但是我找不到它。那是我急切需要帮助的地方。
我查看了regression和general regression PMML
文档,我的PMML看起来很好,但是我不知道为什么我无法获得这些概率。
我们非常感谢您的帮助!
age credit payfreq gmi
25 550 4 1500
27 650 4 3400
35 600 2 3200
40 680 2 4000
<?xml version="1.0"?>
<PMML version="4.2" xmlns="http://www.dmg.org/PMML-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dmg.org/PMML-4_2 http://www.dmg.org/v4-2/pmml-4-2.xsd">
<Header copyright="Copyright (c) 2018 fakici" description="Generalized Linear Regression Model">
<Extension name="user" value="fakici" extender="Rattle/PMML"/>
<Application name="Rattle/PMML" version="1.4"/>
<Timestamp>2018-10-30 17:36:39</Timestamp>
</Header>
<DataDictionary numberOfFields="5">
<DataField name="bad" optype="categorical" dataType="double"/>
<DataField name="age" optype="continuous" dataType="double"/>
<DataField name="credit" optype="continuous" dataType="double"/>
<DataField name="payfreq" optype="continuous" dataType="double"/>
<DataField name="gmi" optype="continuous" dataType="double"/>
</DataDictionary>
<GeneralRegressionModel modelName="General_Regression_Model" modelType="generalLinear" functionName="regression" algorithmName="glm" distribution="binomial" linkFunction="logit" targetReferenceCategory="1">
<MiningSchema>
<MiningField name="bad" usageType="predicted" invalidValueTreatment="returnInvalid"/>
<MiningField name="age" usageType="active" invalidValueTreatment="returnInvalid"/>
<MiningField name="credit" usageType="active" invalidValueTreatment="returnInvalid"/>
<MiningField name="payfreq" usageType="active" invalidValueTreatment="returnInvalid"/>
<MiningField name="gmi" usageType="active" invalidValueTreatment="returnInvalid"/>
</MiningSchema>
<Output>
<OutputField name="Predicted_bad" feature="predictedValue"/>
</Output>
<ParameterList>
<Parameter name="p0" label="(Intercept)"/>
<Parameter name="p1" label="age"/>
<Parameter name="p2" label="credit"/>
<Parameter name="p3" label="payfreq"/>
<Parameter name="p4" label="gmi"/>
</ParameterList>
<FactorList/>
<CovariateList>
<Predictor name="age"/>
<Predictor name="credit"/>
<Predictor name="payfreq"/>
<Predictor name="gmi"/>
</CovariateList>
<PPMatrix>
<PPCell value="1" predictorName="age" parameterName="p1"/>
<PPCell value="1" predictorName="credit" parameterName="p2"/>
<PPCell value="1" predictorName="payfreq" parameterName="p3"/>
<PPCell value="1" predictorName="gmi" parameterName="p4"/>
</PPMatrix>
<ParamMatrix>
<PCell parameterName="p0" df="1" beta="14.4782176066955"/>
<PCell parameterName="p1" df="1" beta="-0.16633241754673"/>
<PCell parameterName="p2" df="1" beta="-0.0125492006930571"/>
<PCell parameterName="p3" df="1" beta="0.422786551151072"/>
<PCell parameterName="p4" df="1" beta="-0.0005500245399861"/>
</ParamMatrix>
</GeneralRegressionModel>
</PMML>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PMML xmlns="http://www.dmg.org/PMML-4_2" xmlns:data="http://jpmml.org/jpmml-model/InlineTable" version="4.2">
<Header>
<Application name="JPMML-SkLearn" version="1.5.8"/>
<Timestamp>2018-10-30T22:10:32Z</Timestamp>
</Header>
<MiningBuildTask>
<Extension>PMMLPipeline(steps=[('mapper', DataFrameMapper(default=False, df_out=False,
features=[(['age', 'credit', 'payfreq', 'gmi'], [ContinuousDomain(high_value=None, invalid_value_replacement=None,
invalid_value_treatment='return_invalid', low_value=None,
missing_value_replacement=None, missing_value_treatment='as_is',
missing_values=None, outlier_treatment='as_is', with_data=True,
with_statistics=True), Imputer(axis=0, copy=True, missing_values='NaN', strategy='mean', verbose=0)])],
input_df=False, sparse=False)),
('classifier', LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
verbose=0, warm_start=False))])</Extension>
</MiningBuildTask>
<DataDictionary>
<DataField name="bad" optype="categorical" dataType="double">
<Value value="0"/>
<Value value="1"/>
</DataField>
<DataField name="age" optype="continuous" dataType="double">
<Interval closure="closedClosed" leftMargin="20.0" rightMargin="50.0"/>
</DataField>
<DataField name="credit" optype="continuous" dataType="double">
<Interval closure="closedClosed" leftMargin="501.0" rightMargin="699.0"/>
</DataField>
<DataField name="payfreq" optype="continuous" dataType="double">
<Interval closure="closedClosed" leftMargin="2.0" rightMargin="4.0"/>
</DataField>
<DataField name="gmi" optype="continuous" dataType="double">
<Interval closure="closedClosed" leftMargin="1012.0" rightMargin="4197.0"/>
</DataField>
</DataDictionary>
<RegressionModel functionName="classification" normalizationMethod="softmax" algorithmName="glm" targetFieldName="bad">
<MiningSchema>
<MiningField name="bad" usageType="target"/>
<MiningField name="age" missingValueReplacement="35.05" missingValueTreatment="asMean"/>
<MiningField name="credit" missingValueReplacement="622.28" missingValueTreatment="asMean"/>
<MiningField name="payfreq" missingValueReplacement="2.74" missingValueTreatment="asMean"/>
<MiningField name="gmi" missingValueReplacement="3119.4" missingValueTreatment="asMean"/>
</MiningSchema>
<Output>
<OutputField name="probability(0)" optype="categorical" dataType="double" feature="probability" value="0"/>
<OutputField name="probability(1)" optype="categorical" dataType="double" feature="probability" value="1"/>
</Output>
<ModelStats>
<UnivariateStats field="age">
<Counts totalFreq="100.0" missingFreq="0.0" invalidFreq="0.0"/>
<NumericInfo minimum="20.0" maximum="50.0" mean="35.05" standardDeviation="9.365228240678386" median="40.5" interQuartileRange="18.0"/>
</UnivariateStats>
<UnivariateStats field="credit">
<Counts totalFreq="100.0" missingFreq="0.0" invalidFreq="0.0"/>
<NumericInfo minimum="501.0" maximum="699.0" mean="622.28" standardDeviation="76.1444784603585" median="662.0" interQuartileRange="150.5"/>
</UnivariateStats>
<UnivariateStats field="payfreq">
<Counts totalFreq="100.0" missingFreq="0.0" invalidFreq="0.0"/>
<NumericInfo minimum="2.0" maximum="4.0" mean="2.74" standardDeviation="0.9656086163658655" median="2.0" interQuartileRange="2.0"/>
</UnivariateStats>
<UnivariateStats field="gmi">
<Counts totalFreq="100.0" missingFreq="0.0" invalidFreq="0.0"/>
<NumericInfo minimum="1012.0" maximum="4197.0" mean="3119.4" standardDeviation="1282.4386379082625" median="4028.5" interQuartileRange="2944.0"/>
</UnivariateStats>
</ModelStats>
<RegressionTable targetCategory="1" intercept="0.9994024132088255">
<NumericPredictor name="age" coefficient="-0.1252021965856186"/>
<NumericPredictor name="credit" coefficient="-8.682780007730786E-4"/>
<NumericPredictor name="payfreq" coefficient="1.2605378393614861"/>
<NumericPredictor name="gmi" coefficient="1.4681704138387003E-4"/>
</RegressionTable>
<RegressionTable targetCategory="0" intercept="0.0"/>
</RegressionModel>
</PMML>
答案 0 :(得分:2)
一种浮动的解决方案是使用“数学公式”节点将sigmoid函数应用于PMML Predictor的输出。你有尝试过吗?
答案 1 :(得分:-1)
阅读有关KNIME中全面PMML预处理的研究论文。
最近的KNIME版本(版本2.4)带有附加的PMML功能。扩展功能,许多预处理节点提供PMML支持,并且可以包含在生成的PMML文档中。此功能允许在KNIME中对整个数据处理流程进行可视化建模,并将其导出到PMML。本文中的工作流程显示了如何将多个预处理步骤添加到学习的PMML聚类模型中。 KNIME中支持PMML预处理的基本思想是,所有能够提供或解释PMML的预处理节点都被赋予了附加的“ PMML端口”。