## Principal component analysis in GAMS

Problems with modeling
Luca
User Posts: 23
Joined: 1 year ago

### Principal component analysis in GAMS

Dear all,
I would like to implement principal component analysis in GAMS. I would ask you if there are linear algebra facilities in GAMS (such as tools to compute eigenvalues...) or there are libraries already available for PCA in GAMS.

Thanks very much for your help. Best regards,
Luca

bussieck
Moderator Posts: 346
Joined: 3 years ago

### Re: Principal component analysis in GAMS

Luca,

GAMS ships a couple of matrix algebra tools like eigenvalue, eigenvector, inverse, etc, see https://www.gams.com/latest/docs/T_MAIN ... SFORMATION. The principle use of these utilities is shown in e.g. https://www.gams.com/latest/testlib_ml/ ... val01.html.

You can also use GAMS Embedded Python Code and numpy or for sparse matrices scipy. Here is a simple dense code with numpy:

Code: Select all

``````set i /i1*i3/;
alias (i,j);

table a(i,j)
i1   i2   i3
i1     9    1    1
i2     1    9    1
i3     1    1    9;

parameter eVal(i) 'eigenvalues';
parameter eVec(i,j) 'eigenvectors';

embeddedCode Python:
import numpy as np
from numpy import linalg as LA

i = list(gams.get('i'))
id = { k:k for k in zip(i,range(len(i))) }
A = np.zeros(shape=(len(i),len(i)))
for a in gams.get('a'):
A[id[a],id[a]] = a
e = LA.eigh(A)
gams.set('eVal', [ (k,k) for k in zip(i,e) ])
eVec = []
for k in zip(i,e):
for j in zip(i,k):
eVec.append((k,j,j))
gams.set('eVec', eVec)

endEmbeddedCode eVal eVec
display eVal, eVec;
``````
You might have to install numpy/scipy into your GAMS/Python. See details at https://www.gams.com/latest/docs/UG_EmbeddedCode.html. I don't know good the numpy/scipy algorithms are, the algorithms in the GAMS tools (like eigenvector) work with sparse matrix algebra (e.g. http://www.netlib.org/lapack/) and scale nicely.

-Michael