Matrix multiplication python shapes not aligned - python

I am currently working on an assignment where we have to multiply four matrices (a.T, b, M, a.T) and I keep getting the error "ValueError: shapes (2,1) and (2,1) not aligned: 1 (dim 1) != 2 (dim 0)".
I have tried implementing it without the transpose which works fine but as soon as I add the transpose, the error shows up so I am assuming it has something to do with the transpose. I have never programmed in Python before so I would really appreciate some help.
def matrix_function(M, a, b):
part1=a.T.dot(b)
part2=M.dot(a.T)
out=part1.dot(part2)
return out
with:
M = np.array(range(4)).reshape((2,2))
a = np.array([[1,1]])
b = np.array([[10, 10]]).T
The assignment says the expected result is
(20 100) of shape (2,1) but I get neither the result nor the shape right. Can somebody help me? Thanks in advance

Related

Numpy product of a vector and it's transpose

I have a N x N complex NumPy array U_0 and I have to do manipulation with it :
First, how can I increase the array with zero efficiently ? I can simply copy it into an np.zeros((2N-1, 2N-1)) but maybe you guys know a better method. Thanks to Alexander Riedel for answer this question with the solution of numpy.pad
Second, I tried with
b = np.array([1,2,3])
I saw on previous post to transpose a 1D vector you can do
b_T = b[..., None]
# or
b_T = np.atleast_2d(b).T
but when I try b_T.dot(b) I get shapes (3,1) and (3,) not aligned: 1 (dim 1) != 3 (dim 0). I don't know how to get b into a shape of (1,3) instead of (3,).
Thanks
You can use the expand_dims function to do what you want. The problem here is that numpy does not consider a shape (3, 1) and a (3, ) equivalent. Alternatively look into the matrix type
Filling the array with zeros, as the commenters pointed out is also the answer to your first question. If that is not efficient enough, look into using sparse matrices from scipy, maybe they have the features you're looking for.

How can I multiply unaligned numpy matrices in python?

I've got two numpy matrices: the first, indata has a shape of (2, 0). The second (self.Ws[0] in my code) has a shape of (100, 0).
Is it possible to multiply these matrices by each other?
def Evaluate(self, indata):
sum = np.dot(self.Ws[0], indata) + self.bs[0]
self.As[0] = self.sigmoid(sum)
for i in range(1, len(self.numLayers)):
sum = np.dot(self.Ws[i], self.As[i-1] + self.bs[i])
self.As[i] = self.softmax(sum)
return self.As[len(self.numLayers)-1]
The error I'm getting when running this code is the following:
File "C:/Users/1/PycharmProjects/Assignment4PartC/Program.py", line 28, in main
NN.Train(10000, 0.1)
File "C:\Users\1\PycharmProjects\Assignment4PartC\Network.py", line 53, in Train
self.Evaluate(self.X[i])
File "C:\Users\1\PycharmProjects\Assignment4PartC\Network.py", line 38, in Evaluate
sum = np.dot(self.Ws[0], indata) + self.bs[0]
ValueError: shapes (100,) and (2,) not aligned: 100 (dim 0) != 2 (dim 0)
Hopefully somebody can help me out with this -- any help is appreciated! If anyone needs more granular information about what I'm running, just let me know and I'll update my post.
There is no such thing as shape (N, 0) for an array unless the array is empty. What you have is probably of shape (2,) and (100,). One way of multiplying these objects is:
np.dot(self.Ws[0].reshape((-1, 1)), indata.reshape((1, -1)))
This is going to give you a (100, 2) array.
Whether this is what you want to get from a mathematical perspective it is really hard to say.

Broadcasting assignment with advanced indexing

Scratching my head about this assignment, which does not behave as expected:
a = np.arange(24).reshape(4,3,2)
b = np.array([-1,-2,-3])
c = np.array([1])
a[...,c] = b
=> ValueError: shape mismatch: value array of shape (3,) could not be broadcast to indexing result of shape (1,4,3)
I expected
The indexing result shape to be (4,3,1) instead of (1,4,3), and
The right side shape of (3,) to actually be broadcastable to (1,4,3)?
Am I missing something here?
This should work. Indexing with [:, None] changes the orientation of the array.
a[...,c] = b[:, None]
Your first expectation is True. If you try a[...,c].shape it is 4,3,1. Not sure why the error says 1,3,4.
For the second expectation, you are assigning a vector (1D) of size 3 to a 3-D matrix (4,3,1). To do this, you need to make the vector b 2-D of shape (3,1). This can be done:
a[...,c] = b[None].T
See Numpy broadcasting

Array stacking/ concatenation error in python

I am trying to concatenate two arrays: a and b, where
a.shape
(1460,10)
b.shape
(1460,)
I tried using hstack and concatenate as:
np.hstack((a,b))
c=np.concatenate(a,b,0)
I am stuck with the error
ValueError: all the input arrays must have same number of dimensions
Please guide me for concatenation and generating array c with dimensions 1460 x 11.
Try
b = np.expand_dims( b,axis=1 )
then
np.hstack((a,b))
or
np.concatenate( (a,b) , axis=1)
will work properly.
np.c_[a, b] concatenates along the last axis.
Per the docs,
... arrays will be stacked along their last axis after
being upgraded to at least 2-D with 1's post-pended to the shape
Since b has shape (1460,) its shape gets upgraded to (1460, 1) before concatenation along the last axis.
In [26]: c = np.c_[a,b]
In [27]: c.shape
Out[27]: (1460, 11)
The most basic operation that works is:
np.concatenate((a,b[:,None]),axis=1)
The [:,None] bit turns b into a (1060,1) array. Now the 1st dimensions of both arrays match, and you can easily concatenate on the 2nd.
There a many ways of adding the 2nd dimension to b, such as reshape and expanddims. hstack uses atleast_1d which does not help in this case. atleast_2d adds the None on the wrong side. I strongly advocate learning the [:,None] syntax.
Once the arrays are both 2d and match on the correct dimensions, concatenation is easy.

Python- list with arrays shape issues

I'm creating a code to run a perceptron algorithm and I can't create a random matrix the way I need it:
from random import choice
from numpy import array, dot, random
unit_step = lambda x: -1 if x < 0 else 1
import numpy as np
m=3 #this will be the number of rows
allys=[]
for j in range(m):
aa=np.random.rand(1,3)
tt=np.random.rand(3)
yy=dot(aa,tt)
ally = [aa, yy]
allys.append(ally)
print "allys", allys
w = random.rand(3)
errors = []
eta = 0.2
n = 10
x=[1,3]
for i in xrange(n):
print i
x, expected = choice(allys)
print "x", x
And I get the problem here:
result = dot(x,w)
error = expected - unit_step(result)
errors.append(error)
w += eta * error * x
print x, expected, w, error, result, errors
The log says
w += eta * error * x
ValueError: non-broadcastable output operand with shape (3,) doesn't
match the broadcast shape (1,3)
The idea is to get result looping randomly over the "table" allys.
How can I solve this? What is shape(3,)?
Thanks!
The error message actually tells you what is wrong. The result of your multiplication is a (1,3) array (2D array, one row, three columns), whereas you try to add it into a 3-element vector.
Both arrays have three elements in a row, so if you do this:
w = w + eta * error * x
there will be no error on that line, but the resulting vector will actually be a (1,3) array. That is unwanted, because then your dot does not work.
There are several ways to fix the problem, but possibly the easiest to read is to reshape x for the calculation to be a 3-element vector (1D array):
w += eta * error * x.reshape(3,)
Possibly a cleaner solution would be to define w as a (1,3) 2D array, as well, and then transpose w for the dot. This is really a matter of taste.
For numpy arrays, shape attribute returns array's dimensionality. In your case, w.shape is (3,). This means that w is a one-dimensional array with 3 elements. In turn, x.shape is (1,3), which means that x is a two-dimensional array with one row and 3 columns. You are getting an error, because the interpreter is confused on how to match the shapes. I am not sure what you are trying to do, so it's hard to suggest the solution. But you might want to try reshaping one of the arrays. For example, x = x.reshape((3,)) for adapting the shape of x to w.

Categories

Resources