You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to be able to use cuML to calculate the precision score of an estimator in Python. I didn't see a C++/CUDA implementation in the metrics/ header files, so this may require both C++/CUDA and Python/Cython bindings eventually. Didn't see an existing issue but will close if I find one.
Currently, this requires going to the CPU if we want to use an existing API (relying on NEP-18 for dispatch doesn't work here). With about three million rows, sklearn's precision_score can take about 1.5 seconds. A stopgap GPU implementation for binary precision using CuPy can be 500x+ faster.
@cjnolet This is the n-class version of the loose code above. It doesn't handle all of what sklearn does, of course. But it gets the core averages (binary, none, and macro). 100-300x speedup on low millions of rows.
Someone could perhaps run with this for an initial CuPy version as a good first issue.
importcupyascpimportnumpyasnpdefcupy_precision_score(y, y_pred, average='binary'):
""" TODO: Handle the following - average=micro (slightly more annoying) - average=weighted (slightly more annoying) """nclasses=len(cp.unique(y))
ifaverage=='binary'andnclasses>2:
raiseValueErrorifnclasses<2:
raiseValueError("Single class precision is not yet supported")
res=cp.zeros(nclasses)
foriinrange(nclasses):
pos_pred_ix=cp.where(y_pred==i)[0]
# short circuitiflen(pos_pred_ix) ==0:
res[i] =0breaktp_sum= (y_pred[pos_pred_ix] ==y[pos_pred_ix]).sum()
fp_sum= (y_pred[pos_pred_ix] !=y[pos_pred_ix]).sum()
res[i] = (tp_sum/ (tp_sum+fp_sum)).item()
ifnotaverage:
returnres.get()
elifaverage=='binary':
returnres[nclasses-1].item()
elifaverage=='macro':
returnres.mean().item()
returnres.get()
Hey! I'm a first-time contributor and would love to pick this up using the approach suggested by @beckernick. Is the issue still open for contribution?
Hi, I'd like to pick this up if it is still free. Linked PR #3184 has been dormant since late 2021 and the last activity here was months ago, so checking first that nobody is actively working on it.
Pure-CuPy precision_score in cuml.metrics, implemented next to accuracy_score using the current input-validation helpers
sklearn-compatible signature: average in {None, 'binary', 'macro', 'micro', 'weighted'} plus labels, pos_label and zero_division, computed from the GPU confusion matrix rather than a per-class loop
Tests against sklearn across numpy/cupy/cudf/pandas inputs, following the conventions in tests/test_metrics.py
Activity
dantegd commentedon Dec 30, 2019
@beckernick starting with a CuPy implementation in cuML is totally fine. Particularly if submitted by the awesome person that is requesting it ;)
beckernick commentedon Feb 12, 2020
@cjnolet This is the n-class version of the loose code above. It doesn't handle all of what sklearn does, of course. But it gets the core averages (binary, none, and macro). 100-300x speedup on low millions of rows.
Someone could perhaps run with this for an initial CuPy version as a good first issue.
beckernick commentedon Feb 12, 2020
Related to #242 . May want to separate the scorers into individual issues and keep using #242 as a tracker.
ptartan21 commentedon Nov 10, 2020
Hi, I'd like to pick this issue up if there's no one else working on it (I'm currently an intern working at NVIDIA).
beckernick commentedon Nov 11, 2020
cc @JohnZed
Nzouh commentedon Mar 29, 2026
Hey! I'm a first-time contributor and would love to pick this up using the approach suggested by @beckernick. Is the issue still open for contribution?
VaggelisGian commentedon Aug 26, 2026
Hi, I'd like to pick this up if it is still free. Linked PR #3184 has been dormant since late 2021 and the last activity here was months ago, so checking first that nobody is actively working on it.
Plan, following @dantegd's CuPy guidance above and @beckernick's sketch:
precision_scoreincuml.metrics, implemented next toaccuracy_scoreusing the current input-validation helpersaveragein {None, 'binary', 'macro', 'micro', 'weighted'} pluslabels,pos_labelandzero_division, computed from the GPU confusion matrix rather than a per-class loopIf @ptartan21 or @Nzouh are still on this, happy to stand down.
VaggelisGian commentedon Aug 26, 2026
Opened PR #8524 implementing this with the CuPy approach described above.