[Tutor] correlation matrix
noufal at airtelbroadband.in
Sat Jul 21 20:54:52 CEST 2007
Beanan O Loughlin wrote:
> hi all,
> I am new to python, and indeed to programming, but i have a question
> regarding correlation matrices.
> If i have a column vector
> and its transpose row vector
> is there a simple way in python to create a 5x5 correlation matrix,
> obviously symmetric and having 1's on the diagonal?
My knowledge of matrix algebra and statistics is quite rusty but from a
programming perspective, I'd understand this like so.
You want a 5x5 table of values (correlations in this case) computed from
all permutations of 2 variables selected from a set of 5 elements.
so, you'd want something like
cor(1,1) cor(1,2) cor(1,3) ....
cor(2,1) cor(2,2) cor(2,3) ...
If so, I think this might be what you're looking for (I've put some
comments to make it explanatory).
>>> import Numeric
>>> ret = 
>>> for i in x:
... for j in x: # Since we're correlating an array with itself.
... ret1.append((i,j,)) # You'd need to call the actual correlation
function here rather than just say (i,j)
The "Numeric" module has fast implementations of matrices and other
I'm quite sure that this thing which I've written above can be improved
on but I think it would do your work for you.
More information about the Tutor