Message Boards Message Boards

7
|
5953 Views
|
4 Replies
|
8 Total Likes
View groups...
Share
Share this post:

BinListsBy

Posted 9 years ago

Several recent posts have remarked that it would be useful to have a general form of BinLsts which could bin a list by means of a function applied to the first level, similar in principal to SortBy. After some thinking, it occurred to me that functionality of the built-in BinLists could be transferred to a user-defined binListsBy if BinLists itself was used in the implementation.

The function BinListsBy defined below will bin the first level items in data using the values obtained when the the function f is applied to each item. binListsBy[data,f,options] accepts 2 arguments and an optional sequence of options. f is a function. When f is mapped onto the first level of data it must return a list of values acceptable to BinLists. options is an optional sequence of options which will be passed directly to BinLists. The built-in function BinLists does the real work. (An attached notebook contains the function and examples.)

Define the binListsBy function

binListsBy[data_, f_, opts___] := Module[{binBy, binning, select},
  (* function f must return a list acceptable to BinLists *)
  binBy = f /@ data;
  (* construct bins of binBy values *)
  binning = Union /@ BinLists[binBy, opts];
  (* selects data elements for which f[element] is in a list *)
  select[l_] := Select[data, MemberQ[l, f[#]] &];
  (* use select to bin the original data according to the binning \
lists *)
  select /@ binning
  ]

Here is an example which uses the function twice to produce a 2D binning:

Some vectors

data = RandomInteger[{0, 10}, {200, 2}];

Bin by Part 1 and then 2 with bin width 2 from 0 to 10

out = binListsBy[#, #[[2]] &, {0, 10, 2}] & /@ 
   binListsBy[data, #[[1]] &, {0, 10, 2}];
Grid[out, Frame -> All]

enter image description here

Attachments:
POSTED BY: David Keith
4 Replies

Hi David,

Though this is nice and works, the performance is lacking. This has n*m performance (n being the number of items, m being the number of bins). A normal binning algorithm does its work in n time. It scans all the items once. The binning and the Map of a Select makes it scan m times!

As per the name; good choice! Now we just have to wait for a solid, high-performance, implementation.

--SH

POSTED BY: Sander Huisman
Posted 9 years ago

Yes -- I quite agree. I hope this will be implemented as a built-in function.

enter image description here

POSTED BY: David Keith

Thanks for the function. But can I make a suggestion not to use UpperCaseFirstLetter for user defined functions?

They can confuse as one might think they are build-in Mathematica official functions. Also, there is a risk if in the future WRI makes such a function with same name, then there will be conflict. It is best to keep all user functions with lowerCaseFirstLetter.

POSTED BY: Nasser M. Abbasi
Posted 9 years ago

Thank you, Nasser. I do agree with you. I allowed myself an exception here because I knew I was occupying a name Mathematica does not use -- or I would be using the built-in function. But I think you are right. If they do add this they will use this name, and I would not then want a conflict.

I revised the original post to the better usage.

Best, David

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract