def inverse_transform(self, y, threshold=None): """ Transform binary labels back to original multi-class labels Parameters ---------- y : array of shape [n_samples, n_classes] threshold : float this value is currently ignored Returns ------- arr : array with original labels """ if has_scipy(): from scipy.sparse import isspmatrix as scipy_sparse_isspmatrix else: from cuml.common.import_utils import dummy_function_always_false \ as scipy_sparse_isspmatrix # If we are already given multi-class, just return it. if cupyx.scipy.sparse.isspmatrix(y): y_mapped = y.tocsr().indices.astype(self._classes_.dtype) elif scipy_sparse_isspmatrix(y): y = y.tocsr() y_mapped = rmm_cupy_ary(cp.array, y.indices, dtype=y.indices.dtype) else: y_mapped = rmm_cupy_ary(cp.argmax, rmm_cupy_ary(cp.asarray, y, dtype=y.dtype), axis=1).astype(y.dtype) return invert_labels(y_mapped, self._classes_)
def inverse_transform(self, y, threshold=None): """ Transform binary labels back to original multi-class labels Parameters ---------- y : array of shape [n_samples, n_classes] threshold : float this value is currently ignored Returns ------- arr : array with original labels """ # If we are already given multi-class, just return it. if cp.sparse.isspmatrix(y): y_mapped = y.tocsr().indices.astype(self.classes_.dtype) elif scipy.sparse.isspmatrix(y): y = y.tocsr() y_mapped = rmm_cupy_ary(cp.array, y.indices, dtype=y.indices.dtype) else: y_mapped = rmm_cupy_ary(cp.argmax, rmm_cupy_ary(cp.asarray, y, dtype=y.dtype), axis=1).astype(y.dtype) return invert_labels(y_mapped, self.classes_)
def predict(self, X) -> CumlArray: """ Perform classification on an array of test vectors X. """ if has_scipy(): from scipy.sparse import isspmatrix as scipy_sparse_isspmatrix else: from cuml.common.import_utils import dummy_function_always_false \ as scipy_sparse_isspmatrix # todo: use a sparse CumlArray style approach when ready # https://github.com/rapidsai/cuml/issues/2216 if scipy_sparse_isspmatrix(X) or cupyx.scipy.sparse.isspmatrix(X): X = X.tocoo() rows = cp.asarray(X.row, dtype=X.row.dtype) cols = cp.asarray(X.col, dtype=X.col.dtype) data = cp.asarray(X.data, dtype=X.data.dtype) X = cupyx.scipy.sparse.coo_matrix((data, (rows, cols)), shape=X.shape) else: X = input_to_cupy_array(X, order='K').array jll = self._joint_log_likelihood(X) indices = cp.argmax(jll, axis=1).astype(self.classes_.dtype) y_hat = invert_labels(indices, classes=self.classes_) return y_hat
def predict(self, X): """ Perform classification on an array of test vectors X. Parameters ---------- X : array-like of shape (n_samples, n_features) Returns ------- C : cupy.ndarray of shape (n_samples) """ if isinstance(X, np.ndarray) or isinstance(X, cp.ndarray): X = cp.asarray(X, X.dtype) elif scipy.sparse.isspmatrix(X) or cp.sparse.isspmatrix(X): X = X.tocoo() rows = cp.asarray(X.row, dtype=X.row.dtype) cols = cp.asarray(X.col, dtype=X.col.dtype) data = cp.asarray(X.data, dtype=X.data.dtype) X = cp.sparse.coo_matrix((data, (rows, cols)), shape=X.shape) jll = self._joint_log_likelihood(X) indices = cp.argmax(jll, axis=1).astype(self.classes_.dtype) y_hat = invert_labels(indices, classes=self.classes_) return y_hat
def predict(self, X) -> CumlArray: """ Perform classification on an array of test vectors X. """ if has_scipy(): from scipy.sparse import isspmatrix as scipy_sparse_isspmatrix else: from cuml.common.import_utils import dummy_function_always_false \ as scipy_sparse_isspmatrix # todo: use a sparse CumlArray style approach when ready # https://github.com/rapidsai/cuml/issues/2216 if scipy_sparse_isspmatrix(X) or cupyx.scipy.sparse.isspmatrix(X): X = _convert_x_sparse(X) else: X = input_to_cupy_array( X, order='K', check_dtype=[cp.float32, cp.float64, cp.int32]).array jll = self._joint_log_likelihood(X) indices = cp.argmax(jll, axis=1).astype(self.classes_.dtype) y_hat = invert_labels(indices, classes=self.classes_) return y_hat
def test_monotonic_validate_invert_labels(arr_type, dtype, copy): arr = np.array([0, 15, 10, 50, 20, 50], dtype=dtype) original = arr.copy() if arr_type == "cp": arr = cp.asarray(arr, dtype=dtype) arr_orig = arr.copy() monotonic, mapped_classes = make_monotonic(arr, copy=copy) cp.cuda.Stream.null.synchronize() assert array_equal(monotonic.get(), np.array([0, 2, 1, 4, 3, 4])) # We only care about in-place updating if data is on device if arr_type == "cp": if copy: assert array_equal(arr_orig.get(), arr.get()) else: assert array_equal(arr.get(), monotonic.get()) wrong_classes = cp.asarray([0, 1, 2], dtype=dtype) val_labels = check_labels(monotonic.get(), classes=wrong_classes) cp.cuda.Stream.null.synchronize() assert not val_labels correct_classes = cp.asarray([0, 1, 2, 3, 4], dtype=dtype) val_labels = check_labels(monotonic.get(), classes=correct_classes) cp.cuda.Stream.null.synchronize() assert val_labels if arr_type == "cp": monotonic_copy = monotonic.copy() inverted = invert_labels(monotonic, classes=cp.asarray([0, 10, 15, 20, 50], dtype=dtype), copy=copy) cp.cuda.Stream.null.synchronize() if arr_type == "cp": if copy: assert array_equal(monotonic_copy.get(), monotonic.get()) else: assert array_equal(monotonic.get(), arr_orig.get()) assert array_equal(inverted.get(), original)