-
Notifications
You must be signed in to change notification settings - Fork 15
feat(metrics): add a classification metrics module #176
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import ibis.expr.datatypes as dt | ||
|
||
|
||
def accuracy_score(y_true: dt.Integer, y_pred: dt.Integer) -> float: | ||
"""Calculate the accuracy score of predicted values against true values. | ||
|
||
Parameters | ||
---------- | ||
y_true | ||
Table expression column containing the true labels. | ||
y_pred | ||
Table expression column containing the predicted labels. | ||
|
||
Returns | ||
------- | ||
float | ||
The accuracy score, representing the fraction of correct predictions. | ||
|
||
Examples | ||
-------- | ||
>>> import ibis | ||
>>> from ibis_ml.metrics import accuracy_score | ||
>>> ibis.options.interactive = True | ||
>>> t = ibis.memtable( | ||
... { | ||
... "id": range(1, 13), | ||
... "actual": [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], | ||
... "prediction": [1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1], | ||
... } | ||
... ) | ||
>>> accuracy_score(t.actual, t.prediction) | ||
┌──────────┐ | ||
│ 0.583333 │ | ||
└──────────┘ | ||
""" | ||
return (y_true == y_pred).mean() # .to_pyarrow().as_py() | ||
|
||
|
||
def precision_score(y_true: dt.Integer, y_pred: dt.Integer) -> float: | ||
"""Calculate the precision score of predicted values against true values. | ||
|
||
Parameters | ||
IndexSeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
---------- | ||
y_true | ||
Table expression column containing the true labels. | ||
y_pred | ||
Table expression column containing the predicted labels. | ||
|
||
Returns | ||
------- | ||
float | ||
The precision score, representing the fraction of true positive predictions. | ||
|
||
Examples | ||
IndexSeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-------- | ||
>>> import ibis | ||
>>> from ibis_ml.metrics import precision_score | ||
>>> ibis.options.interactive = True | ||
>>> t = ibis.memtable( | ||
... { | ||
... "id": range(1, 13), | ||
... "actual": [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], | ||
... "prediction": [1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1], | ||
... } | ||
... ) | ||
>>> precision_score(t.actual, t.prediction) | ||
┌──────────┐ | ||
│ 0.666667 │ | ||
└──────────┘ | ||
""" | ||
true_positive = (y_true & y_pred).sum() | ||
predicted_positive = y_pred.sum() | ||
return true_positive / predicted_positive | ||
|
||
|
||
def recall_score(y_true: dt.Integer, y_pred: dt.Integer) -> float: | ||
"""Calculate the recall score of predicted values against true values. | ||
|
||
Parameters | ||
IndexSeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
---------- | ||
y_true | ||
Table expression column containing the true labels. | ||
y_pred | ||
Table expression column containing the predicted labels. | ||
|
||
Returns | ||
------- | ||
float | ||
The recall score, representing the fraction of true positive predictions. | ||
|
||
Examples | ||
-------- | ||
>>> import ibis | ||
>>> from ibis_ml.metrics import recall_score | ||
>>> ibis.options.interactive = True | ||
>>> t = ibis.memtable( | ||
... { | ||
... "id": range(1, 13), | ||
... "actual": [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], | ||
... "prediction": [1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1], | ||
... } | ||
... ) | ||
>>> recall_score(t.actual, t.prediction) | ||
┌──────────┐ | ||
│ 0.571429 │ | ||
└──────────┘ | ||
""" | ||
true_positive = (y_true & y_pred).sum() | ||
actual_positive = y_true.sum() | ||
return true_positive / actual_positive | ||
|
||
|
||
def f1_score(y_true: dt.Integer, y_pred: dt.Integer) -> float: | ||
"""Calculate the F1 score of predicted values against true values. | ||
|
||
Parameters | ||
IndexSeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
---------- | ||
y_true | ||
Table expression column containing the true labels. | ||
y_pred | ||
Table expression column containing the predicted labels. | ||
|
||
Returns | ||
------- | ||
float | ||
The F1 score, representing the harmonic mean of precision and recall. | ||
|
||
Examples | ||
-------- | ||
>>> import ibis | ||
>>> from ibis_ml.metrics import f1_score | ||
>>> ibis.options.interactive = True | ||
>>> t = ibis.memtable( | ||
... { | ||
... "id": range(1, 13), | ||
... "actual": [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], | ||
... "prediction": [1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1], | ||
... } | ||
... ) | ||
>>> f1_score(t.actual, t.prediction) | ||
┌──────────┐ | ||
│ 0.615385 │ | ||
└──────────┘ | ||
""" | ||
precision = precision_score(y_true, y_pred) | ||
recall = recall_score(y_true, y_pred) | ||
return (2 * precision * recall) / (precision + recall) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import ibis | ||
import pytest | ||
import sklearn.metrics | ||
|
||
import ibis_ml.metrics | ||
|
||
|
||
@pytest.fixture | ||
def results_table(): | ||
return ibis.memtable( | ||
{ | ||
"id": range(1, 13), | ||
"actual": [1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], | ||
"prediction": [1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1], | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"metric_name", | ||
[ | ||
pytest.param("accuracy_score", id="accuracy_score"), | ||
pytest.param("precision_score", id="precision_score"), | ||
pytest.param("recall_score", id="recall_score"), | ||
pytest.param("f1_score", id="f1_score"), | ||
], | ||
) | ||
def test_classification_metrics(results_table, metric_name): | ||
ibis_ml_func = getattr(ibis_ml.metrics, metric_name) | ||
sklearn_func = getattr(sklearn.metrics, metric_name) | ||
t = results_table | ||
df = t.to_pandas() | ||
result = ibis_ml_func(t.actual, t.prediction).to_pyarrow().as_py() | ||
expected = sklearn_func(df["actual"], df["prediction"]) | ||
assert result == pytest.approx(expected, abs=1e-4) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.