Skip to content

Instantly share code, notes, and snippets.

@andreaskoepf
Created March 16, 2016 15:24
Show Gist options
  • Save andreaskoepf/937c2059ad11cc0768be to your computer and use it in GitHub Desktop.
Save andreaskoepf/937c2059ad11cc0768be to your computer and use it in GitHub Desktop.
local function pca(X)
-- PCA -------------------------------------------------------------------------
-- X is m x n
local mean = torch.mean(X, 1) -- 1 x n
local m = X:size(1)
local Xm = X - torch.ones(m, 1) * mean
Xm:div(math.sqrt(m - 1))
local v,s,_ = torch.svd(Xm:t())
s:cmul(s) -- n
return mean, v, s -- v:= eigenVectors, s:=eigenValues
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment