import os
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn.decomposition import PCA
from datmo.monitoring import Monitoring
# Set save location
save_directory = os.path.dirname(os.path.realpath(__file__))
# Set datmo client
datmo_client = Monitoring(api_key="6a3a3cd900eaf7b406a41d68f8ca7969")
filter_schema = {"model_id": "{{ model.id }}",
"deployment_version_id": "{{ deployment_version_id }}",
"model_version_id": "{{ model_version_id }}"}
# Get data during inference from datmo
rows = datmo_client.search_metadata(filter_schema)
# Form pandas dataframe for the prediction data
inputs = []
predictions = []
for row in rows:
prediction = {'Class': row['prediction']['prediction']}
input_dict = row['input']
inputs.append(input_dict)
predictions.append(prediction)
# Transform and predict using model
X = pd.DataFrame(inputs)
y = pd.DataFrame(predictions)
X.sort_index(axis=1,inplace=True)
# Perform PCA over the input features during the prediction
t0 = time.time()
X_reduced_pca = PCA(n_components=2, random_state=42).fit_transform(X.values)
t1 = time.time()
# Plot the graph and save them
plt.suptitle('Clusters using Dimensionality Reduction', fontsize=14)
blue_patch = mpatches.Patch(color='#0A0AFF', label='No Fraud')
red_patch = mpatches.Patch(color='#AF0000', label='Fraud')
new_y = y.values.ravel()
plt.scatter(X_reduced_pca[:,0], X_reduced_pca[:,1], c=(new_y == 0), cmap='coolwarm', label='No Fraud', linewidths=2)
plt.scatter(X_reduced_pca[:,0], X_reduced_pca[:,1], c=(new_y == 1), cmap='coolwarm', label='Fraud', linewidths=2)
plt.legend(handles=[blue_patch, red_patch])
plt.grid(True)
plt.savefig(os.path.join(save_directory, 'image.jpg'))