/
accuracy.pyx
83 lines (65 loc) · 2.5 KB
/
accuracy.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# Copyright (c) 2019, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# distutils: language = c++
import numpy as np
from libc.stdint cimport uintptr_t
import cudf
import cuml.internals
from cuml.common.input_utils import input_to_cuml_array
from cuml.raft.common.handle cimport handle_t
from cuml.raft.common.handle import Handle
cimport cuml.common.cuda
cdef extern from "cuml/metrics/metrics.hpp" namespace "ML::Metrics":
float accuracy_score_py(handle_t &handle,
int *predictions,
int *ref_predictions,
int n) except +
@cuml.internals.api_return_any()
def accuracy_score(ground_truth, predictions, handle=None, convert_dtype=True):
"""
Calcuates the accuracy score of a classification model.
Parameters
----------
handle : cuml.Handle
prediction : NumPy ndarray or Numba device
The labels predicted by the model for the test dataset
ground_truth : NumPy ndarray, Numba device
The ground truth labels of the test dataset
Returns
-------
float
The accuracy of the model used for prediction
"""
handle = Handle() \
if handle is None else handle
cdef handle_t* handle_ =\
<handle_t*><size_t>handle.getHandle()
cdef uintptr_t preds_ptr, ground_truth_ptr
preds_m, n_rows, _, _ = \
input_to_cuml_array(predictions,
convert_to_dtype=np.int32
if convert_dtype else None)
preds_ptr = preds_m.ptr
ground_truth_m, _, _, ground_truth_dtype=\
input_to_cuml_array(ground_truth,
convert_to_dtype=np.int32
if convert_dtype else None)
ground_truth_ptr = ground_truth_m.ptr
acc = accuracy_score_py(handle_[0],
<int*> preds_ptr,
<int*> ground_truth_ptr,
<int> n_rows)
return acc