Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kuhns{III-VII} metrics Added #572

Merged
merged 7 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- 5 new distance/similarity
1. KuhnsIII
2. KuhnsIV
3. KuhnsV
4. KuhnsVI
5. KuhnsVII
### Changed
- PyPI badge in `README.md`
- GitHub actions are limited to the `dev` and `master` branches
Expand Down
289 changes: 287 additions & 2 deletions Document/Distance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,291 @@
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kuhns III"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kuhns III correlation [[40]](#ref40)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$corr_{KuhnsIII} =\n",
"\\frac{\\delta(TP + FP, TP + FN)}\n",
"{(1-\\frac{TP}{2 \\times TP + FP + FN})(2 \\times TP + FP + FN-\\frac{(TP + FP)(TP + FN)}{n})}\n",
"$$\n",
"\n",
"$$\n",
"\\delta(TP + FP, TP + FN) = TP - \\frac{(TP + FP) \\times (TP + FN)}{N}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0.4148148148148148, 1: 0.1388888888888889, 2: 0.08088235294117647}"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cm.distance(metric=DistanceType.KuhnsIII)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<ul>\n",
" <li><span style=\"color:red;\">Notice </span> : new in <span style=\"color:red;\">version 4.2</span> </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kuhns IV"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kuhns IV correlation [[40]](#ref40)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$corr_{KuhnsIV} =\n",
"\\frac{\\delta(TP + FP, TP + FN)}\n",
"{\\min(TP + FP, TP + FN)}\n",
"$$\n",
"\n",
"$$\n",
"\\delta(TP + FP, TP + FN) = TP - \\frac{(TP + FP) \\times (TP + FN)}{N}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0.5833333333333334, 1: 0.25, 2: 0.1}"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cm.distance(metric=DistanceType.KuhnsIV)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<ul>\n",
" <li><span style=\"color:red;\">Notice </span> : new in <span style=\"color:red;\">version 4.2</span> </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kuhns V"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kuhns V correlation [[40]](#ref40)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$corr_{KuhnsV} =\n",
"\\frac{\\delta(TP + FP, TP + FN)}\n",
"{\\max((TP+FP)(1-\\frac{TP+FP}{n}), (TP+FN)(1-\\frac{TP+FN}{n}))}\n",
"$$\n",
"\n",
"$$\n",
"\\delta(TP + FP, TP + FN) = TP - \\frac{(TP + FP) \\times (TP + FN)}{N}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0.6000000000000001, 1: 0.2222222222222222, 2: 0.16666666666666666}"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cm.distance(metric=DistanceType.KuhnsV)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<ul>\n",
" <li><span style=\"color:red;\">Notice </span> : new in <span style=\"color:red;\">version 4.2</span> </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kuhns VI"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kuhns VI correlation [[40]](#ref40)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$corr_{KuhnsVI} =\n",
"\\frac{\\delta(TP + FP, TP + FN)}\n",
"{\\min((TP+FP)(1-\\frac{TP+FP}{n}), (TP+FN)(1-\\frac{TP+FN}{n}))}\n",
"$$\n",
"\n",
"$$\n",
"\\delta(TP + FP, TP + FN) = TP - \\frac{(TP + FP) \\times (TP + FN)}{N}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0.7777777777777778, 1: 0.3, 2: 0.17142857142857146}"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cm.distance(metric=DistanceType.KuhnsVI)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<ul>\n",
" <li><span style=\"color:red;\">Notice </span> : new in <span style=\"color:red;\">version 4.2</span> </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kuhns VII"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kuhns VII correlation [[40]](#ref40)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$corr_{KuhnsVII} =\n",
"\\frac{\\delta(TP + FP, TP + FN)}\n",
"{\\sqrt{(TP + FP) \\times (TP + FN)}}\n",
"$$\n",
"\n",
"$$\n",
"\\delta(TP + FP, TP + FN) = TP - \\frac{(TP + FP) \\times (TP + FN)}{N}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0.45184805705753195, 1: 0.20412414523193154, 2: 0.09128709291752768}"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cm.distance(metric=DistanceType.KuhnsVII)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<ul>\n",
" <li><span style=\"color:red;\">Notice </span> : new in <span style=\"color:red;\">version 4.2</span> </li>\n",
"</ul>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -3140,7 +3425,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -3154,7 +3439,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.10.12"
},
"toc": {
"base_numbering": 1,
Expand Down
10 changes: 10 additions & 0 deletions Test/verified_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsI)[1], 0.005004425239483548, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm1.distance(metric=DistanceType.KuhnsII)[1], 0.49489795918367346, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsII)[1], 0.32695578231292516, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm1.distance(metric=DistanceType.KuhnsIII)[1], 0.3307757885763001, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsIII)[1], 0.21873141468207793, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm1.distance(metric=DistanceType.KuhnsIV)[1], 0.49489795918367346, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsIV)[1], 0.3923469387755102, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm1.distance(metric=DistanceType.KuhnsV)[1], 0.497435897435897, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsV)[1], 0.329477292202228, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm1.distance(metric=DistanceType.KuhnsVI)[1], 0.497435897435897, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsVI)[1], 0.394865211810013, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm1.distance(metric=DistanceType.KuhnsVII)[1], 0.49489795918367346, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> assert isclose(cm2.distance(metric=DistanceType.KuhnsVII)[1], 0.3581621145590755, abs_tol=ABS_TOL, rel_tol=REL_TOL)
>>> mlcm = MultiLabelCM(actual_vector=[{"cat", "bird"}, {"dog"}], predict_vector=[{"cat"}, {"dog", "bird"}], classes=["cat", "dog", "bird"]) # Verified Case -- (http://bitly.ws/GNq2)
>>> mlcm.actual_vector_multihot
[[1, 0, 1], [0, 1, 0]]
Expand Down
Loading
Loading