Newbie question about numpy

P

Paul Johnston

Hi I'm new to python and have just been taking a look at what it has
to offer.
I noted the lack of matrices so installed numpy
I know the documentation is chargable so wanted a quick play to see if
I should buy it
However

_________________________________________________________
from numpy import *

a = array([[1,2,3],[4,5,6],[1,2,3]])
b = array([[1,3,6],[2,5,1],[1,1,1]])

print 'a = \n',a,"\n"
print 'b = \n',b,"\n"

print 'a has shape ', a.shape
print 'b has shape ', b.shape, "\n"

print "a * b is \n", a * b
_________________________________________________________

Gives me
_________________________________________________________
a =
[[1 2 3]
[4 5 6]
[1 2 3]]

b =
[[1 3 6]
[2 5 1]
[1 1 1]]

a has shape (3, 3)
b has shape (3, 3)

a * b is
[[ 1 6 18]
[ 8 25 6]
[ 1 2 3]]
_________________________________________________________


I know its a long time since my degree but that's not matrix
multiplication is it ?

TIA Paul
 
G

Gabriel Genellina

from numpy import *

a = array([[1,2,3],[4,5,6],[1,2,3]])
b = array([[1,3,6],[2,5,1],[1,1,1]])
print "a * b is \n", a * b

I know its a long time since my degree but that's not matrix
multiplication is it ?

No, it's plain element-by-element multiplication.
You want matrixmultiply:
http://numpy.scipy.org/numpydoc/numpy-9.html#pgfId-36542



Gabriel Genellina
Softlab SRL





__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
R

Robert Kern

Paul said:
Hi I'm new to python and have just been taking a look at what it has
to offer.
I noted the lack of matrices so installed numpy

You will want to ask numpy questions on the numpy list.

http://www.scipy.org/Mailing_Lists

numpy arrays are not matrices; they are arrays. All of the arithmetic operations
on them are done element-wise. The dot() function will do matrix multiplication.

There is a matrix class (with the constructor numpy.mat(some_array)) that
derives from arrays and overrides the * operator to do matrix multiplication if
that is what you want. I prefer using dot() on regular arrays, myself.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
A

Avell Diroll

Paul Johnston wrote:
(snip)
I noted the lack of matrices so installed numpy (snip)
_________________________________________________________
from numpy import *

a = array([[1,2,3],[4,5,6],[1,2,3]])
b = array([[1,3,6],[2,5,1],[1,1,1]]) (snip)
print "a * b is \n", a * b
_________________________________________________________ (snip)
a * b is
[[ 1 6 18]
[ 8 25 6]
[ 1 2 3]]
_________________________________________________________


I know its a long time since my degree but that's not matrix
multiplication is it ?

You consider that a and b are matrices, but for the python interpreter
they are arrays so a*b returns the multiplication of 2 arrays.

For matrices multiplication, you could get a hint by typing the
following in the interpreter :

which could make you want to try the following code :
from numpy import *
a = array([[1,2,3],[4,5,6],[1,2,3]])
b = array([[1,3,6],[2,5,1],[1,1,1]])
print matrixmultiply(a,b)

....
output :
....

array([[ 8, 16, 11],
[20, 43, 35],
[ 8, 16, 11]])
....

HIH,
avell
 
R

Robert Kern

Avell said:
For matrices multiplication, you could get a hint by typing the
following in the interpreter :

Note that the name matrixmultiply() has been deprecated in favor of dot() for
many, many years now even in Numeric, numpy's predecessor. It has finally been
removed in recent versions of numpy.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
D

Dennis Lee Bieber

I know its a long time since my degree but that's not matrix
multiplication is it ?

Define "matrix multiplication"...

What you see appears to be multiplication of corresponding elements.

Were you expecting a dot product, or a cross product, or something
else?
a = numpy.array([[1,2,3],[4,5,6],[1,2,3]])
b = numpy.array([[1,3,6],[2,5,1],[1,1,1]])
a * b
array([[ 1, 6, 18],
[ 8, 25, 6],
[ 1, 2, 3]])array([[ 8, 16, 11],
[20, 43, 35],
[ 8, 16, 11]])array([[ 3, -3, 1],
[-25, 8, 10],
[ -1, 2, -1]])--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

And does this look familiar?
import numpy
a = numpy.array([[2, 0, -1, 1],[1, 2, 0, 1]])
a
array([[ 2, 0, -1, 1],
[ 1, 2, 0, 1]])
b = numpy.array([[1, 5, -7], [1, 1, 0], [0, -1, 1], [2, 0, 0]])
b
array([[ 1, 5, -7],
[ 1, 1, 0],
[ 0, -1, 1],
[ 2, 0, 0]])array([[ 4, 11, -15],
[ 5, 7, -7]])
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top