github.com/kubeflow/training-operator@v1.7.0/examples/xgboost/xgboost-dist/predict.py (about)

     1  # Licensed under the Apache License, Version 2.0 (the "License");
     2  # you may not use this file except in compliance with the License.
     3  # You may obtain a copy of the License at
     4  #
     5  #     http://www.apache.org/licenses/LICENSE-2.0
     6  #
     7  # Unless required by applicable law or agreed to in writing, software
     8  # distributed under the License is distributed on an "AS IS" BASIS,
     9  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  # See the License for the specific language governing permissions and
    11  # limitations under the License.
    12  
    13  from sklearn.metrics import precision_score
    14  
    15  import logging
    16  import numpy as np
    17  
    18  from utils import extract_xgbooost_cluster_env, read_predict_data, read_model
    19  
    20  
    21  def predict(args):
    22      """
    23      This is the demonstration for the batch prediction
    24      :param args: parameter for model related config
    25      """
    26  
    27      addr, port, rank, world_size = extract_xgbooost_cluster_env()
    28  
    29      dmatrix, y_test = read_predict_data(rank, world_size, None)
    30  
    31      model_path = args.model_path
    32      storage_type = args.model_storage_type
    33      booster = read_model(storage_type, model_path, args)
    34  
    35      preds = booster.predict(dmatrix)
    36  
    37      best_preds = np.asarray([np.argmax(line) for line in preds])
    38      score = precision_score(y_test, best_preds, average='macro')
    39  
    40      logging.info("Predict accuracy: %f", score)