# How can I iterate my code a 1000 times?

The exercise is about classifying random Points by a (randomly generated) target function to 2 classes (-1 and +1), build a new target function by linear Regression and compare where they don't classify to the same value.

first I generate two Random Points to build the target function

funktionsPunkte = RandomReal[{-1, 1}, {2, 2}]


then generate the target function from it (to classify the data points)

targetFct[{x1_, x2_}] :=
With[{targetFunction = LinearModelFit[funktionsPunkte, {1, t}, t]},
x2 - targetFunction[x1]];


then I generate 100 data Points and classify them to either +1 or -1 ,depending if they are above or below the target function

data = With[{points = RandomReal[{-1, 1}, {100, 2}]},
Map[{Prepend[#, 1], If[targetFct[#] < 0, -1, 1]} &, points]];


one data Point is of the form {{1,x1,x2},y} where y is either +1 or -1.

now Comes the ugly part. I Need the weight vector w = PseudoInverse[{1,x1,x2}].y

w = (PseudoInverse[#[] & /@ data]).(#[] & /@ data)


only {1,x1,x2} :

xlist = (#[] &) /@ data;


the sign of each element w.x (-1 or 1 depending on which class we classify it)

 sig = Sign /@ (w.# &) /@ xlist ;


only the y of our data Points

datasig = #[] & /@ data;


now we Count the "misclassified" elements

Length[Select[
Table[sig[[i]] != datasig[[i]], {i, 100}], # == True &]]


now I Need to repeat this Experiment a 1000 times and take the mean. But how do I do that. I'm coming from an imperative Point of view and this would be trivial there.

I'm also welcoming more elegant Solutions that more use functional and mathematica specific elements.

edit:

f := Module[{}, funktionsPunkte = RandomReal[{-1, 1}, {2, 2}];
targetFct[{x1_, x2_}] :=
With[{targetFunction = LinearModelFit[funktionsPunkte, {1, t}, t]},
x2 - targetFunction[x1]];
data = With[{points = RandomReal[{-1, 1}, {100, 2}]},
Map[{Prepend[#, 1], If[targetFct[#] < 0, -1, 1]} &, points]];
w = (PseudoInverse[#[] & /@ data]).(#[] & /@ data);
xlist = (#[] &) /@ data;
sig = Sign /@ (w.# &) /@ xlist;
datasig = #[] & /@ data;
Length[Select[
Table[sig[[i]] != datasig[[i]], {i, 100}], # == True &]]]


and then

f & /@ Range


it's awfully slow but it works (have not implemented the other suggestions yet)

• Pack your steps into one function and repeat $n$ times. Module or similar are useful for that. You can merge separate cells, but make sure you use the right amount of ;. Nov 19, 2013 at 15:17
• I should mention that this is an ungraded homework from a mooc course of caltech Nov 19, 2013 at 15:35
• funktionsPunkte, some Denglish there :D Nov 19, 2013 at 15:39
• Why don't you wait after it is graded? Nov 19, 2013 at 15:44
• @RolfMertig he said ungraded, right? Also from the homework: "You are also encouraged to take part in the forum [other forum] where there are lots of threads about each homework" Nov 19, 2013 at 15:50

One part of your question is about iteration. Building on Yves comment, one good way is to pack everything inside a function. To simplify your problem, say the function is:

f := RandomReal[{0, 1}];


Each time f is called, you get a new random number. (This greatly simplifies your problem, but the same idea holds.) Now to iterate 1000 times, you can use Map

f & /@ Range


and you get a list of the function, evaluated 1000 times.

In your case, you have a more complex function... Module will let you nicely place lots of commands together:

f := Module[{}, statement1; statement2; Sign[RandomReal[{-1, 1}]]]


Again, f & /@ Range will iterate this 1000 times.

You use #[[i]]&/@somelist a lot when instead you can use a part specification like: somelist[[All, i]] to get the i-th column.

To count miss-classified you can look at the difference of the lists and count all non-zero elements like:

Length[Select[Table[sig[[i]] != datasig[[i]], {i, 100}], # == True &]]

(* Same as: *)
Total@Unitize[sig - datasig]


The function Sign is Listable which means you don't have to use Map:

Sign[{-1, 2, 1}]
(* {-1, 1, 1} *)