Skip to content
Snippets Groups Projects
Commit 8954c365 authored by DIANE's avatar DIANE
Browse files

metrics update

parent fa5863ce
No related branches found
No related tags found
No related merge requests found
from Packages import * from Packages import *
def metrics(train, cv=None, test = None): class metrics:
def __init__(self, meas, pred):
C = pd.DataFrame()
CV = pd.DataFrame() if isinstance(meas, pd.DataFrame):
T = pd.DataFrame() self.meas = meas.to_numpy()
else :
if train is not None and cv is not None and test is not None: self.meas = meas.ravel()
C["r"] = [np.corrcoef(train[0], train[1])[0, 1]] self.pred = pred.to_numpy().ravel()
C["r2"] = [r2_score(train[0], train[1])]
C["rmse"] = [np.sqrt(mean_squared_error(train[0], train[1]))] @property
C["mae"] = [mean_absolute_error(train[0], train[1])] def evaluate_(self):
C.index = ['perf'] xbar = np.mean(self.meas) # the average of measured values
METRICS = C e2 = (self.meas - self.pred)**2 # the squared error
print(xbar)
CV["r"] = [np.corrcoef(cv[0], cv[1])[0, 1]] # Sum of squared:
CV["r2"] = [r2_score(cv[0], cv[1])] # TOTAL
CV["rmse"] = [np.sqrt(mean_squared_error(cv[0], cv[1]))] sst = np.sum((self.meas-xbar)**2)
CV["mae"] = [mean_absolute_error(cv[0], cv[1])] # RESIDUAL
CV.index = ['perf'] ssr = np.sum(e2)
# REGRESSION OR MODEL
T["r"] = [np.corrcoef(test[0], test[1])[0, 1]] ssm = np.sum(self.pred - xbar)
T["r2"] = [r2_score(test[0], test[1])]
T["rmse"] = [np.sqrt(mean_squared_error(test[0], test[1]))]
T["mae"] = [mean_absolute_error(test[0], test[1])]
T.index = ['perf'] # Compute statistical metrics
metr = pd.DataFrame()
METRICS = pd.concat([C, CV, T], axis=1) metr['r'] = [np.corrcoef(self.meas.ravel(), self.pred)[0,1]]
metr['r2'] = [1-ssr/sst]
elif train is not None and cv is not None and test is None: metr['rmse'] = [np.sqrt(np.mean(e2))]
metr['mae'] = [np.mean(np.abs(e2))]
C["r"] = [np.corrcoef(train[0], train[1])[0, 1]] metr['rpd'] = [np.std(self.meas)/np.sqrt(np.mean(e2))]
C["r2"] = [r2_score(train[0], train[1])] metr['rpiq'] = [(np.quantile(self.meas,.75)-np.quantile(self.meas,.25))/np.sqrt(np.mean(e2))]
C["rmse"] = [np.sqrt(mean_squared_error(train[0], train[1]))] return metr
C["mae"] = [mean_absolute_error(train[0], train[1])] \ No newline at end of file
C.index = ['perf']
CV["r"] = [np.corrcoef(cv[0], cv[1])[0, 1]]
CV["r2"] = [r2_score(cv[0], cv[1])]
CV["rmse"] = [np.sqrt(mean_squared_error(cv[0], cv[1]))]
CV["mae"] = [mean_absolute_error(cv[0], cv[1])]
CV.index = ['perf']
METRICS = pd.concat([C, CV], axis=1)
elif train is not None and cv is None and test is not None:
C["r"] = [np.corrcoef(train[0], train[1])[0, 1]]
C["r2"] = [r2_score(train[0], train[1])]
C["rmse"] = [np.sqrt(mean_squared_error(train[0], train[1]))]
C["mae"] = [mean_absolute_error(train[0], train[1])]
C.index = ['perf']
T["r"] = [np.corrcoef(test[0], test[1])[0, 1]]
T["r2"] = [r2_score(test[0], test[1])]
T["rmse"] = [np.sqrt(mean_squared_error(test[0], test[1]))]
T["mae"] = [mean_absolute_error(test[0], test[1])]
T.index = ['perf']
METRICS = pd.concat([C, T], axis=1)
elif train is None and cv is not None and test is not None:
CV["r"] = [np.corrcoef(cv[0], cv[1])[0, 1]]
CV["r2"] = [r2_score(cv[0], cv[1])]
CV["rmse"] = [np.sqrt(mean_squared_error(cv[0], cv[1]))]
CV["mae"] = [mean_absolute_error(cv[0], cv[1])]
CV.index = ['perf']
T["r"] = [np.corrcoef(test[0], test[1])[0, 1]]
T["r2"] = [r2_score(test[0], test[1])]
T["rmse"] = [np.sqrt(mean_squared_error(test[0], test[1]))]
T["mae"] = [mean_absolute_error(test[0], test[1])]
T.index = ['perf']
METRICS = pd.concat([CV, T], axis=1)
elif train is not None and cv is None and test is None:
C["r"] = [np.corrcoef(train[0], train[1])[0, 1]]
C["r2"] = [r2_score(train[0], train[1])]
C["rmse"] = [np.sqrt(mean_squared_error(train[0], train[1]))]
C["mae"] = [mean_absolute_error(train[0], train[1])]
C.index = ['perf']
METRICS = C
if train is None and cv is not None and test is None:
CV["r"] = [np.corrcoef(cv[0], cv[1])[0, 1]]
CV["r2"] = [r2_score(cv[0], cv[1])]
CV["rmse"] = [np.sqrt(mean_squared_error(cv[0], cv[1]))]
CV["mae"] = [mean_absolute_error(cv[0], cv[1])]
CV.index = ['perf']
METRICS = CV
if train is None and cv is None and test is not None:
T["r"] = [np.corrcoef(test[0], test[1])[0, 1]]
T["r2"] = [r2_score(test[0], test[1])]
T["rmse"] = [np.sqrt(mean_squared_error(test[0], test[1]))]
T["mae"] = [mean_absolute_error(test[0], test[1])]
T.index = ['perf']
METRICS = T
return METRICS
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment