此代码将帮助您获取我需要用C ++打开的model.pkl文件
# Load libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from sklearn.externals import joblib
# Load data
iris = datasets.load_iris()
features = iris.data
target = iris.target
# Create decision tree classifer object
classifer = RandomForestClassifier()
# Train model
model = classifer.fit(features, target)
# Save the model as pickle file
joblib.dump(model, "model.pkl")
因此,从以上代码中,我们获得了model.pkl文件,该文件是机器学习模型(随机森林分类器)。现在,我需要使用C ++读取model.pkl文件,并使用示例数据(new_observation)测试模型。我可以在python中执行以下操作:
from sklearn.externals import joblib
# Load model from file
classifer = joblib.load("model.pkl")
# Create new observation
new_observation = [[ 5.2, 3.2, 1.1, 0.1]]
# Predict observation's class
classifer.predict(new_observation)
但是需要使用C ++做到这一点,基本上,我需要上面不知道的4行代码(从python到C ++)的等效代码。