使用线性回归我试图根据温度,真空,压力和湿度的值来预测产生的功率,这些值受到启发并改编自" http://datascience-enthusiast.com"并将模型应用于Kafka主题的实时数据。正确生成pickle .pkl.z文件并使用建议的https://github.com/jpmml/jpmml-sklearn使用JPMML转换为PMML。
Kafka Producer,一个Python程序(kafka_producer.py)在float中随机生成某些范围内的数据并转换为字符串并将其作为字节发送到Kafka主题。
Kafka Consumer,一个作为Openscoring python客户端的python程序(kafka_consumer.py),从Kafka主题读取数据,将字节字符串转换为字符串,最后转换为形成参数的字典,如arguments = {" AT" :9.2," V" :39.82," AP" :1013.19," RH" :91.25} for result = os.evaluate(" CCPP",arguments)声明。
效果很好并预测能力,但在正确显示4到10条记录的结果后, Openscoring服务器抛出
SEVERE: INFO:
Received EvaluationRequest{id=null, arguments={AT=12.12, V=41.35, AP=1031.67, RH=66.32}}
Nov 20, 2017 6:39:16 AM org.openscoring.service.ModelResource evaluate
INFO: Returned EvaluationResponse{id=null, result={PE=472.152110955029}}
Nov 20, 2017 6:39:17 AM org.openscoring.service.ModelResource evaluate
INFO: Received EvaluationRequest{id=null, arguments={AT=34.06, V=51.53, AP=1016.22, RH=91.7}}
Nov 20, 2017 6:39:17 AM org.openscoring.service.ModelResource evaluate
INFO: Returned EvaluationResponse{id=null, result={PE=444.9147880324237}}
Nov 20, 2017 6:39:18 AM org.openscoring.service.ModelResource evaluate
INFO: Received EvaluationRequest{id=null, arguments={AT=20.41, V=50.33, AP=1018.19, RH=100.18}}
Nov 20, 2017 6:39:18 AM org.openscoring.service.ModelResource doEvaluate
**SEVERE: Failed to evaluate**
org.jpmml.evaluator.InvalidResultException (at or around line 130)
at org.jpmml.evaluator.FieldValueUtil.performInvalidValueTreatment(FieldValueUtil.java:178) at org.jpmml.evaluator.FieldValueUtil.prepareInputValue(FieldValueUtil.java:90)
at org.jpmml.evaluator.InputField.prepare(InputField.java:64)
Kafka Consumer停止并显示:提高异常(self.message) 例外:错误请求
kafka_producer.py
import random
import time
from kafka import KafkaProducer
from kafka.errors import KafkaError
producer = KafkaProducer(bootstrap_servers='localhost:9092')
topic = "power"
for i in range(1000):
AT = "19.651231"
V = "54.305804"
AP = "1013.259078"
RH = "73.308978"
def getAT():
return str(round(random.uniform(2.0, 38.0),2))
def getV():
return str(round(random.uniform(26.0, 81.5),2))
def getAP():
return str(round(random.uniform(993.0, 1033.0),2))
def getRH():
return str(round(random.uniform(26.0, 101.0),2))
# arguments = {"AT" :9.2, "V" : 39.82, "AP" : 1013.19, "RH" : 91.25}
message = "{"AT" : " + getAT() + "," + ""V" : " +getV() + "," + ""AP" : " +getAP() + "," + ""RH" : " + getRH() + "}"
producer.send(topic, key=str.encode('key_{}'.format(i)), value=(message.encode('utf-8')))
time.sleep(1)
producer.close()
kafka_consumer.py
import ast
from kafka import KafkaConsumer
import openscoring
import os
os = openscoring.Openscoring("http://localhost:8080/openscoring")
kwargs = {"auth" : ("admin", "adminadmin")}
os.deploy("CCPP", "/home/gopinathankm/jpmml-sklearn-master/ccpp.pmml", **kwargs)
consumer = KafkaConsumer('power', bootstrap_servers='localhost:9092')
for message in consumer:
arguments =message.value
argsdict = arguments.decode("utf-8")
dict = ast.literal_eval(argsdict)
print(dict)
result = os.evaluate("CCPP", dict)
print(result)
对于生成的某些数据,它无法正常工作,我真的不知道请求的生成有多糟糕。 任何帮助将受到高度赞赏。 问候 Gopinathan K.M
答案 0 :(得分:1)
获得Villu Ruusmann的帮助和解决方案如下,以便其他人可以受益:
https://github.com/jpmml/jpmml-evaluator/issues/84
"异常类型InvalidResultException意味着无法成功完成模型的评估,因为一个或多个输入字段值超出其声明的范围。 PMML文档和Python脚本的值范围不匹配。删除PMML文档值范围(以便所有输入值都被视为有效)或合同Python脚本值范围。"正如Villu Ruusmann所建议的那样。