BookmarkSubscribeRSS Feed
RO_C
Calcite | Level 5

I'm converting SAS code into Python and I've noticed a difference between the results of a SAS procedure and its Python equivalent.

 

For example :


The SAS code :
proc iml;
abc = {1 2 3 1,
4 5 6 1,
7 8 9 1,
1 1 1 1};

/* Calculation of eigenvalues (eigm) and eigenvectors (eigv) */
eigm = eigval(abc);
eigv = eigvec(abc);
print eigm, eigv;
quit;

the result :

image (1).png

 

The Python equivalent of this SAS code :

import numpy as np

# Definition of matrix abc
abc = np.array([[1, 2, 3, 1],
[4, 5, 6, 1],
[7, 8, 9, 1],
[1, 1, 1, 1]])

# Calculation of eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(abc)

print("Eigenvalues :", eigenvalues)
print("Eigenvectors :\n", eigenvectors)

 

unnamed1.png

 

Remarks:
It's easy to see that the values are the same in both sets of results, but they don't follow the same layout.

5 REPLIES 5
ballardw
Super User

If  layout is an issue then send one or more sets of results to a data set and then write code to show in the desired layout (and rounded/formatted for similarity if desired).

RO_C
Calcite | Level 5
In fact, the matrix I have is much larger than this one. I don't know what movements there will be in the other columns
Quentin
Super User

So it's just an issue of the ordering of the eigenvector columns, right?  The SAS docs explain the SAS ordering:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/imlug/imlug_langref_sect123.htm

 

The numpy docs explain that eigenvalues (and thus eigenvectors) are "not necessarily ordered."

https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Troy Martin Hughes presents Calling Open-Source Python Functions within SAS PROC FCMP: A Google Maps API Geocoding Adventure on Wednesday April 23.
Register now at https://www.basug.org/events.
RO_C
Calcite | Level 5
I need to use these results in another step, in which case the results obtained in SAS will be different from those obtained in Python, except that I absolutely must find the same results in SAS as in Python.
Mike_N
SAS Employee

I've moved this post to the IML board, which more closely aligns with this question. As pointed out by a previous answer, from the IML documentation, the eigenvalues for a non-symmetric matrix are sorted first by their real parts, then by the magnitude of their imaginary parts. You can get sorted eigenvalues from numpy using the numpy.linalg.eigh() function, but note that that function sorts eigenvalues in ascending order. 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 5 replies
  • 1663 views
  • 6 likes
  • 4 in conversation