def score(self, X, y, **kwargs): """Scoring function for based on mean accuracy. Parameters ---------- X : [cudf.DataFrame] Test samples on which we predict y : [cudf.Series, device array, or numpy array] Ground truth values for predict(X) Returns ------- score : float Accuracy of self.predict(X) wrt. y (fraction where y == pred_y) """ from cuml.metrics.accuracy import accuracy_score from cuml.utils import input_to_dev_array X_m = input_to_dev_array(X)[0] y_m = input_to_dev_array(y)[0] if hasattr(self, 'handle'): handle = self.handle else: handle = None return accuracy_score(y_m, cuda.to_device(self.predict(X_m)), handle=handle)
def test_convert_order_dev_array(dtype, input_type, from_order, to_order): input_data, real_data = get_input(input_type, 10, 10, dtype, order=from_order) # conv_data = np.array(real_data, order=to_order, copy=True) if from_order == to_order: conv_data, _, _, _, _ = \ input_to_dev_array(input_data, fail_on_order=False, order=to_order) else: # Warning is raised for non cudf dataframe or numpy arrays # those are converted form order by their respective libraries if input_type in ['cupy', 'numba']: with pytest.warns(UserWarning): conv_data, _, _, _, _ = \ input_to_dev_array(input_data, fail_on_order=False, order=to_order) else: conv_data, _, _, _, _ = \ input_to_dev_array(input_data, fail_on_order=False, order=to_order) assert (check_numba_order(conv_data, to_order)) np.testing.assert_equal(real_data, conv_data.copy_to_host())
def test_dtype_check(dtype, check_dtype, input_type, order): if (dtype == np.float16 or check_dtype == np.float16)\ and input_type != 'numpy': pytest.xfail("float16 not yet supported by numba/cuDF.from_gpu_matrix") if dtype in [np.uint8, np.uint16, np.uint32, np.uint64]: if input_type == 'dataframe': pytest.xfail("unsigned int types not yet supported by \ cuDF") input_data, real_data = get_input(input_type, 10, 10, dtype, order=order) if input_type == 'cupy' and input_data is None: pytest.skip('cupy not installed') if dtype == check_dtype: _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype=check_dtype, order=order) assert got_dtype == check_dtype else: with pytest.raises(TypeError): _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype=check_dtype, order=order)
def score(self, X, y, **kwargs): """Scoring function for linear classifiers Returns the coefficient of determination R^2 of the prediction. Parameters ---------- X : [cudf.DataFrame] Test samples on which we predict y : [cudf.Series, device array, or numpy array] Ground truth values for predict(X) Returns ------- score : float R^2 of self.predict(X) wrt. y. """ from cuml.metrics.regression import r2_score from cuml.utils import input_to_dev_array X_m = input_to_dev_array(X)[0] y_m = input_to_dev_array(y)[0] if hasattr(self, 'handle'): handle = self.handle else: handle = None return r2_score(y_m, cuda.to_device(self.predict(X_m)), handle=handle)
def test_input_to_dev_array(dtype, input_type, num_rows, num_cols, order): input_data, real_data = get_input(input_type, num_rows, num_cols, dtype, order=order) if input_type == 'cupy' and input_data is None: pytest.skip('cupy not installed') # 1 row with order F triggers conversion warning if cupy or numba # not with numpy since numpy does the conversion if (num_rows == 1 and not num_cols == 1) and order == 'F' and \ input_type in ['cupy', 'numba']: with pytest.warns(UserWarning): X, X_ptr, n_rows, n_cols, dtype = input_to_dev_array(input_data, order=order) # 1 col with order C triggers conversion warning if cupy or numba # not with numpy since numpy does the conversion elif (num_cols == 1 and not num_rows == 1) and order == 'F' and \ input_type in ['cupy', 'numba']: with pytest.warns(UserWarning): X, X_ptr, n_rows, n_cols, dtype = input_to_dev_array(input_data, order=order) else: X, X_ptr, n_rows, n_cols, dtype = input_to_dev_array(input_data, order=order) np.testing.assert_equal(X.copy_to_host(), real_data) assert n_rows == num_rows assert n_cols == num_cols assert dtype == dtype del input_data del real_data
def test_dtype_check(dtype, check_dtype, input_type, order): if (dtype == np.float16 or check_dtype == np.float16)\ and input_type != 'numpy': pytest.xfail("float16 not yet supported by numba/cuDF.from_gpu_matrix") input_data, real_data = get_input(input_type, 10, 10, dtype, order=order) if input_type == 'cupy' and input_data is None: pytest.skip('cupy not installed') if dtype == check_dtype: _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype=check_dtype, order=order) assert got_dtype == check_dtype else: with pytest.raises(TypeError): _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype=check_dtype, order=order) # check if error is raise when input is not expected dtype with pytest.raises(ValueError): _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype='float32', order=order)
def test_fail_on_order(dtype, input_type, order, order_check): # this is tested only for non cudf dataframe or numpy arrays # those are converted form order by their respective libraries input_data, real_data = get_input(input_type, 10, 10, dtype, order=order) if input_type == 'cupy' and input_data is None: pytest.skip('cupy not installed') if order == order_check: _, _, _, _, _ = \ input_to_dev_array(input_data, fail_on_order=False, order=order) else: with pytest.raises(ValueError): _, _, _, _, _ = \ input_to_dev_array(input_data, fail_on_order=True, order=order_check)
def test_dtype_check(dtype, check_dtype, input_type): if (dtype == np.float16 or check_dtype == np.float16)\ and input_type != 'numpy': pytest.xfail("float16 not yet supported by numba/cuDF.from_gpu_matrix") input_data, real_data = get_input(input_type, 10, 10, dtype) if input_data is None: pytest.skip('cupy not installed') if dtype == check_dtype: _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype=check_dtype) assert got_dtype == check_dtype else: with pytest.raises(TypeError): _, _, _, _, got_dtype = \ input_to_dev_array(input_data, check_dtype=check_dtype)
def score(self, X, y, **kwargs): """Scoring function for linear classifiers Returns the coefficient of determination R^2 of the prediction. Parameters ---------- X : [cudf.DataFrame] Test samples on which we predict y : [cudf.Series] True values for predict(X) Returns ------- score : float R^2 of self.predict(X) wrt. y. """ from cuml.metrics.regression import r2_score from cuml.utils import input_to_dev_array X_m = input_to_dev_array(X)[0] y_m = input_to_dev_array(y)[0] return r2_score(y_m, cuda.to_device(self.predict(X_m)))
def test_input_to_dev_array(dtype, input_type, num_rows, num_cols): input_data, real_data = get_input(input_type, num_rows, num_cols, dtype) if input_data is None: pytest.skip('cupy not installed') X, X_ptr, n_rows, n_cols, dtype = input_to_dev_array(input_data) np.testing.assert_equal(X.copy_to_host(), real_data) assert n_rows == num_rows assert n_cols == num_cols assert dtype == dtype del input_data del real_data