Skip to content

Commit

Permalink
Adding docstring to app
Browse files Browse the repository at this point in the history
  • Loading branch information
FazilaRubab committed Dec 21, 2023
1 parent d26e919 commit 8c4ca0d
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 39 deletions.
15 changes: 15 additions & 0 deletions SeqMetrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
from ._rgr import centered_rms_dev
from ._rgr import mapd
from ._rgr import sga
from ._rgr import mse
from ._rgr import RegressionMetrics

from ._cls import f1_score
Expand All @@ -125,6 +126,20 @@
from ._cls import balanced_accuracy
from ._cls import confusion_matrix
from ._cls import cross_entropy
from ._cls import error_rate
from ._cls import false_positive_rate
from ._cls import false_negative_rate
from ._cls import false_discovery_rate
from ._cls import false_omission_rate
from ._cls import f2_score
from ._cls import fowlkes_mallows_index
from ._cls import mathews_corr_coeff
from ._cls import negative_likelihood_ratio
from ._cls import negative_predictive_value
from ._cls import positive_likelihood_ratio
from ._cls import prevalence_threshold
from ._cls import specificity
from ._cls import youden_index
from ._cls import ClassificationMetrics


Expand Down
113 changes: 110 additions & 3 deletions SeqMetrics/_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def false_positive_rate(self):
return false_positive_rate(true= self.true, predicted= self.predicted)

def false_discovery_rate(self):
return false_positive_rate(true= self.true, predicted= self.predicted)
return false_discovery_rate(true= self.true, predicted= self.predicted)

def false_negative_rate(self):
return false_negative_rate(true= self.true, predicted= self.predicted)
Expand Down Expand Up @@ -364,6 +364,14 @@ def cross_entropy(true, predicted, epsilon=1e-12) -> float:
-------
scalar
Parameters
----------
true :
ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted :
simulated values
"""
cls= ClassificationMetrics(true, predicted)
if cls.is_categorical:
Expand Down Expand Up @@ -568,6 +576,12 @@ def specificity(true, predicted, average=None):
It's formula is following
TN / TN+FP
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
Examples
--------
>>> import numpy as np
Expand Down Expand Up @@ -602,6 +616,12 @@ def balanced_accuracy(true, predicted, average=None)->float:
"""
balanced accuracy.
It performs better on imbalanced datasets.
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
TP = cls._tp()
Expand Down Expand Up @@ -646,6 +666,14 @@ def f1_score(true, predicted, average=None)->Union[np.ndarray, float]:
def f2_score(true, predicted, average=None):
"""
f2 score
Parameters
----------
average : str (default=None)
one of None, ``macro``, ``weighted``, or ``micro``
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
return cls._f_score(average, 2.0)
Expand All @@ -657,6 +685,12 @@ def false_positive_rate(true, predicted):
TP / (TP + TN)
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
TP = cls._tp()
Expand All @@ -669,6 +703,12 @@ def false_discovery_rate(true, predicted):
"""
False discovery rate
FP / (TP + FP)
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
FP = cls._fp()
Expand All @@ -683,6 +723,12 @@ def false_negative_rate(true, predicted):
False Negative Rate or miss rate.
FN / (FN + TP)
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
FN = cls._fn()
Expand All @@ -695,6 +741,12 @@ def negative_predictive_value(true, predicted):
"""
Negative Predictive Value
TN/(TN+FN)
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
TN = cls._tn()
Expand All @@ -707,6 +759,12 @@ def error_rate(true, predicted):
"""
Error rate is the number of all incorrect predictions divided by the total
number of samples in data.
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)

Expand All @@ -716,6 +774,12 @@ def mathews_corr_coeff(true, predicted):
"""
Methew's correlation coefficient
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
TP, TN, FP, FN = cls._tp(), cls._tn(), cls._fp(), cls._fn()
Expand All @@ -730,6 +794,13 @@ def positive_likelihood_ratio(true, predicted, average=None):
Positive likelihood ratio
sensitivity / 1-specificity
Parameters
----------
average : str (default=None)
one of None, ``macro``, ``weighted``, or ``micro``
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
return cls.recall(average=average) / (1 - cls.specificity(average=average))
Expand All @@ -741,6 +812,14 @@ def negative_likelihood_ratio(true, predicted, average=None):
1 - sensitivity / specificity
https://en.wikipedia.org/wiki/Likelihood_ratios_in_diagnostic_testing#positive_likelihood_ratio
Parameters
----------
average : str (default=None)
one of None, ``macro``, ``weighted``, or ``micro``
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)

Expand All @@ -753,6 +832,14 @@ def youden_index(true, predicted, average=None):
j = TPR + TNR − 1 = sensitivity + specificity - 1
https://en.wikipedia.org/wiki/Youden%27s_J_statistic
Parameters
----------
average : str (default=None)
one of None, ``macro``, ``weighted``, or ``micro``
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
return cls.recall(average) + cls.specificity(average) - 1
Expand All @@ -767,6 +854,14 @@ def fowlkes_mallows_index(true, predicted, average=None):
TPR is true positive rate or recall or sensitivity
https://en.wikipedia.org/wiki/Fowlkes%E2%80%93Mallows_index
Parameters
----------
average : str (default=None)
one of None, ``macro``, ``weighted``, or ``micro``
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
return np.sqrt(cls.precision(average) * cls.recall(average))
Expand All @@ -778,6 +873,14 @@ def prevalence_threshold(true, predicted, average=None):
sqrt(FPR) / (sqrt(TPR) + sqrt(FPR))
TPR is true positive rate or recall
Parameters
----------
average : str (default=None)
one of None, ``macro``, ``weighted``, or ``micro``
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
FPR = cls.false_positive_rate()
Expand All @@ -789,6 +892,12 @@ def false_omission_rate(true, predicted):
False omission rate
FN / (FN + TN)
Parameters
----------
true : ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted : simulated values
"""
cls= ClassificationMetrics(true, predicted)
FN = cls._fn()
Expand All @@ -800,5 +909,3 @@ def false_omission_rate(true, predicted):





27 changes: 24 additions & 3 deletions SeqMetrics/_rgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ def post_process_kge(cc, alpha, beta, return_all=False):

def r2(true, predicted, preprocess:bool = True) -> float:
"""
R2 is a statistical measure of how well the regression line approximates the actual data.
Quantifies the percent of variation in the response that the 'model'
explains_. The 'model' here is anything from which we obtained predicted
array. It is also called coefficient of determination or square of pearson
Expand Down Expand Up @@ -448,6 +449,7 @@ def r2(true, predicted, preprocess:bool = True) -> float:
def nse(true, predicted, preprocess:bool = True) -> float:
"""Nash-Sutcliff Efficiency.
The Nash-Sutcliffe efficiency (NSE) is a normalized statistic that determines the relative magnitude of the residual variance compared to the measured data variance
It determine how well the model simulates trends for the output response of concern. But cannot help identify
model bias and cannot be used to identify differences in timing and magnitude of peak flows and shape of
recession curves; in other words, it cannot be used for single-event simulations. It is sensitive to extreme
Expand Down Expand Up @@ -3704,7 +3706,17 @@ def std_ratio(true, predicted, preprocess:bool = True, **kwargs) -> float:
true, predicted = maybe_preprocess(preprocess, true, predicted, METRIC_TYPES['std_ratio'])
return float(np.std(predicted, **kwargs) / np.std(true, **kwargs))
def umbrae(true, predicted, preprocess:bool = True, benchmark: np.ndarray = None):
""" Unscaled Mean Bounded Relative Absolute Error
""" Unscaled Mean Bounded Relative Absolute Error
Parameters
----------
true :
ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted :
simulated values
preprocess :
process the true and predicted array
Examples
---------
>>> import numpy as np
Expand Down Expand Up @@ -3919,6 +3931,17 @@ def norm_ape(true, predicted, preprocess:bool = True) -> float:

def mse(true, predicted, preprocess=False, weights=None):
"""
Mean Square Error
Parameters
----------
true :
ture/observed/actual/target values. It must be a numpy array,
or pandas series/DataFrame or a list.
predicted :
simulated values
preprocess :
process the true and predicted array
Examples
---------
>>> import numpy as np
Expand All @@ -3929,5 +3952,3 @@ def mse(true, predicted, preprocess=False, weights=None):
"""
true, predicted = maybe_preprocess(preprocess, true, predicted, METRIC_TYPES['mse'])
return float(np.average((true - predicted) ** 2, axis=0, weights=weights))


Loading

0 comments on commit 8c4ca0d

Please sign in to comment.