nyoka库函数的简介
Nyoka是一个全面支持最新PMML (PMML 4.4)标准的Python库。使用Nyoka,数据科学家可以出口大量的机器学习和深度学习从流行的Python框架PMML模型通过使用任何众多包括现成的出口商或通过创建自己的专业出口商/个人模型类型通过构造函数的调用序列。
除了大约500 Python类,每个封面PMML标签和所有构造函数参数/属性中定义的标准,Nyoka还提供了越来越多的方便的类和函数,简化数据科学家的生活例如通过阅读或编写任何PMML文件在一行代码中你最喜欢的Python环境。
Nyoka提供了完整的Python源代码,扩展的HTML类/函数文档,以及越来越多的Jupyter笔记本教程,帮助您熟悉Nyoka支持您使用PMML作为您最喜欢的数据科学传输文件格式的方式。
Nyoka文档:https://softwareag.github.io/nyoka/
1、Nyoka的核心模块
Statsmodels Exporter Module
Keras Exporter Module
RetinaNet Exporter Module
LightGBM Exporter Module
Pre-Processing Exporter Module
Scikit-Learn Exporter Module
XGBoost Exporter Module
ExponentialSmoothing Exporter Module
Nyoka's Pre-Processing Module
Enums Module
2、模型
(1)、基础模型
linear_model.LinearRegression
linear_model.LogisticRegression
linear_model.RidgeClassifier
linear_model.SGDClassifier
discriminant_analysis.LinearDiscriminantAnalysis
tree.DecisionTreeClassifier
tree.DecisionTreeRegressor
svm.SVC
svm.SVR
svm.LinearSVC
svm.LinearSVR
svm.OneClassSVM
naive_bayes.GaussianNB
ensemble.RandomForestRegressor
ensemble.RandomForestClassifier
ensemble.GradientBoostingRegressor
ensemble.GradientBoostingClassifier
ensemble.IsolationForest
neural_network.MLPClassifier
neural_network.MLPRegressor
neighbors.KNeighborsClassifier
neighbors.KNeighborsRegressor
cluster.KMeans
(2)、LightGBM:
LGBMClassifier
LGBMRegressor
(3)、XGBoost (version <= 0.90):
XGBClassifier
XGBRegressor
(4)、Statsmodels (version <= 0.11.1):
tsa.arima_model.ARIMA
tsa.arima.model.ARIMA (In statespace form)
tsa.statespace.SARIMAX
tsa.statespace.VARMAX
tsa.statespace.ExponentialSmoothing
nyoka库函数的安装
pip install nyoka
pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple nyoka
nyoka库函数的使用方法
1、Nyoka为每个库包含独立的导出程序,例如scikit-learn、keras、xgboost等。
library exporter
scikit-learn skl_to_pmml
xgboost xgboost_to_pmml
lightgbm lgbm_to_pmml
keras KerasToPmml
statsmodels StatsmodelsToPmml & ExponentialSmoothingToPmml
retinanet RetinanetToPmml
2、基于StandardScaler的决策树分类器案例
from sklearn.pipeline import Pipeline
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import StandardScaler
pipeline_obj = Pipeline([
("scaler",StandardScaler()),
("model",DecisionTreeClassifier())
])
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)
from nyoka import skl_to_pmml
skl_to_pmml(pipeline=pipeline_obj,col_names=features,target_name="species",pmml_f_name="decision_tree.pmml")
3、LGBMClassifier → PMML
import pandas as pd
from sklearn import datasets
from sklearn.pipeline import Pipeline
from lightgbm import LGBMRegressor,LGBMClassifier
from nyoka import lgb_to_pmml
iris = datasets.load_iris()
irisd = pd.DataFrame(iris.data,columns=iris.feature_names)
target = 'Species'
irisd[target] = iris.target
features = irisd.columns.drop(target)
#保存模型
pipeline_obj = Pipeline([ ('lgbmc',LGBMClassifier())])
pipeline_obj.fit(irisd[features],irisd[target])
lgb_to_pmml(pipeline_obj,features,target,"lgbmc_pmml.pmml")
#读入数据进行测试
auto = pd.read_csv('auto-mpg.csv')
X = auto.drop(['mpg','car name'], axis=1)
y = auto['mpg']
feature_names = [name for name in auto.columns if name not in ('mpg','car name')]
target_name='mpg'
pipeline_obj = Pipeline([ ('lgbmr',LGBMRegressor()) ])
pipeline_obj.fit(auto[feature_names],auto[target_name])
lgb_to_pmml(pipeline_obj,feature_names,target_name,"lgbmr_pmml.pmml")
4、基于nyoka库利用LGBMClassifier模型实现对iris数据集训练、保存为pmml模型并重新载入pmml模型进而实现推理
ML之nyoka:基于nyoka库利用LGBMClassifier模型实现对iris数据集训练、保存为pmml模型并重新载入pmml模型进而实现推理