unsorted_segment_mean#

ivy.unsorted_segment_mean(data, segment_ids, num_segments)[source]#

Compute the mean of elements along segments of an array. Segments are defined by an integer array of segment IDs.

Parameters:
  • data (Union[ivy.Array, ivy.NativeArray]) – The array from which to gather values.

  • segment_ids (Union[ivy.Array, ivy.NativeArray]) – Must be in the same size with the first dimension of data. Has to be of integer data type. The index-th element of segment_ids array is the segment identifier for the index-th element of data.

  • num_segments (Union[int, ivy.Array, ivy.NativeArray]) – An integer or array representing the total number of distinct segment IDs.

Return type:

Array

Returns:

ivy.Array – The output array, representing the result of a segmented mean operation. For each segment, it computes the mean value in data where segment_ids equals to segment ID.

Array.unsorted_segment_mean(self, segment_ids, num_segments)[source]#

Compute the mean of values in the array ‘self’ based on segment identifiers.

Parameters:
  • self (ivy.Array) – The array from which to gather values.

  • segment_ids (ivy.Array) – Must be in the same size with the first dimension of self. Has to be of integer data type. The index-th element of segment_ids array is the segment identifier for the index-th element of self.

  • num_segments (Union[int, ivy.Array]) – An integer or array representing the total number of distinct segment IDs.

Return type:

Array

Returns:

ret (ivy.Array) – The output array, representing the result of a segmented mean operation. For each segment, it computes the mean of values in self where segment_ids equals to segment ID.

Examples

>>> data = ivy.array([1.0, 2.0, 3.0, 4.0])
>>> segment_ids = ivy.array([0, 0, 0, 0])
>>> num_segments = 1
>>> result = ivy.unsorted_segment_mean(data, segment_ids, num_segments)
>>> result
ivy.array([2.5])
>>> data = ivy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
>>> segment_ids = ivy.array([0, 0, 1, 1, 2, 2])
>>> num_segments = 3
>>> result = ivy.unsorted_segment_mean(data, segment_ids, num_segments)
>>> result
ivy.array([[1.5, 3.5, 5.5],[1.5, 3.5, 5.5],[1.5, 3.5, 5.5]])
Container.unsorted_segment_mean(self, segment_ids, num_segments)[source]#

Compute the mean of values in the input array or container based on segment identifiers.

Parameters:
  • self (ivy.Container) – Input array or container from which to gather the input.

  • segment_ids (ivy.Container) – An array of integers indicating the segment identifier for each element in ‘self’.

  • num_segments (Union[int, ivy.Container]) – An integer or array representing the total number of distinct segment IDs.

Return type:

Container

Returns:

ivy.Container – A container representing the result of a segmented mean operation. For each segment, it computes the mean of values in ‘self’ where ‘segment_ids’ equals the segment ID.

Example

>>> data = ivy.Container(a=ivy.array([0., 1., 2., 4.]),
...                      b=ivy.array([3., 4., 5., 6.]))
>>> segment_ids = ivy.array([0, 0, 1, 1])
>>> num_segments = 2
>>> result = ivy.unsorted_segment_mean(data, segment_ids, num_segments)
>>> print(result)
{
    a: ivy.array([0.5, 3.0]),
    b: ivy.array([3.5, 5.5])
}
>>> data = ivy.Container(a=ivy.array([0., 1., 2., 4., 5., 6.]),
...                      b=ivy.array([3., 4., 5., 6., 7., 8.]))
>>> segment_ids = ivy.array([0, 0, 1, 1, 2, 2])
>>> num_segments = 3
>>> result = ivy.unsorted_segment_mean(data, segment_ids, num_segments)
>>> print(result)
{
    a: ivy.array([0.5, 3.0, 5.5]),
    b: ivy.array([3.5, 5.5, 7.5])
}