-2

MY input(just for example):

from numpy import * 

x=[['1' '7']
 ['1.5' '8']
 ['2' '5.5']
 ['2' '9']]

I want to make next thing on random matrix:

1. for each row calculate:

> for example first row:    [1;7]*[1,7] = [[1,  7];      #value * value.transpose
                                          [7,  49]]

> for example second row:   [1.5;8]*[1.5,8]=  [[2.25, 12];
                                               [12,  64]]
 >.......

This is simple with numpy, because transpose is just x.T, if x=[1,7] This must be calculated for every row on matrix!

2. now I want to sum as in this way...

[1+2.25+...         7+12+......  ]
[                                ]           
[7+12+....          49+64+....   ]

So result is this matrix.

Any ideas?


EDIT2:

x=[['1','7']
 ['1.5', '8']
 ['2', '5.5']
 ['2','9']]

y = x[:, :, None] * x[:, None]
print y.sum(axis=0)

I received error:

"list indices must be integers, not tuple"

But if x is x = numpy.array([[1, 7], [1.5, 8], [2, 5.5], [2, 9]]) then it's ok, but I don't have such input.

thaking
  • 3,495
  • 8
  • 27
  • 33

2 Answers2

0

How about the following:

In [1]: import numpy as np

In [2]: x=np.array([[1, 7],[1.5, 8],[2, 5.5],[2, 9]])

In [3]: np.sum(np.outer(row,row) for row in x)
Out[3]: 
array([[  11.25,   48.  ],
       [  48.  ,  224.25]])
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

First, you should create the matrix containing floating point numbers instead of strings:

x = numpy.array([[1, 7], [1.5, 8], [2, 5.5], [2, 9]])

Next, you can use NumPy's broadcasting rules to build the product matrices:

y = x[:, :, None] * x[:, None]

Finally, sum over all matrices:

print y.sum(axis=0)

printing

[[  11.25   48.  ]
 [  48.    224.25]]

Note that this solution avoids any Python loops.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Hmmm, this is perfect for me; but my input is: x=[['1' '7'] ['1.5' '8'] ['2' '5.5'] ['2' '9']] How can implement your code for this? i try with array, but no sucess... – thaking Apr 29 '11 at 19:04
  • @thaking: Why does your matrix contain strings? Usually, you sould initialise it in a way that you have a matrix of floating point number right from the beginning. If this is impossible for any reason, you can convert the matrix using `x = x.astype(float)`. (Note that I can't imagine any reason why this should be necessary.) – Sven Marnach Apr 29 '11 at 19:09
  • Please look at my EDIT 2, where I described my problem with input data...and why I can't get matrix..thanks – thaking Apr 29 '11 at 19:21
  • @thaking: This seems to be a rather unrelated question. You might want to ask a new question for this. Make sure that you explain where your "original input" comes from in that new question. – Sven Marnach Apr 29 '11 at 19:29
  • @thaking: As stated in my post, you should initialise your matrix as an *NumPy array of floats*, and not, as you did in the edit, as *a list of strings*. NumPy arrays and Python lists are very different data types, and your are trying to use the same code for both of them. – Sven Marnach Apr 29 '11 at 19:59