现在两个模型都在 MLServer 上运行,我们可以使用测试集中的样本来验证每个模型的准确性。
以下代码向每个模型发送一个批处理请求(包含完整的测试集),然后将预测值与真实标签进行比较。在整个测试集上执行此操作,为我们提供了衡量每个模型准确性的方法,并将最终结果打印出来。
import pandas as pdimport requestsimport json#Import the test data and split the data from the labelstest = pd.read_csv('./data/fashion-mnist_test.csv', header=0)y_test = test['label']X_test = test.drop(['label'],axis=1)#Build the inference requestinference_request = { "inputs": [ { "name": "predict", "shape": X_test.shape, "datatype": "FP64", "data": X_test.values.tolist() } ]}#Send the prediction request to the relevant model, compare responses to training labels and calculate accuracydef infer(model_name, version): endpoint = f"http://localhost:8080/v2/models/{model_name}/versions/{version}/infer" response = requests.post(endpoint, json=inference_request) #calculate accuracy correct = 0 for i, prediction in enumerate(json.loads(response.text)['outputs'][0]['data']): if y_test == prediction: correct += 1 accuracy = correct / len(y_test) print(f'Model Accuracy for {model_name}: {accuracy}')infer("fashion-xgboost", "v1")infer("fashion-sklearn", "v1")1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.结果表明,XGBoost 模型略微优于 SVM scikit-learn 模型:
Model Accuracy for fashion-xgboost: 0.8953Model Accuracy for fashion-sklearn: 0.8641.2.总结