def test_ranking_5(self): "farray.ranking_5" x = np.array([3.0, 1.0, 2.0])[:, None] desired = np.array([0.0, 0.0, 0.0])[:, None] with np.errstate(invalid='ignore'): actual = ranking(x, axis=1) assert_almost_equal(actual, desired)
def test_ranking_5(self): "farray.ranking_5" x = np.array([3.0, 1.0, 2.0])[:,None] desired = np.array([0.0, 0.0, 0.0])[:,None] with np.errstate(invalid='ignore'): actual = ranking(x, axis=1) assert_almost_equal(actual, desired)
def test_ranking_17(self): "farray.ranking_17" x = np.array([[nan, 1.0, 1.0, 1.0], [1.0, 1.5, 2.0, 2.0], [2.0, nan, 1.0, 2.0], [2.0, 1.5, 1.0, 2.0]]) desired = np.array([[nan, -1.0, -1.0, -1.0], [-1.0, 0.0, 1.0, 1.0], [1.0, nan, -1.0, 1.0], [1.0, 0.0, -1.0, 1.0]]) desired *= 8.0 / 13 actual = ranking(x, axis=None) assert_almost_equal(actual, desired)
def test_ranking_16(self): "farray.ranking_16" x = np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 2.0, 2.0], [2.0, 2.0, 1.0, 2.0], [2.0, 2.0, 1.0, 2.0]]) desired = np.array([[-1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, 1.0, 1.0], [1.0, 1.0, -1.0, 1.0], [1.0, 1.0, -1.0, 1.0]]) desired *= 8.0 / 15 actual = ranking(x, axis=None) assert_almost_equal(actual, desired)
def test_ranking_2(self): "farray.ranking_2" x = np.array([[1.0, nan, 2.0, nan, nan], [2.0, 2.0, nan, nan, nan], [3.0, 3.0, 3.0, 3.0, nan]]) desired = np.array([[-1.0, nan, -1.0, nan, nan], [0.0, -1.0, nan, nan, nan], [1.0, 1.0, 1.0, 0.0, nan]]) with np.errstate(invalid='ignore'): actual = ranking(x) assert_almost_equal(actual, desired)
def test_ranking_8(self): "farray.ranking_8" x = np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 2.0, 2.0], [2.0, 2.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0]]) desired = np.array([[-2.0 / 3, -2.0 / 3, -1.0, -1.0], [-2.0 / 3, -2.0 / 3, -1.0 / 3, 0.0], [2.0 / 3, 1.0 / 3, 2.0 / 3, 0.0], [2.0 / 3, 1.0, 2.0 / 3, 1.0]]) actual = ranking(x, 0) assert_almost_equal(actual, desired)
def test_ranking_7(self): "farray.ranking_7" x = np.array([[1.0, nan, 1.0, nan, nan], [1.0, 1.0, nan, nan, nan], [1.0, 2.0, 0.0, 2.0, nan], [1.0, 3.0, 1.0, 1.0, 0.0]]) desired = np.array([[0.0, nan, 0.0, nan, nan], [0.0, 0.0, nan, nan, nan], [-1.0 / 3, 2.0 / 3, -1.0, 2.0 / 3, nan], [0.0, 1.0, 0.0, 0.0, -1.0]]) actual = ranking(x, 1) assert_almost_equal(actual, desired)
def test_ranking_11(self): "farray.ranking_11" x = np.array([[ nan, nan], [ nan, nan], [ nan, nan]]) desired = np.array([[ nan, nan], [ nan, nan], [ nan, nan]]) actual = ranking(x, axis=0) assert_almost_equal(actual, desired)
def test_ranking_3(self): "farray.ranking_3" x = np.array([[1.0, nan, 2.0, nan, nan], [2.0, 2.0, nan, nan, nan], [3.0, 3.0, 3.0, 3.0, nan], [4.0, 2.0, 3.0, 1.0, 0.0]]) desired = np.array([[-1.0, nan, 1.0, nan, nan], [0.0, 0.0, nan, nan, nan], [0.0, 0.0, 0.0, 0.0, nan], [1.0, 0.0, 0.5, -0.5, -1.0]]) actual = ranking(x, axis=1) assert_almost_equal(actual, desired)
def test_ranking_2(self): "farray.ranking_2" x = np.array([[ 1.0, nan, 2.0, nan, nan], [ 2.0, 2.0, nan, nan, nan], [ 3.0, 3.0, 3.0, 3.0, nan]]) desired = np.array([[-1.0, nan, -1.0, nan, nan], [ 0.0, -1.0, nan, nan, nan], [ 1.0, 1.0, 1.0, 0.0, nan]]) with np.errstate(invalid='ignore'): actual = ranking(x) assert_almost_equal(actual, desired)
def test_ranking_9(self): "farray.ranking_9" x = np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 2.0, 2.0], [2.0, 2.0, 3.0, 2.0], [2.0, 3.0, 3.0, 3.0]]) x = x.T desired = np.array([[-2.0 / 3, -2.0 / 3, -1.0, -1.0], [-2.0 / 3, -2.0 / 3, -1.0 / 3, 0.0], [2.0 / 3, 1.0 / 3, 2.0 / 3, 0.0], [2.0 / 3, 1.0, 2.0 / 3, 1.0]]) desired = desired.T actual = ranking(x, 1) assert_almost_equal(actual, desired)
def test_ranking_3(self): "farray.ranking_3" x = np.array([[ 1.0, nan, 2.0, nan, nan], [ 2.0, 2.0, nan, nan, nan], [ 3.0, 3.0, 3.0, 3.0 , nan], [ 4.0, 2.0, 3.0, 1.0 , 0.0 ]]) desired = np.array([[-1.0, nan, 1.0, nan, nan], [ 0.0, 0.0, nan, nan, nan], [ 0.0, 0.0, 0.0, 0.0, nan], [ 1.0, 0.0, 0.5, -0.5, -1.0]]) actual = ranking(x, axis=1) assert_almost_equal(actual, desired)
def test_ranking_8(self): "farray.ranking_8" x = np.array([[ 1.0, 1.0, 1.0, 1.0], [ 1.0, 1.0, 2.0, 2.0], [ 2.0, 2.0, 3.0, 2.0], [ 2.0, 3.0, 3.0, 3.0]]) desired = np.array([[-2.0/3, -2.0/3, -1.0, -1.0], [-2.0/3, -2.0/3, -1.0/3, 0.0], [ 2.0/3, 1.0/3, 2.0/3, 0.0], [ 2.0/3, 1.0, 2.0/3, 1.0]]) actual = ranking(x, 0) assert_almost_equal(actual, desired)
def test_ranking_7(self): "farray.ranking_7" x = np.array([[ 1.0, nan, 1.0, nan, nan], [ 1.0, 1.0, nan, nan, nan], [ 1.0, 2.0, 0.0, 2.0, nan], [ 1.0, 3.0, 1.0, 1.0, 0.0]]) desired = np.array([[ 0.0, nan , 0.0, nan , nan], [ 0.0, 0.0 , nan, nan , nan], [-1.0/3, 2.0/3, -1.0, 2.0/3, nan], [ 0.0, 1.0 , 0.0, 0.0 , -1.0]]) actual = ranking(x, 1) assert_almost_equal(actual, desired)
def test_ranking_17(self): "farray.ranking_17" x = np.array([[ nan, 1.0, 1.0, 1.0], [ 1.0, 1.5, 2.0, 2.0], [ 2.0, nan, 1.0, 2.0], [ 2.0, 1.5, 1.0, 2.0]]) desired = np.array([[ nan, -1.0, -1.0, -1.0], [-1.0, 0.0, 1.0, 1.0], [ 1.0, nan, -1.0, 1.0], [ 1.0, 0.0, -1.0, 1.0]]) desired *= 8.0 / 13 actual = ranking(x, axis=None) assert_almost_equal(actual, desired)
def test_ranking_16(self): "farray.ranking_16" x = np.array([[ 1.0, 1.0, 1.0, 1.0], [ 1.0, 1.0, 2.0, 2.0], [ 2.0, 2.0, 1.0, 2.0], [ 2.0, 2.0, 1.0, 2.0]]) desired = np.array([[-1.0, -1.0, -1.0, -1.0], [-1.0, -1.0, 1.0, 1.0], [ 1.0, 1.0, -1.0, 1.0], [ 1.0, 1.0, -1.0, 1.0]]) desired *= 8.0 / 15 actual = ranking(x, axis=None) assert_almost_equal(actual, desired)
def test_ranking_9(self): "farray.ranking_9" x = np.array([[ 1.0, 1.0, 1.0, 1.0], [ 1.0, 1.0, 2.0, 2.0], [ 2.0, 2.0, 3.0, 2.0], [ 2.0, 3.0, 3.0, 3.0]]) x = x.T desired = np.array([[-2.0/3, -2.0/3, -1.0, -1.0], [-2.0/3, -2.0/3, -1.0/3, 0.0], [ 2.0/3, 1.0/3, 2.0/3, 0.0], [ 2.0/3, 1.0, 2.0/3, 1.0]]) desired = desired.T actual = ranking(x, 1) assert_almost_equal(actual, desired)
def group_ranking(x, groups, norm='-1,1', axis=0): """ Ranking within groups along axis. Parameters ---------- x : ndarray Data to be ranked. groups : list List of group membership of each element along axis=0. norm : str A string that specifies the normalization: '0,N-1' Zero to N-1 ranking '-1,1' Scale zero to N-1 ranking to be between -1 and 1 'gaussian' Rank data then scale to a Gaussian distribution axis : int, {default: 0} axis along which the ranking is calculated Returns ------- idx : ndarray The ranked data. The dtype of the output is always np.float even if the dtype of the input is int. Notes ---- If there is only one non-NaN value within a group along the axis=0, then that value is set to the midpoint of the specified normalization method. For '0,N-1' normalization, note that N is the number of element in the group even in there are NaNs. """ # Find set of unique groups ugroups = unique_group(groups) # Convert groups to a numpy array groups = np.asarray(groups) # Loop through unique groups and normalize xnorm = np.nan * np.zeros(x.shape) for group in ugroups: idx = groups == group idxall = [slice(None)] * x.ndim idxall[axis] = idx xnorm[idxall] = ranking(x[idxall], axis=axis, norm=norm) return xnorm
def test_ranking_15(self): "farray.ranking_15" x = np.array([ -np.inf, nan, 1.0, np.inf]) desired = np.array([-1.0, nan, 0.0, 1.0]) actual = ranking(x, axis=None) assert_almost_equal(actual, desired)
def test_ranking_13(self): "farray.ranking_13" x = np.array([ 1.0, np.inf, 2.0]) desired = np.array([-1.0, 1.0, 0.0]) actual = ranking(x, axis=0) assert_almost_equal(actual, desired)
def test_ranking_4(self): "farray.ranking_4" x = np.array([3.0, 1.0, 2.0])[:, None] desired = np.array([1.0, -1.0, 0.0])[:, None] actual = ranking(x, axis=0) assert_almost_equal(actual, desired)
def test_ranking_13(self): "farray.ranking_13" x = np.array([1.0, np.inf, 2.0]) desired = np.array([-1.0, 1.0, 0.0]) actual = ranking(x, axis=0) assert_almost_equal(actual, desired)
def test_ranking_4(self): "farray.ranking_4" x = np.array([3.0, 1.0, 2.0])[:,None] desired = np.array([1.0,-1.0, 0.0])[:,None] actual = ranking(x, axis=0) assert_almost_equal(actual, desired)
def test_ranking_15(self): "farray.ranking_15" x = np.array([-np.inf, nan, 1.0, np.inf]) desired = np.array([-1.0, nan, 0.0, 1.0]) actual = ranking(x, axis=None) assert_almost_equal(actual, desired)