Function Repository Resource:

PairMap

Source Notebook

Map a function to pairs formed from a list and another function

Contributed by: Seth J. Chandler

ResourceFunction["PairMap"][f,args]

creates a data structure whose head is the same as the head of args and each element of which is a list with the first element in args, and the second element has function f applied to that first element.

ResourceFunction["PairMap"][f,args,g]

applies g to each element in the result.

ResourceFunction["PairMap"][f,args,g,h]

wraps h around the overall result.

ResourceFunction["PairMap"][f]

is an operator form of ResourceFunction["PairMap"] that can be applied to arguments.

Details and Options

ResourceFunction["PairMap"][f,args,g,h] wraps the function h around the output from ResourceFunction["PairMap"][f,args,h].

Examples

Basic Examples (3) 

Create a list of lists with elements of the form {x,f[x]}:

In[1]:=
ResourceFunction["PairMap"][f, {1, 2, 3}]
Out[1]=

Create a list of rules of the form xf[x]:

In[2]:=
ResourceFunction["PairMap"][f, {1, 2, 3}, Rule]
Out[2]=

Apply an operator form of PairMap to some data:

In[3]:=
ResourceFunction["PairMap"][f][{1, 2, 3}]
Out[3]=

Scope (2) 

The head of the result returned by PairMap is the same as the head of args:

In[4]:=
ResourceFunction["PairMap"][f, q[1, 2, 3]]
Out[4]=

A four-argument form of PairMap in which the function h is composed with the result:

In[5]:=
ResourceFunction["PairMap"][f, {1, 2, 3}, g, h]
Out[5]=

Applications (3) 

Use PairMap to create a Dataset in which the keys are integers and the columns are the squares and cubes of those integers:

In[6]:=
ResourceFunction["PairMap"][
 AssociationThread[{"squared", "cubed"}, Through[{#^2 &, #^3 &}[#]]] &, {1, 2, 3}, Rule, Association/*Dataset]
Out[6]=

Get data in a form suitable for statistical functions such as LinearModelFit:

In[7]:=
data = {{3, 4, 2}, {1, 6, 2.3}, {2, 1, 4.3}, {5, 4, 3.1}};
In[8]:=
ResourceFunction["PairMap"][(#[[1]]*#[[2]])/#[[3]]^2 &, data, Append]
Out[8]=

Get data in a form suitable for machine learning functions such as Predict, Classify or NetTrain:

In[9]:=
ResourceFunction["PairMap"][(#[[1]]*#[[2]])/#[[3]]^2 &, data, Rule]
Out[9]=

Properties and Relations (1) 

Replicate the functionality of AssociationMap:

In[10]:=
ResourceFunction["PairMap"][f, {1, 2, 3}, Rule, Association]
Out[10]=

Neat Examples (1) 

Take a complex model and build local emulators around a point {0.4,0.7} using LASSO with varying degrees of regularization in a process that approximates the LIME algorithm for explaining complex machine learning models:

In[11]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/d5931516-74bd-4bc8-81cb-f42640705bc4"]
Out[11]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 06 May 2020

Source Metadata

License Information