average: The average of the ranks that would have been assigned to all the tied values is assigned to each value.
min: The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as “competition” ranking.)
max: The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.
dense: Like min, but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.
ordinal: All values are given a distinct rank, corresponding to the order that the values occur in a.
@torch.jit.script defrankdata_avg(input: torch.Tensor, dim: int = -1) -> torch.Tensor: """Assign ranks to data, ranks begin at 1. The average of the ranks that would have been assigned to all the tied values is assigned to each value. Examples: >>> input = torch.tensor([0, 2, 3, 2]) >>> rankdata_avg(input) tensor([1.0000, 2.5000, 4.0000, 2.5000]) """ input = input.swapdims(dim, -1).contiguous() sorted_input, _ = torch.sort(input, dim=-1) left = torch.searchsorted(sorted_input, input, right=False).swapdims(dim, -1) right = torch.searchsorted(sorted_input, input, right=True).swapdims(dim, -1) ranks = (left + right + 1) * 0.5 return ranks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@torch.jit.script defrankdata_min(input: torch.Tensor, dim: int = -1) -> torch.Tensor: """Assign ranks to data, ranks begin at 1. The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. Examples: >>> input = torch.tensor([0, 2, 3, 2]) >>> rankdata_min(input) tensor([1, 2, 4, 2]) """ input = input.swapdims(dim, -1).contiguous() sorted_input, _ = torch.sort(input, dim=-1) ranks = torch.searchsorted(sorted_input, input, right=False).swapdims(dim, -1) + 1 return ranks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@torch.jit.script defrankdata_max(input: torch.Tensor, dim: int = -1) -> torch.Tensor: """Assign ranks to data, ranks begin at 1. The maximum of the ranks that would have been assigned to all the tied values is assigned to each value. Examples: >>> input = torch.tensor([0, 2, 3, 2]) >>> rankdata_max(input) tensor([1, 3, 4, 3]) """ input = input.swapdims(dim, -1).contiguous() sorted_input, _ = torch.sort(input, dim=-1) ranks = torch.searchsorted(sorted_input, input, right=True).swapdims(dim, -1) return ranks
@torch.jit.script defrankdata_ordinal(input: torch.Tensor, dim: int = -1) -> torch.Tensor: """Assign ranks to data, ranks begin at 1. All values are given a distinct rank, corresponding to the order that the values occur in `input`. Examples: >>> input = torch.tensor([0, 2, 3, 2]) >>> rankdata_ordinal(input) tensor([1, 2, 4, 3]) """ dim = (dim + input.ndim) % input.ndim indices = torch.argsort(input, dim=dim) shape = [1if i != dim else -1for i inrange(input.ndim)] ranks = torch.arange(1, input.size(dim) + 1, device=input.device).view(shape).expand_as(input) output = torch.empty_like(input, dtype=torch.long) output.scatter_(dim, indices, ranks) return output