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

Created new per class accuracy function. #624

Merged
merged 23 commits into from
Nov 24, 2019
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
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ nav:
- user_guide/data/three_blobs_data.md
- user_guide/data/wine_data.md
- evaluate:
- user_guide/evaluate/accuracy_score.md
- user_guide/evaluate/bias_variance_decomp.md
- user_guide/evaluate/bootstrap.md
- user_guide/evaluate/bootstrap_point632_score.md
Expand Down
1 change: 1 addition & 0 deletions docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The CHANGELOG for the current development version is available at
##### New Features

- The `SequentialFeatureSelector` now supports using pre-specified feature sets via the `fixed_features` parameter. ([#578](https://github.com/rasbt/mlxtend/pull/578))
- Adds a new `accuracy_score` function to `mlxtend.evaluate` for computing basic classifcation accuracy, per-class accuracy, and average per-class accuracy. ([#624](https://github.com/rasbt/mlxtend/pull/624) via [Deepan Das](https://github.com/deepandas11))

##### Changes

Expand Down
1 change: 1 addition & 0 deletions docs/sources/USER_GUIDE_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [wine_data](user_guide/data/wine_data.md)

## `evaluate`
- [accuracy_score](user_guide/evaluate/accuracy_score.md)
- [bias_variance_decomp](user_guide/evaluate/bias_variance_decomp.md)
- [bootstrap](user_guide/evaluate/bootstrap.md)
- [bootstrap_point632_score](user_guide/evaluate/bootstrap_point632_score.md)
Expand Down
327 changes: 327 additions & 0 deletions docs/sources/user_guide/evaluate/accuracy_score.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Accuracy Score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A function for computing for computing basic classifcation accuracy, per-class accuracy, and average per-class accuracy."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> from mlxtend.evaluate import accuracy_score"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 1 -- Standard Accuracy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The \"overall\" accuracy is defined as the number of correct predictions (*true positives* TP and *true negatives* TN) over all samples *n*:\n",
"\n",
"$$ACC = \\frac{TP + TN}{n}$$\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5555555555555556"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"from mlxtend.evaluate import accuracy_score\n",
"\n",
"\n",
"y_targ = [0, 0, 0, 1, 1, 1, 2, 2, 2]\n",
"y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2]\n",
"\n",
"accuracy_score(y_targ, y_pred)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2 -- Per-Class Accuracy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The per-class accuracy is the accuracy of one class (defined as the `pos_label`) versus all remaining datapoints in the dataset."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Standard accuracy: 55.56%\n",
"Class 1 accuracy: 66.67%\n"
]
}
],
"source": [
"import numpy as np\n",
"from mlxtend.evaluate import accuracy_score\n",
"\n",
"\n",
"y_targ = [0, 0, 0, 1, 1, 1, 2, 2, 2]\n",
"y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2]\n",
"\n",
"std_acc = accuracy_score(y_targ, y_pred)\n",
"bin_acc = accuracy_score(y_targ, y_pred, method='binary', pos_label=1)\n",
"\n",
"print(f'Standard accuracy: {std_acc*100:.2f}%')\n",
"print(f'Class 1 accuracy: {bin_acc*100:.2f}%')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 3 -- Average Per-Class Accuracy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Overview"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The \"overall\" accuracy is defined as the number of correct predictions (*true positives* TP and *true negatives* TN) over all samples *n*:\n",
"\n",
"$$ACC = \\frac{TP + TN}{n}$$\n",
"\n",
"in a binary class setting:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](./scoring_files/conf_mat_binary-1.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a multi-class setting, we can generalize the computation of the accuracy as the fraction of all true predictions (the diagonal) over all samples n.\n",
"\n",
"$$ACC = \\frac{T}{n}$$\n",
"\n",
"Considering a multi-class problem with 3 classes (C0, C1, C2)\n",
"\n",
"![](./scoring_files/conf_mat_multi-1.png)\n",
"\n",
"let's assume our model made the following predictions:\n",
"\n",
"![](./scoring_files/multi-ex-1.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We compute the accuracy as:\n",
" \n",
"$$ACC = \\frac{3 + 50 + 18}{90} \\approx 0.79$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, in order to compute the ***average per-class accuracy***, we compute the binary accuracy for each class label separately; i.e., if class 1 is the positive class, class 0 and 2 are both considered the negative class.\n",
"\n",
"$$APC\\;ACC = \\frac{83/90 + 71/90 + 78/90}{3} \\approx 0.86$$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Standard accuracy: 55.56%\n",
"Class 1 accuracy: 66.67%\n",
"Average per-class accuracy: 70.37%\n"
]
}
],
"source": [
"import numpy as np\n",
"from mlxtend.evaluate import accuracy_score\n",
"\n",
"\n",
"y_targ = [0, 0, 0, 1, 1, 1, 2, 2, 2]\n",
"y_pred = [1, 0, 0, 0, 1, 2, 0, 2, 2]\n",
"\n",
"std_acc = accuracy_score(y_targ, y_pred)\n",
"bin_acc = accuracy_score(y_targ, y_pred, method='binary', pos_label=1)\n",
"avg_acc = accuracy_score(y_targ, y_pred, method='average')\n",
"\n",
"print(f'Standard accuracy: {std_acc*100:.2f}%')\n",
"print(f'Class 1 accuracy: {bin_acc*100:.2f}%')\n",
"print(f'Average per-class accuracy: {avg_acc*100:.2f}%')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### References\n",
"\n",
"- [1] S. Raschka. [An overview of general performance metrics of binary classifier systems](http://arxiv.org/abs/1410.5330). Computing Research Repository (CoRR), abs/1410.5330, 2014.\n",
"- [2] Cyril Goutte and Eric Gaussier. [A probabilistic interpretation of precision, recall and f-score, with implication for evaluation](http://link.springer.com/chapter/10.1007/978-3-540-31865-1_25). In Advances in Information Retrieval, pages 345–359. Springer, 2005.\n",
"- [3] Brian W Matthews. [Comparison of the predicted and observed secondary structure of T4 phage lysozyme](http://www.sciencedirect.com/science/article/pii/0005279575901099). Biochimica et Biophysica Acta (BBA)- Protein Structure, 405(2):442–451, 1975."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"## accuracy_score\n",
"\n",
"*accuracy_score(y_target, y_predicted, method='standard', pos_label=1, normalize=True)*\n",
"\n",
"General accuracy function for supervised learning.\n",
"**Parameters**\n",
"\n",
"- `y_target` : array-like, shape=[n_values]\n",
"\n",
" True class labels or target values.\n",
"\n",
"- `y_predicted` : array-like, shape=[n_values]\n",
"\n",
" Predicted class labels or target values.\n",
"\n",
"- `method` : str, 'standard' by default.\n",
"\n",
" The chosen method for accuracy computation.\n",
" If set to 'standard', computes overall accuracy.\n",
" If set to 'binary', computes accuracy for class pos_label.\n",
" If set to 'average', computes average per class accuracy.\n",
"\n",
"- `pos_label` : str or int, 1 by default.\n",
"\n",
" The class whose accuracy score is to be reported.\n",
" Used only when `method` is set to 'binary'\n",
"\n",
"- `normalize` : bool, True by default.\n",
"\n",
" If True, returns fraction of correctly classified samples.\n",
" If False, returns number of correctly classified samples.\n",
"\n",
"**Returns**\n",
"\n",
"score: float\n",
"\n",
"\n"
]
}
],
"source": [
"with open('../../api_modules/mlxtend.evaluate/accuracy_score.md', 'r') as f:\n",
" print(f.read())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
4 changes: 3 additions & 1 deletion mlxtend/evaluate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .f_test import combined_ftest_5x2cv
from .proportion_difference import proportion_difference
from .bias_variance_decomp import bias_variance_decomp
from .accuracy import accuracy_score

__all__ = ["scoring", "confusion_matrix",
"mcnemar_table", "mcnemar_tables",
Expand All @@ -37,4 +38,5 @@
"feature_importance_permutation",
"RandomHoldoutSplit", "PredefinedHoldoutSplit",
"ftest", "combined_ftest_5x2cv",
"proportion_difference", "bias_variance_decomp"]
"proportion_difference", "bias_variance_decomp",
"accuracy_score"]
Loading