def test_repeat_array(): n_elements = 10 x = np.zeros(n_elements) # repeating row-wise r1 = utils.repeat_array(x, length=1, axis=0) r2 = utils.repeat_array(x, length=2, axis=0) assert r1.shape == (1, n_elements) assert r2.shape == (2, n_elements) # repeating column-wise c1 = utils.repeat_array(x, length=1, axis=1) c2 = utils.repeat_array(x, length=2, axis=1) assert c1.shape == (n_elements, 1) assert c2.shape == (n_elements, 2)
def transform(self, x: ArrayLike) -> np.ndarray: """Scale covariates according to the feature range. Args: x: array-like of shape (n_samples, n_features) Input data that will be transformed. Returns: ndarray with transformed data. """ x = np.array(x) xarr = repeat_array(x, len(self.threshold_indices_), axis=-1) tarr = repeat_array(self.threshold_indices_.transpose(), len(x), axis=0) thresh = (xarr > tarr).reshape(x.shape[0], -1) return thresh.astype(np.uint8)
def transform(self, x: ArrayLike) -> np.ndarray: """Scale covariates according to the feature range. Args: x: array-like of shape (n_samples, n_features) Input data that will be transformed. Returns: ndarray with transformed data. """ x = np.array(x) xarr = repeat_array(x, self.n_hinges_ - 1, axis=-1) lharr = repeat_array(self.hinge_indices_[:-1].transpose(), len(x), axis=0) rharr = repeat_array(self.hinge_indices_[1:].transpose(), len(x), axis=0) lh = left_hinge(xarr, lharr, self.maxs_) rh = right_hinge(xarr, self.mins_, rharr) return np.concatenate((lh, rh), axis=2).reshape(x.shape[0], -1)
def right_hinge(x: ArrayLike, mn: float, mx: float) -> np.ndarray: """Computes hinge transformation values. Args: x: Array-like of covariate values mn: Minimum covariate value to fit hinges to mx: Maximum covariate value to fit hinges to Returns: Array of hinge features """ mn_broadcast = repeat_array(mn, mx.shape[-1], axis=1) return np.minimum(1, np.maximum(0, (x - mn_broadcast) / (mx - mn_broadcast)))
def left_hinge(x: ArrayLike, mn: float, mx: float) -> np.ndarray: """Computes hinge transformation values. Args: x: Array-like of covariate values mn: Minimum covariate value to fit hinges to mx: Maximum covariate value to fit hinges to Returns: Array of hinge features """ return np.minimum( 1, np.maximum(0, (x - mn) / (repeat_array(mx, mn.shape[-1], axis=1) - mn)))