用于将大量 ML 模型转换为 PMML 的 Python 库
项目描述
尼奥卡
概述
Nyoka 是一个 Python 库,用于全面支持最新的 PMML (PMML 4.4) 标准。使用 Nyoka,数据科学家可以将大量机器学习模型从流行的 Python 框架导出到 PMML,方法是使用众多包含的即用型导出器中的任何一个,或者通过简单地调用构造函数序列。
除了大约 500 个 Python 类,每个类都涵盖标准中定义的 PMML 标记和所有构造函数参数/属性,Nyoka 还提供了越来越多的便利类和函数,例如通过读取或写入任何 PMML 文件,使数据科学家的生活更轻松在您最喜欢的 Python 环境中的一行代码中。
Nyoka 为您提供了完整的 Python 源代码、类/函数的扩展 HTML 文档,以及越来越多的 Jupyter Notebook 教程,可帮助您熟悉 Nyoka 支持您使用 PMML 作为您最喜欢的数据科学传输文件的方式格式。
阅读Nyoka 文档中的文档。
Nyoka 支持的库和模型列表:
Scikit-Learn(版本 <= 0.23.1):
楷模 -
linear_model.LinearRegressionlinear_model.LogisticRegressionlinear_model.RidgeClassifierlinear_model.SGDClassifierdiscriminant_analysis.LinearDiscriminantAnalysistree.DecisionTreeClassifiertree.DecisionTreeRegressorsvm.SVCsvm.SVRsvm.LinearSVCsvm.LinearSVRsvm.OneClassSVMnaive_bayes.GaussianNBensemble.RandomForestRegressorensemble.RandomForestClassifierensemble.GradientBoostingRegressorensemble.GradientBoostingClassifierensemble.IsolationForestneural_network.MLPClassifierneural_network.MLPRegressorneighbors.KNeighborsClassifierneighbors.KNeighborsRegressorcluster.KMeans
预处理 -
preprocessing.StandardScalerpreprocessing.MinMaxScalerpreprocessing.RobustScalerpreprocessing.MaxAbsScalerpreprocessing.LabelEncoderpreprocessing.Imputerpreprocessing.Binarizerpreprocessing.PolynomialFeaturespreprocessing.LabelBinarizerpreprocessing.OneHotEncoderfeature_extraction.text.TfidfVectorizerfeature_extraction.text.CountVectorizerdecomposition.PCAsklearn_pandas.CategoricalImputer(来自sklearn_pandas库)
光GBM:
XGBoost(版本 <= 1.5.2):
Statsmodels(版本 <= 0.11.1):
tsa.arima_model.ARIMAtsa.arima.model.ARIMA(SARIMAX 的扩展)tsa.statespace.SARIMAXtsa.statespace.VARMAXtsa.statespace.ExponentialSmoothing
先决条件
- 蟒蛇> = 3.6
依赖项
尼奥卡要求:
- lxml
安装
您可以使用以下方法安装 nyoka:
pip install --upgrade nyoka
用法
Nyoka 包含每个库的单独导出器,例如 scikit-learn、keras、xgboost 等。
| 图书馆 | 出口商 |
|---|---|
| scikit-学习 | skl_to_pmml |
| xgboost | xgboost_to_pmml |
| lightgbm | lgbm_to_pmml |
| 统计模型 | StatsmodelsToPmml & ExponentialSmoothingToPmml |
Nyoka的主要模块是nyoka. 要将其用于您的模型,您需要从 nyoka 导入特定的导出器 -
from nyoka import skl_to_pmml, lgb_to_pmml #... so on
注意 - 如果使用 scikit-learn、xgboost 和 lightgbm 模型,则该模型应在 sklearn 的管道中使用。
工作流程如下(例如,带有 StandardScaler 的决策树分类器) -
-
创建 scikit-learn 的
Pipeline对象并使用任何预处理步骤和模型对象填充它。from sklearn.pipeline import Pipeline from sklearn.tree import DecisionTreeClassifier from sklearn.preprocessing import StandardScaler pipeline_obj = Pipeline([ ("scaler",StandardScaler()), ("model",DecisionTreeClassifier()) ])
-
调用
Pipeline.fit(X,y)方法来训练模型。from sklearn.dataset import load_iris iris_data = load_iris() X = iris_data.data y = iris_data.target features = iris_data.feature_names pipeline_obj.fit(X,y)
-
使用特定的导出器并将管道对象、训练数据集的特征名称、PMML 的目标名称和预期名称传递给导出器函数。如果没有给出目标名称,
target则使用默认值。同样,对于 pmml 名称,使用默认值from_sklearn.pmml// 。from_xgboost.pmmlfrom_lighgbm.pmmlfrom nyoka import skl_to_pmml skl_to_pmml(pipeline=pipeline_obj,col_names=features,target_name="species",pmml_f_name="decision_tree.pmml")
对于 Statsmodels,管道不是必需的。拟合模型需要传递给导出器。
import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
from nyoka import StatsmodelsToPmml
sales_data = pd.read_csv('sales-cars.csv', index_col=0, parse_dates = True)
model = ARIMA(sales_data, order = (4, 1, 2))
result = model.fit()
StatsmodelsToPmml(result,"Sales_cars_ARIMA.pmml")
例子
示例 jupyter 笔记本可以在nyoka/examples. 这些文件包含代码来展示如何使用不同的导出器。
-
将模型导出
scikit-learn到 PMML -
将模型导出
XGBoost到 PMML -
将模型导出
LightGBM到 PMML -
将模型导出
statsmodels到 PMML
Nyoka 子模块
Nyoka 包含一个名为preprocessing. 该模块包含 Nyoka 实现的预处理类。目前只有一个预处理类,即Lag.
什么是滞后?什么时候使用它?
Lag 是 Nyoka 实现的预处理类。当在 scikit-learn 的管道中使用时,它只是通过组合先前记录的数量来
aggregation为数据集的给定特征应用一个函数。value它需要两个参数 - 聚合和值。
有效
aggregation函数是 - “min”、“max”、“sum”、“avg”、“median”、“product”和“stddev”。
使用滞后-
- 从 nyoka 导入它 -
from nyoka.preprocessing import Lag
- 创建一个滞后实例 -
lag_obj = Lag(aggregation="sum", value=5) ''' This means taking previous 5 values and perform `sum`. When used inside pipeline, this will be applied to all the columns. If used inside DataFrameMapper, the it will be applied to only those columns which are inside DataFrameMapper. '''
- 在 scikit-learn 的管道中使用此对象进行训练。
from sklearn.pipeline import Pipeline from sklearn.tree import DecisionTreeClassifier from nyoka.preprocessing import Lag pipeline_obj = Pipeline([ ("lag",Lag(aggregation="sum",value=5)), ("model",DecisionTreeClassifier()) ])
卸载
pip uninstall nyoka
支持
您可以在以下位置提问:
- Stack Overflow通过使用#pmml、#nyoka 标记您的问题
- 您还可以在GitHub 问题中发布错误报告
请注意,这个项目是随贡献者行为准则一起发布的。通过参与本项目,您同意遵守其条款。
这些工具按原样提供,不提供保修或支持。它们不构成 Software AG 产品套件的一部分。用户可以根据许可协议自由使用、分叉和修改它们。虽然 Software AG 欢迎贡献,但我们不能保证将所有贡献都包含在主项目中。