Page MenuHomec4science

compute_stats.py
No OneTemporary

File Metadata

Created
Fri, Jul 4, 18:05

compute_stats.py

#!/usr/bin/env python
# coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
def compute_statistics_model (y_pred, y_truth, print_stats=True, print_graph=True) :
"""
Method that computes usefull information to evaluate a model according to the predictions is produced. This method computes the absolute error, its mean, variance, max value, min value, first quantile, second quanitle (median) and the 3rd quantile.
It also computes the NRMSE (see report for def.) and the coefficient of determination (a.k.a as coefficient of determination).
"""
abs_error = np.abs(y_pred - y_truth)
mean = abs_error.mean()
var = abs_error.var()
max_ = abs_error.max()
min_ = abs_error.min()
q1 = np.quantile(abs_error, 0.25)
q2 = np.quantile(abs_error, 0.5)
q3 = np.quantile(abs_error, 0.75)
NRMSE = np.sqrt(np.sum(abs_error**2)/abs_error.shape[0])/(y_pred.mean())
determination_coefficient = 1 - np.sum((y_truth - y_pred)**2)/np.sum((y_truth - y_truth.mean())**2)
if print_stats :
print("######################################")
print("NRMSE is : ", NRMSE)
print("Coefficient of determination (i.e R^2 score) : ", determination_coefficient)
print("Mean absolute error is : ", mean)
print("Max asbolute error is : ", max_)
print("Min absolute error is : ", min_)
print("1st quantile of abs. error : ", q1)
print("2nd quantile (i.e median) of abs. error :", q2)
print("3rd quantile of abs. error: ", q3)
if print_graph :
plt1 = plt.boxplot(abs_error, sym='')
plt1 = plt.title("Box-plot of the distribution of the absolute error")
plt.show()
plt.hist(y_pred - y_truth, normed=False, bins=60)
plt.title("Histogram of difference between ground truth and predicted value")
plt.show()
print("######################################")

Event Timeline