将输入传递给函数以获取预测

时间:2019-05-08 20:50:34

标签: python pandas machine-learning scikit-learn cloudera

我正在尝试通过REST API函数(它的新增功能)部署模型,以将预测动态应用于未标记的数据。 我不明白如何将输入功能映射到args(或者应该是kwargs)功能

使用这个简单的示例作为指导

# Read the fitted model from the file model.pkl
# and define a function that uses the model to
# predict petal width from petal length

import pickle
import numpy as np

model = pickle.load(open('model.pkl', 'rb'))

def predict(args):
  iris_x = np.reshape(float(args.get('petal_length')), (-1,1))
  result = model.predict(iris_x)
  return result[0][0]

predict({'petal_length': 3})
0.88346654397265678

# doing it manually via dataframe

row = {'petal_length': 3}
df = pd.DataFrame([row])
result = model.predict(df)
result[0][0]
0.88346654397265678

我有一个带有13个要素的输入数据框,这些要素具有混合的数据类型,这些要素插入到sklearn分类管道中,执行缩放,ohe和tfidf之类的FE技术... api函数不起作用,并且看起来与基于错误的形状...由于sklearn管线,似乎输入格式必须为数据框

model = joblib.load(persisted_model)

def preds(args):
  unlabeled_X = np.reshape(args\
                 .get('c1',\
                     'c2',\
                     'c3',\
                     'c4',\
                     'c5',\
                     'c6',\
                     'c7',\
                     'c8',\
                     'c9',\
                     'c10',\
                     'c11',\
                     'c12',\
                     'c13'), (-1,13))
  result = model.predict(unlabeled_X)
  return result[0][0]

# using dummy data as example
preds({
  "c1": "some text",
  "c2": "more text",
  "c3": 0,
  "c4": 0,
  "c5": 0,
  "c6": "cat var",
  "c7": "cat var",
  "c8": "cat var",
  "c9": "flag",
  "c10": 0,
  "c11": 0,
  "c12": 0,
  "c13": 0
})

TypeError: get expected at most 2 arguments, got 13

如果我尝试通过dict手动操作到df,

# using dummy data as example
row = {
  "c1": "some text",
  "c2": "more text",
  "c3": 0,
  "c4": 0,
  "c5": 0,
  "c6": "cat var",
  "c7": "cat var",
  "c8": "cat var",
  "c9": "flag",
  "c10": 0,
  "c11": 0,
  "c12": 0,
  "c13": 0
}

df = pd.DataFrame([row])
model.predict(df)[0]
'CLASS1'

我的主要问题是如何使api函数起作用,以便它可以接受新数据进行预测?

也许可以修改函数以将字典输入转换为数据框,以便可以调用API?

谢谢!

0 个答案:

没有答案