# Pairs of data to Classify[]

Suppose I had the following data

a={1,2,3,4,5}


How would I turn this into

b={{1,2,3}->4,{2,3,4}->5}


That is, transforming the data into rules where each class has a definition as the three before it.

a = {1, 2, 3, 4, 5};

(Most[#] -> Last[#]) & /@ Partition[a, 4, 1]


{{1, 2, 3} -> 4, {2, 3, 4} -> 5}

• Great! Thanks so much! – Liam Schumm Apr 27 '15 at 18:21
• Yes, the 2nd argument to Partition is n+1; however, n must have a numeric (positive integer) value. – Bob Hanlon Apr 27 '15 at 18:29

Try this solution:

ruleTransformer[from___, to_] := {from} -> to
ruleTransformer @@@ Partition[Range[5], 4, 1]

(* {{1, 2, 3} -> 4, {2, 3, 4} -> 5} *)


Partition breaks the data into groups of 4, with start of each group shifted by 1 from the previous. We then apply the helper function ruleTransformer, which takes a list of arguments from___ to be put into the beginning of the Rule, and a single argument to_ to be put at the end of the rule.

We can accomplish the same thing (with a slight modification) with an anonymous Function:

Function[# -> Reverse[{##2}]] @@@ Reverse /@ Partition[Range[5], 4, 1]

DeveloperPartitionMap[Most@# -> Last@# &, a, 4, 1]
(* {{1, 2, 3} -> 4, {2, 3, 4} -> 5} *)

♃ = {#, #[[0]] @@ (# /. #[[0]] -> (1 + {##} &))} &@(#[[;; 3]] -> #[[4]] &@#) &;

♃ @ {1, 2, 3, 4, 5}
(* {{1, 2, 3} -> 4, {2, 3, 4} -> 5} *)

• ... posted this separately since it belongs to a different ... well, species. In case some/most of your alphabetic keys are not functioning replace ♃ with \$`. – kglr Apr 28 '15 at 6:04