From the uDemy course on LLM engineering.
https://www.udemy.com/course/llm-engineering-master-ai-and-large-language-models
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.1 KiB
97 lines
3.1 KiB
import math |
|
import matplotlib.pyplot as plt |
|
|
|
GREEN = "\033[92m" |
|
YELLOW = "\033[93m" |
|
RED = "\033[91m" |
|
RESET = "\033[0m" |
|
COLOR_MAP = {"red":RED, "orange": YELLOW, "green": GREEN} |
|
|
|
class Tester: |
|
|
|
def __init__(self, predictor, data, title=None, size=250): |
|
self.predictor = predictor |
|
self.data = data |
|
self.title = title or predictor.__name__.replace("_", " ").title() |
|
self.size = size |
|
self.guesses = [] |
|
self.truths = [] |
|
self.errors = [] |
|
self.colors = [] |
|
|
|
def color_for(self, error, truth): |
|
if error == truth: |
|
return "green" |
|
else: |
|
return "red" |
|
|
|
def run_datapoint(self, i): |
|
datapoint = self.data[i] |
|
guess = self.predictor(datapoint) |
|
truth = datapoint.price |
|
error = guess == truth |
|
color = self.color_for(error, truth) |
|
title = datapoint.title if len(datapoint.title) <= 40 else datapoint.title[:40]+"..." |
|
self.guesses.append(guess) |
|
self.truths.append(truth) |
|
self.errors.append(error) |
|
self.colors.append(color) |
|
print(f"{COLOR_MAP[color]}{i+1}: Guess: ${guess:,.2f} Truth: ${truth:,.2f} Error: ${error:,.2f} SLE: {sle:,.2f} Item: {title}{RESET}") |
|
|
|
def chart(self, title): |
|
actual = self.truths |
|
predicted = self.guesses |
|
|
|
# Get unique classes |
|
classes = list(set(actual + predicted)) # Union of unique classes in actual and predicted |
|
|
|
# Initialize the confusion matrix as a dictionary |
|
confusion_matrix = {true: {pred: 0 for pred in classes} for true in classes} |
|
|
|
# Populate the confusion matrix |
|
for a, p in zip(actual, predicted): |
|
confusion_matrix[a][p] += 1 |
|
|
|
# Convert the confusion matrix into a 2D list for visualization |
|
matrix = [[confusion_matrix[true][pred] for pred in classes] for true in classes] |
|
|
|
# Plot the confusion matrix |
|
plt.figure(figsize=(8, 6)) |
|
plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.Blues) |
|
plt.title(title) |
|
plt.colorbar() |
|
|
|
# Add labels |
|
tick_marks = range(len(classes)) |
|
plt.xticks(tick_marks, classes) |
|
plt.yticks(tick_marks, classes) |
|
plt.ylabel('Actual Label') |
|
plt.xlabel('Predicted Label') |
|
|
|
# Add text annotations |
|
for i in range(len(classes)): |
|
for j in range(len(classes)): |
|
plt.text(j, i, matrix[i][j], |
|
horizontalalignment="center", |
|
color="white" if matrix[i][j] > max(max(row) for row in matrix) / 2 else "black") |
|
|
|
plt.tight_layout() |
|
plt.show() |
|
|
|
|
|
def report(self): |
|
average_error = sum(self.errors) / self.size |
|
rmsle = math.sqrt(sum(self.sles) / self.size) |
|
hits = sum(1 for color in self.colors if color=="green") |
|
title = f"{self.title} Error=${average_error:,.2f} RMSLE={rmsle:,.2f} Hits={hits/self.size*100:.1f}%" |
|
self.chart(title) |
|
|
|
def run(self): |
|
self.error = 0 |
|
for i in range(self.size): |
|
self.run_datapoint(i) |
|
self.report() |
|
|
|
@classmethod |
|
def test(cls, function, data): |
|
cls(function, data).run() |