我们首先看代码
# -*- coding: utf-8 -*- """ Created on Wed Oct 20 15:04:12 2021 @author: chenguimei """ from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler import pandas as pd from xgboost import XGBClassifier seed = 123 iris = datasets.load_iris) target = 'Species' features = iris.feature_names iris_df = pd.DataFrameiris.data, columns=features) iris_df[target] = iris.target X, y = iris_df[features], iris_df[target] X_train, X_test, y_train, y_test = train_test_splitX, y, test_size=0.33, random_state=seed) pipeline = Pipeline[ 'scaling', StandardScaler)), 'xgb', XGBClassifiern_estimators=5, seed=seed)) ]) pipeline.fitX_train, y_train) y_pred = pipeline.predictX_test) y_pred_proba = pipeline.predict_probaX_test) #生成pmml文件 from nyoka import xgboost_to_pmml xgboost_to_pmmlpipeline, features, target, "xgb-iris.pmml") #调用pmml from pypmml import Model model = Model.load"xgb-iris.pmml") model.predictX_test)
最后结果:
但是,
但是。。。。
想要跑通上面这段代码,却踩了不少坑
首先执行
model = Model.load"xgb-iris.pmml")
会报错,如果报的是FileNotFoundError: [WinError 2] 系统找不到指定的文件,你以为是路径错了,其实不是,解决方法是安装Java1.8版本,注意不要安装错了,Java官网可能有Java17版本,切记不要安装,只能安装Java1.8版本,在官网找不到,就百度找安装包。安装之后要注意设置环境变量,设置教程https://www.cnblogs.com/maoning/p/10701349.html,注意,不管是个人账号,还是系统账号,都要设置环境变量。
把这些东西做好,重启python,就能跑出结果来。