Fast way to subtract the mean from a dataset
Do this:
Standardize[data, Mean, 1 &]
Here are the timings for comparison, with f1
and f2
as defined in Mr.Wizard's answer:
big = RandomReal[{0, 50}, {1*^7, 2}];
f1[big] // RepeatedTiming // First
f2[big] // RepeatedTiming // First
Standardize[big, Mean, 1 &] // RepeatedTiming // First
0.274
0.232
0.12
This is a modest improvement on your code:
f2[data_] := (data\[Transpose] - Mean[data])\[Transpose]
With your code as a function f1
for reference:
f1[data_] := Transpose[# - Mean /@ # &[Transpose[data]]]
And now with Simon Woods's f3
calling the internal function used by Standardize
in v11:
f3[data_] :=
Module[{a = data}, Statistics`Library`MatrixRowTranslate[a, -Mean[a]]; a]
Timings:
big = RandomReal[{0, 50}, {1*^7, 2}];
f1[big] // RepeatedTiming // First
f2[big] // RepeatedTiming // First
f3[big] // RepeatedTiming // First
0.193 0.15 0.0936
f1[big] === f2[big] === f3[big]
True
In Mathematica 10.1 under Windows x64 Standardize
performs rather poorly. I get:
Standardize[big, Mean, 1 &] // RepeatedTiming // First
0.852
I am curious to know if this is version or platform dependent, or perhaps both.