1

我正在使用 Scikit-Learn 应用 SVM 算法来预测客户是否会选择房屋贷款。我希望将模型导出为 PMML 格式。数据集中的特征和标签如下所示:

特征
1. 访问频率
2. 对报价的响应
3. 网上银行设施
的使用 4. 储蓄账户
数量 5. 支票账户
数量 6. 开出的支票
数量 7. 数量完成的电子转帐
8. 获得的财产
9. 其他贷款行为
10. 收入

标签
是房屋贷款

模型生成正确,但无法导出到 PMML。代码粘贴在下面:

代码:

from sklearn.decomposition import PCA

from sklearn2pmml.decoration import ContinuousDomain

import pandas
import sklearn_pandas
from sklearn.svm import SVC



home_loan = pandas.read_csv('home-loan-dataset.csv')

home_loan = home_loan.drop(['CustID'], axis=1)

home_loan_df = pandas.concat((pandas.DataFrame(home_loan[:], columns = ['Frequencyofvisits','Responsetooffers','UsageofOnlineBankingFacility','Numberofsavingsaccount','Numberofcheckingaccount','Numberofcheckswritten','NumberofEFTsdone','PropertyAcquired','OtherLoansBehaviour','Income']), pandas.DataFrame(home_loan['IsHouseLoan'], columns = ["IsHouseLoan"])), axis = 1)

home_loan_mapper = sklearn_pandas.DataFrameMapper([
    (['Frequencyofvisits','Responsetooffers','UsageofOnlineBankingFacility','Numberofsavingsaccount','Numberofcheckingaccount','Numberofcheckswritten','NumberofEFTsdone','PropertyAcquired','OtherLoansBehaviour','Income'], [ContinuousDomain(), PCA(n_components = 3)]),
    ("IsHouseLoan", None)
])


home_loan = home_loan_df


home_loan_X = home_loan[['Frequencyofvisits','Responsetooffers','UsageofOnlineBankingFacility','Numberofsavingsaccount','Numberofcheckingaccount','Numberofcheckswritten','NumberofEFTsdone','PropertyAcquired','OtherLoansBehaviour','Income']]

home_loan_y = home_loan[['IsHouseLoan']]

# Classify using SVM

home_loan_classifier = SVC()



home_loan_classifier.fit(home_loan_X, home_loan_y.values.ravel())

SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)



#
# Conversion to PMML
#

from sklearn2pmml import sklearn2pmml

sklearn2pmml(home_loan_classifier, home_loan_mapper, "SVMHomeLoan.pmml", with_repr = True)


转换为 PMML 时显示以下错误:

错误:

C:\Python27\python.exe C:/Users/Admin/PycharmProjects/ML-Programs/Bank-Customer-Segmentation/svm-pmml.py
Aug 17, 2016 11:35:01 AM org.jpmml.sklearn.Main run
INFO: Parsing DataFrameMapper PKL..
Aug 17, 2016 11:35:01 AM org.jpmml.sklearn.Main run
INFO: Parsed DataFrameMapper PKL in 30 ms.
Aug 17, 2016 11:35:01 AM org.jpmml.sklearn.Main run
INFO: Converting DataFrameMapper..
Aug 17, 2016 11:35:01 AM org.jpmml.sklearn.Main run
SEVERE: Failed to convert DataFrameMapper
java.lang.IllegalArgumentException: The value of the sklearn2pmml.decoration.ContinuousDomain.data_min_ attribute (null) is not a supported array type
    at org.jpmml.sklearn.ClassDictUtil.getArray(ClassDictUtil.java:51)
    at sklearn2pmml.decoration.ContinuousDomain.getDataMin(ContinuousDomain.java:111)
    at sklearn2pmml.decoration.ContinuousDomain.encodeFeatures(ContinuousDomain.java:50)
    at sklearn_pandas.DataFrameMapper.encodeFeatures(DataFrameMapper.java:70)
    at org.jpmml.sklearn.Main.run(Main.java:146)
    at org.jpmml.sklearn.Main.main(Main.java:107)

Exception in thread "main" java.lang.IllegalArgumentException: The value of the sklearn2pmml.decoration.ContinuousDomain.data_min_ attribute (null) is not a supported array type
    at org.jpmml.sklearn.ClassDictUtil.getArray(ClassDictUtil.java:51)
    at sklearn2pmml.decoration.ContinuousDomain.getDataMin(ContinuousDomain.java:111)
    at sklearn2pmml.decoration.ContinuousDomain.encodeFeatures(ContinuousDomain.java:50)
    at sklearn_pandas.DataFrameMapper.encodeFeatures(DataFrameMapper.java:70)
    at org.jpmml.sklearn.Main.run(Main.java:146)
    at org.jpmml.sklearn.Main.main(Main.java:107)
Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/ML-Programs/Bank-Customer-Segmentation/svm-pmml.py", line 52, in <module>
    sklearn2pmml(home_loan_classifier, home_loan_mapper, "SVMHomeLoan.pmml", with_repr = True)
  File "C:\Python27\lib\site-packages\sklearn2pmml\__init__.py", line 56, in sklearn2pmml
    subprocess.check_call(cmd)
  File "C:\Python27\lib\subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['java', '-cp', 'C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\guava-19.0.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\istack-commons-runtime-2.21.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\jaxb-core-2.2.11.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\jaxb-runtime-2.2.11.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\jcommander-1.48.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\jpmml-converter-1.0.7.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\jpmml-sklearn-1.0-SNAPSHOT.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\jpmml-xgboost-1.0.5.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\pmml-agent-1.2.16.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\pmml-model-1.2.16.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\pmml-model-metro-1.2.16.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\pmml-schema-1.2.16.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\pyrolite-4.12.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\serpent-1.12.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\slf4j-api-1.7.21.jar;C:\\Python27\\lib\\site-packages\\sklearn2pmml\\resources\\slf4j-jdk14-1.7.21.jar', 'org.jpmml.sklearn.Main', '--pkl-estimator-input', 'c:\\users\\Admin\\appdata\\local\\temp\\tmplgmrjq.pkl', '--repr-estimator', "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',\n  max_iter=-1, probability=False, random_state=None, shrinking=True,\n  tol=0.001, verbose=False)", '--pkl-mapper-input', 'c:\\users\\Admin\\appdata\\local\\temp\\tmpobahse.pkl', '--repr-mapper', "DataFrameMapper(features=[(['Frequencyofvisits', 'Responsetooffers', 'UsageofOnlineBankingFacility', 'Numberofsavingsaccount', 'Numberofcheckingaccount', 'Numberofcheckswritten', 'NumberofEFTsdone', 'PropertyAcquired', 'OtherLoansBehavior', 'Income100000'], TransformerPipeline(steps=[('continuousdomain', ContinuousDomain(invalid_value_treatment='return_invalid')), ('pca', PCA(copy=True, n_components=3, whiten=False))])), ('IsHouseLoan', None)],\n        sparse=False)", '--pmml-output', 'SVMHomeLoan.pmml']' returned non-zero exit status 1


可能是什么原因 ?

4

1 回答 1

2

显然,您的数据列之一不符合sklearn2pmml.decoration.ContinuousDomain转换的预期。在没有看到您的数据的情况下,不可能说出哪一列以及问题的确切性质是什么(例如,分类操作类型而不是连续的、错误的数字数据类型、列包含 NA 值等)。

您在这里有两个选择:

  1. 识别行为异常的列,并修复数据问题,以便ContinuousDomain转换正常工作。
  2. ContinuousDomain从列表转换列表中排除。

目前,您正在使用直接从 sklearn2pmml README.md 文件复制的数据预处理逻辑。请重新处理它以匹配您的数据 -[ContinuousDomain(), PCA(n_components = 3)]转换不太可能是您的用例的正确解决方案。

此外,这个问题是相当特定于sklearn2pmml包的。如果您在 sklearn2pmml 问题跟踪器中打开问题,您可能会得到更好/更快的回复。

于 2016-08-17T06:42:18.553 回答