from packages import * class metrics: def __init__(self, c:Optional[float] = None, cv:Optional[List] = None, t:Optional[List] = None, method = 'regression')-> DataFrame: phase = [c, cv, t] index = np.array(["train", "cv", "test"]) notnone = [i for i in range(3) if phase[i] != None] met_index = index[notnone] methods = ['regression', 'classification'] perf = {} for i in notnone: if method == 'regression': perf[index[i]] = metrics.reg_(phase[i][0], phase[i][1]) elif method == 'classification': perf[index[i]] = metrics.class_(phase[i][0], phase[i][1]) if notnone == 1: self.ret = perf.T else: self.ret = DataFrame(perf).T @staticmethod def reg_(meas, pred): meas = np.array(meas) pred = np.array(pred) xbar = np.mean(meas) # the average of measured values e = np.subtract(meas , pred) e2 = e**2# the squared error # Sum of squared: # TOTAL sst = np.sum((meas - xbar)**2) # RESIDUAL ssr = np.sum(e2) # REGRESSION OR MODEL ssm = np.sum(pred - xbar) # Compute statistical metrics metr = {} metr['r'] = np.corrcoef(meas, pred)[0, 1] metr['r2'] = 1-ssr/sst metr['rmse'] = np.sqrt(np.mean(e2)) metr['mae'] = np.mean(np.abs(e2)) metr['rpd'] = np.std(meas)/np.sqrt(np.mean(e2)) metr['rpiq'] = (np.quantile(meas, .75) - np.quantile(meas, .25))/np.sqrt(np.mean(e2)) return metr @staticmethod def class_(meas, pred): pass @property def scores_(self): return self.ret