Issue with numpy matrix multiplication - python

I'm trying to multiply two matrices of dimensions (17,2) by transposing one of the matrices
Here is example p1
p1 = [[ 0.15520622 -0.92034567]
[ 0.43294367 -1.05921439]
[ 0.7569707 -1.15179354]
[ 1.08099772 -1.15179354]
[ 1.35873517 -0.96663524]
[-1.51121847 -0.64260822]
[-1.32606018 -0.87405609]
[-1.00203315 -0.96663524]
[-0.67800613 -0.96663524]
[-0.3539791 -0.87405609]
[ 0.89583942 1.02381648]
[ 0.66439155 1.3478435 ]
[ 0.3866541 1.48671223]
[ 0.15520622 1.5330018 ]
[-0.07624165 1.5330018 ]
[-0.3539791 1.44042265]
[-0.58542698 1.20897478]]
here is another example matrix p2
p2 = [[ 0.20932473 -0.90029958]
[ 0.53753779 -1.03849455]
[ 0.88302521 -1.10759204]
[ 1.24578701 -1.02122018]
[ 1.47035383 -0.77937898]
[-1.46628927 -0.69300713]
[-1.29354556 -0.9521227 ]
[-0.96533251 -1.03849455]
[-0.63711946 -1.00394581]
[-0.3089064 -0.90029958]
[ 0.86575084 1.06897874]
[ 0.55481216 1.37991742]
[ 0.26114785 1.50083802]
[ 0.03658102 1.51811239]
[-0.1879858 1.50083802]
[-0.46437574 1.37991742]
[-0.74076568 1.08625311]]
I'm trying to multiply them using numpy
import numpy
print(p1.T * p2)
But I'm getting the following error
operands could not be broadcast together with shapes (2,17) (17,2)
This is the expected matrix multiplication output
[[11.58117944 2.21072324]
[-0.51754442 22.28728876]]
Where exactly am I going wrong

Matrix multiplication is done with np.dot(p1.T,p2), because
A * B means matrix elements-wise multiply.

So you should use np.dot:
p1.T.dot(p2)

Sorry for a vague question. Initially, I was getting p1 and p2 values from numpy matrix. I later stored them in json file as list for optimization by using
.tolist()
method and was reading it back as numpy array using
numpy.array()
method which is apparently wrong..I changed my code to read the numpy array using
numpy.matrix()
method which seems to solve the issue. Hope this helps someone

Related

Convert a list to an 3 dimensional numpy array

I have a list, which consist collection of differently shaped 2 dimensional numpy arrays inside it. It looks like this.
My goal is to convert this to a 3d numpy array so that its structure is something like this
[
[[ ]
[ ]
:
],
[[ ]
[ ]
:
],
:
]
or in words
[
two dimensional array 1,
two dimensional array 2,
two dimensional array 3,
:
:
]
I tried doing
arr = np.array(garbage)
that gives me an array but it is not structured as I described it. Its shape comes out to be (40336,)
I have to pass the array to a RNN. Do I have to pad zeros for all internal 2 dimensional arrays so that they are of same shape which will make the outer array of the three dimensional shape that I want?

Python: Turn lists into matrix

I am very new to python and I am wondering if I could get help with how to save vector autoregressive's results as a matrix. I have successfully gotten the VAR results following the code below.
from statsmodels.tsa.api import VAR
varmodel = VAR(df)
results = varmodel.fit()
print(results.coefs)
Then the results I got are:
[[[ 0.1182087 -0.1512611 0.0757709 -0.53515347]
[ 0.35138686 0.19483162 -0.01398611 -0.13697023]
[ 0.24409855 0.36790842 0.90589776 0.41936542]
[ 0.18225916 -0.01139466 0.05554881 0.47024742]]]
The dimension of the results shown above is (row, column)= (1,4). I am wondering how I could make them a 4-by-4 matrix.
Looks like it is 1x4x4. You can reshape it with
results.coefs.reshape((4, 4))

Distance with array of different sizes

I have an array with dimensions as such:
pos = np.array([[ 1.72, 2.56],
[ 0.24, 5.67],
[ -1.24, 5.45],
[ -3.17, -0.23],
[ 1.17, -1.23],
[ 1.12, 1.08]])
and I want to find the distance between each line of the array to an index point which would be
ref = np.array([1.22, 1.18])
I would thus have an array with 4 elements as an answer but I'm really confused as to the method of approaching this with only numpy as I've tried many ways yet the size of the ref array presents a challenge. Thanks for the help.
The expected answer is an array with 6 elements. The elements are approximately:
[ 1.468, 4.596, 4.928 , 4.611, 2.410, 0.141 ]
Using numpy and assuming Euclidean metric:
import numpy as np
np.linalg.norm(pos - ref, axis=1)
If you need a Python list (instead of numpy array), add .tolist() to the previous line:
np.linalg.norm(pos - ref, axis=1).tolist()

SciPy method eigsh giving nonintuitive results

I tried to use SciPy function linalg.eigsh to calculate a few eigenvalues and eigenvectors of a matrix. However, when I print the calculated eigenvectors, they are of the same dimension as the number of eigenvalues I wanted to calculate. Shouldn't it give me the actual eigenvector, whose dimension is the same as that of the original matrix?
My code for reference:
id = np.eye(13)
val, vec = sp.sparse.linalg.eigsh(id, k = 2)
print(vec[1])
Which gives me:
[-0.26158945 0.63952164]
While intuitively it should have a dimension of 13. And it should not be a non-integer value either. Is it just my misinterpretation of the function? If so, is there any other function in Python that can calculate a few eigenvectors (I don't want the full spectrum) of the wanted dimensionality?
vec is an array with shape (13, 2).
In [21]: vec
Out[21]:
array([[ 0.36312724, -0.04921923],
[-0.26158945, 0.63952164],
[ 0.41693924, 0.34811192],
[ 0.30068329, -0.11360339],
[-0.05388733, -0.3225355 ],
[ 0.47402124, -0.28180261],
[ 0.50581823, 0.29527393],
[ 0.06687073, 0.19762049],
[ 0.103382 , 0.29724875],
[-0.09819873, 0.00949533],
[ 0.05458907, -0.22466131],
[ 0.15499849, 0.0621803 ],
[ 0.01420219, 0.04509334]])
The eigenvectors are stored in the columns of vec. To see the first eigenvector, use vec[:, 0]. When you printed vec[0] (which is equivalent to vec[0, :]), you printed the first row of vec, which is just the first components of the two eigenvectors.

Slicing a numpy 3-d matrix into 2-d matrix

I have a 3d numpy matrix t as follows, generated randomly:
t = np.random.rand(2,2,2)
array([[[ 0.80351862, 0.25631294],
[ 0.7971346 , 0.29468456]],
[[ 0.33771957, 0.91776256],
[ 0.6018604 , 0.55290615]]])
I want to extract a 2-d matrix such that the result is sliced along the columns of the 3-d matrix. Something like:
array([[ 0.25631294 , 0.91776256],
[ 0.29468456, 0.55290615]])
How can I slice in such a way?
Thanks for the help.
That's just taking the last dim, with a transpose:
>>> t[:,:,1].T
array([[ 0.25631294, 0.91776256],
[ 0.29468456, 0.55290615]])
You could do a combination of a slice, a reshape and a transpose, like so:
t[:, :, 1:].reshape((2, 2)).T
I hope it helps

Categories