-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsumer-predict.py
More file actions
48 lines (37 loc) · 1.3 KB
/
consumer-predict.py
File metadata and controls
48 lines (37 loc) · 1.3 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
from settings import ANOMALIES_TOPIC, TRANSACTIONS_CONSUMER_GROUP, TRANSACTIONS_TOPIC
from streaming.utils import create_consumer, create_producer
import pandas as pd
import numpy as np
import logging
import json
from joblib import load
clf = load("models/isolation_forest.joblib")
consumer = create_consumer(topic=TRANSACTIONS_TOPIC, group_id=TRANSACTIONS_CONSUMER_GROUP)
producer = create_producer()
while True:
message = consumer.poll(timeout=50)
if message is None:
continue
if message.error():
logging.error("Consumer error: {}".format(message.error()))
continue
# Message that came from producer
record = json.loads(message.value().decode('utf-8'))
print(record)
test_data = np.delete(record, 0)
data = np.array(test_data, dtype=float).reshape(1, -1)
prediction = clf.predict(data)
if prediction == -1:
print("prediction")
print(prediction)
score = clf.score_samples(data)
print("score")
print(score)
roundes_score = np.round(score, 3).tolist()
print("roundes_score")
print(roundes_score)
record_bytes = json.dumps(record).encode("utf-8")
producer.produce(topic=ANOMALIES_TOPIC,
value=record_bytes)
producer.flush()
consumer.flush()