2
votes

In one of my codes, I use numpy for matrices calculations.

At one point, I have to do the outer product between 2 vectors to get a matrix. That's where I'm stuck. At first, I tried numpy.dot, or other matrix product, but when the arguments are both 1D, it only does the scalar product, which not what I want. Then I found that numpy.outer does exactly what I want : a column * a line.

The thing is, my vectors are not arrays. Since they result from a numpy.dot operation, they are ndarray objects. But ndarrays do not have an outer method. I have tried everything I found on the Internet to convert my ndarrays to simple arrays. But nothing works, I still have a ndarray and the same attribute error again and again.

Now I don't know what to try, so I wanted to check if you knew another way to do this outer product, before I do some nasty things implying cloning the values in a array.

Thank you very much for your help.

1
So just to clarify: you want to convert an ndarray object to an array of arrays? - Capn Jack
Someone may correct me, but I think all numpy arrays are instances of the numpy.ndarray class. There is no such class as simply array. Arrays do not have an outer method, outer is just a plain old function in the numpy module. You use it like numpy.outer(x, y) - myrtlecat
If I do numpy.outer, I just get this error : AttributeError: 'numpy.ndarray' object has no attribute 'outer'. It's not that I want to convert my ndarray (which is 1D) to an array, it is just something I thought about to do my outer product. But if there is another way, I could try it - Kwikerr
Did you call one of your variables numpy? Can you post a minimal, complete, verifiable example? - myrtlecat
Mea Culpa, numpy.outer seem to work. I just thought it was a method and tried it the wrong way. Thank you very much for your help - Kwikerr

1 Answers

3
votes

outer is not a method of any class, it is just a plain old function found in the numpy module.

Here is an example of how to use it:

import numpy
x = numpy.array([1, 2, 3])
y = numpy.array([4, 5, 6])
# x.__class__ and y.__class__ are both 'numpy.ndarray'

outer_product = numpy.outer(x, y)
# outer_product has the value:
# array([[ 4,  5,  6],
#        [ 8, 10, 12],
#        [12, 15, 18]])