Function Repository Resource:

ValuesReduceOperator

Source Notebook

Group data according to the positionally corresponding values of other data

Contributed by: Seth J. Chandler

ResourceFunction["ValuesReduceOperator"][]

creates a pure function that will group the data in its first argument according to the value in the same position of the second argument.

ResourceFunction["ValuesReduceOperator"][h]

reduces the groups of data by applying the function h to each of the groups.

Details

ResourceFunction["ValuesReduceOperator"] is similar to the resource function MapReduceOperator, except that instead of the grouping being done based on a function of the data, the grouping is done based on pre-computed values, which become the keys to the Association produced by the function.

Examples

Basic Examples (2) 

Group a four-membered list of pairs according to the values of a four-membered list:

In[1]:=
ResourceFunction[
  "ValuesReduceOperator"][][{{3, 4}, {4, 5}, {1, 2}, {0, 0}}, {1, 0, 0, 1}]
Out[1]=

Group a four member list of pairs according to the values of a four member list and produce a MatrixPlot of each grouping:

In[2]:=
ResourceFunction["ValuesReduceOperator"][
  MatrixPlot][{{3, 4}, {4, 5}, {1, 2}, {0, 0}}, {1, 0, 0, 1}]
Out[2]=

Scope (2) 

The data to be grouped can be anything:

In[3]:=
ResourceFunction["ValuesReduceOperator"][][
 CharacterRange["a", "z"], {2, 2, 3, 1, 3, 3, 3, 1, 3, 1, 2, 2, 2, 3, 2, 1, 1, 2, 2, 3, 2, 3, 3, 1, 2, 2}]
Out[3]=

The values can be anything:

In[4]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/6cebec7c-f41b-48a1-824c-079a97807c6f"]
Out[4]=

Applications (3) 

A feature matrix and a target vector:

In[5]:=
features = {{5, 3}, {8, 2}, {10, 2}, {1, 10}, {9, 8}, {1, 9}, {10, 5}, {5, 3}, {3, 7}, {3, 5}};
target = {False, False, True, False, True, False, True, False, True, False};

Plot the features color-coded by the targets:

In[6]:=
ResourceFunction["ValuesReduceOperator"][][features, target] // ListPlot
Out[6]=

Show the mean value of features grouped by the value of the target:

In[7]:=
ResourceFunction["ValuesReduceOperator"][Mean][features, target] // KeyValueMap[Callout[#2, #1] &] // ListPlot
Out[7]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 24 March 2021

Related Resources

License Information