Visualization in Python
To visualize the metrics API response, you can use the following Python code in a Jupyter notebook.
Classification Metrics
This example demonstrates how to plot both a confusion matrix and the performance metrics (e.g., accuracy, F1, precision, recall, specificity) over time.
First, we need to fetch the metrics for a specific time window.
import requests
url = f"http://{CLUSTER_IP}/metrics-server/api/v1/metrics/pipeline/classification"
params = {
'namespace': 'seldon',
'pipelineName': 'iris-model-pipeline',
'modelName': 'iris-model',
'startTime': '2025-02-25T11:51:22Z',
'endTime': '2025-02-25T11:53:22Z',
'interval': '10s'
}
response = requests.get(url, params=params)
Next, we can build a confusion matrix for the selected time bucket. For more details on time bucketing, refer to the "Calculating Metrics" page.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
json_data = response.json()
# Choosing a time bucket
time_bucket_index = 0
confusion_matrix_data = json_data['metrics'][time_bucket_index]['confusionMatrix']
# Extracting the confusion matrix values
categories = confusion_matrix_data['categories']
values = confusion_matrix_data['values']
# Reshape the values to form the confusion matrix
matrix_size = len(categories)
confusion_matrix = np.array(values).reshape((matrix_size, matrix_size))
# Creating a pandas dataframe for better visualization
df_cm = pd.DataFrame(confusion_matrix, index=categories, columns=categories)
# Plotting the confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(df_cm, annot=True, fmt='g', cmap='Blues')
plt.title('Confusion Matrix')
plt.xlabel('Predicted Labels')
plt.ylabel('Actual Labels')
plt.show()

Finally, we can plot the Accuracy, Precision, Recall, F1, and Specificity metrics over time.
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(json_data['metrics'])
# Plotting the data
plt.figure(figsize=(10, 6))
for metric in ['accuracy', 'f1', 'precision', 'recall', 'specificity']:
df_filtered = df[df[metric] != -1]
plt.plot(df_filtered['endTime'], df_filtered[metric], label=metric)
plt.xlabel('Time')
plt.ylabel('Metrics')
plt.title('Model Performance Metrics Over Time')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Regression Metrics
This example demonstrates how to plot such performance metrics as Mean Absolute Error, Mean Squared Error, Root Mean Squared Error over time.
Let's fetch the metrics over specific time window first.
import requests
url = f"http://{CLUSTER_IP}/metrics-server/api/v1/metrics/pipeline/regression"
params = {
'namespace': 'seldon',
'pipelineName': 'house-value-pipeline',
'modelName': 'house-value-model',
'startTime': '2025-02-25T16:02:28Z',
'endTime': '2025-02-25T16:04:28Z',
'interval': '5s'
}
response = requests.get(url, params=params)
Finally, we can plot the Mean Absolute Error, Mean Squared Error, Root Mean Squared Error over time.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
json_data = response.json()
df = pd.DataFrame(json_data ['metrics'])
plt.figure(figsize=(10, 6))
for metric in ['meanAbsoluteError', 'meanSquaredError', 'rootMeanSquaredError']:
filtered = df[df[metric] != -1]
plt.plot(filtered['endTime'], filtered[metric], label=metric)
plt.xlabel('Time')
plt.ylabel('Metrics')
plt.title('Model Performance Metrics Over Time')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Last updated
Was this helpful?