github.com/pachyderm/pachyderm@v1.13.4/examples/ml/iris/python/iris-infer-python/pyinfer.py (about)

     1  import pandas as pd
     2  from sklearn.externals import joblib
     3  import argparse
     4  import os
     5  
     6  # command line arguments
     7  parser = argparse.ArgumentParser(description='Train a model for iris classification.')
     8  parser.add_argument('inmodeldir', type=str, help='Input directory containing the training set')
     9  parser.add_argument('inattdir', type=str, help='Input directory containing the input attributes')
    10  parser.add_argument('outdir', type=str, help='Output directory for the trained model')
    11  args = parser.parse_args()
    12  
    13  # attribute column names
    14  features = [
    15      "Sepal_Length",
    16      "Sepal_Width",
    17      "Petal_Length",
    18      "Petal_Width"
    19  ]
    20  
    21  # load the model
    22  mymodel = joblib.load(os.path.join(args.inmodeldir, 'model.pkl')) 
    23  
    24  # walk the input attributes directory and make an
    25  # inference for every attributes file found
    26  for dirpath, dirs, files in os.walk(args.inattdir):
    27      for file in files:
    28  
    29          # read in the attributes
    30          attr = pd.read_csv(os.path.join(dirpath, file), names=features)
    31  
    32          # make the inference
    33          pred = mymodel.predict(attr)
    34  
    35          # save the inference
    36          output = pd.DataFrame(pred, columns=["Species"])
    37          output.to_csv(os.path.join(args.outdir, file.split(".")[0]), header=False, index=False)
    38