I'd like to calculate a dot product of two matrices, where one of them is a diagonal matrix. However, I don't want to use np.diag or np.diagflat in order to create the full matrix, but instead use the 1D array directly filled with the diagonal values. Is there any way or numpy operation which I can use for this kind of problem?
x = np.arange(9).reshape(3,3)
y = np.arange(3) # diagonal elements
z = np.dot(x, np.diag(y))
and the solution I'm looking for should be without np.diag
z = x ??? y
Directly multiplying the ndarray by your vector will work. Numpy conveniently assumes that you want to multiply the nth column of x by the nth element of your y.
x = np.random.random((5, 5)
y = np.random.random(5)
diagonal_y = np.diag(y)
z = np.dot(x, diagonal_y)
np.allclose(z, x * y) # Will return True
The Einstein summation is an elegant solution to these kind of problems:
import numpy as np
x = np.random.uniform(0,1, size=5)
w = np.random.uniform(0,1, size=(5, 3))
diagonal_x = np.diagflat(x)
z = np.dot(diagonal_x, w)
zz = np.einsum('i,ij->ij',x , w)
np.allclose(z, zz) # Will return True
See: https://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html#numpy.einsum
Related
I want a function that takes in 2 numbers and a matrix and does a multiplication operation using them and then sums every element of the matrix after multiplication (see code below).
I am trying to do this operation for all possible combinations of x and y. What is the best way to do this without using loops, since I know I could loop over the function with different values of x and y but this doesn't seem like an efficient way to do it.
I tried using a meshgrid as input but that didnt work due to the way the input is broadcasted.
import numpy as np
def my_func(num1, num2, matrix2):
return np.sum(matrix*num1*num2)
x = np.linspace(0,5)
y = np.linspace(0,1)
X, Y = np.meshgrid(x, y)
matrix = np.array([[1],[2],[3]])
a = my_func(X,Y,matrix)
I get the following error:
ValueError: operands could not be broadcast together with shapes (50,50) (3,1)
I would like a to equal a meshgrid of values where each value in the array corresponds to the output of my_func for every possible combination of x and y.
The result of x * y * M when x and y are scalar is just M.shape. If you want this result for each value of x and y, you will want a result of shape x.shape + y.shape + M.shape. You can do this with broadcasting for the totally general case. The idea is that you need to reshape x to have trailing ones too fill in y.ndim + M.ndim dimensions and y to have M.ndim trailing dimensions.
For the sake of the summation, it's actually easier to ravel M, even though np.sum allows for multiple axes since version 1.7.0.
def my_func(x, y, matrix):
x = np.reshape(x, x.shape + (1,) * (y.ndim + 1))
y = np.reshape(y, y.shape + (1,))
return (x * y * matrix.ravel()).sum(axis=-1)
If you want to input x and y that are already broadcasted together, you can adjust the calculation slightly:
def my_func(x, y, matrix):
return ((x * y)[:, None] * matrix.ravel()).sum(-1)
The conceptual difference is that the first version accepts the linspaces you created directly, while the second version requires you to construct the meshgrid, or at least transpose one of the arrays.
It looks like you have to adjust the dimensions of your x and y array:
import numpy as np
def my_func(num1, num2, matrix2):
return matrix*num1*num2
x = np.linspace(0,5, num=3)
y = np.linspace(0,1, num=3)
X, Y = np.meshgrid(x, y)
matrix = np.array([[1],[2],[3]])
a = my_func(X,Y,matrix)
print(a)
# [[ 0. 0. 0. ]
# [ 0. 2.5 5. ]
# [ 0. 7.5 15. ]]
I'm trying to put results of a calculus into a big matrix where the last dimension can be 1 or 2 or more.
so to put my result in the matrix I do
res[i,j,:,:] = y
If y is sized (N,2) or more than 2 it is find, but if y is sized (N) I got an error saying:
ValueError: could not broadcast input array from shape (10241) into shape (10241,1)
Small example:
import numpy as np
N=10
y = np.zeros((N,2))
res = np.zeros((2,2,N,2))
res[0,0,:,:]= y
y = np.zeros((N,1))
res = np.zeros((2,2,N,1))
res[0,0,:,:]= y
y = np.zeros(N)
res = np.zeros((2,2,N,1))
res[0,0,:,:]= y
I'm getting the error for the last example but they are both (y and res) 1D vector right?
I'm wondering if it exists a solution to make this assignment whatever the size of the last dimension (1, 2 or more)?
In my code I made an try except but could exist another way
try:
self.res[i,j,:,:] = self.ODE_solver(len(self.t))
except:
self.res[i, j, :, 0] = self.ODE_solver(len(self.t))
For the generic solution that works across all three scenarios, use -
res[0,0,:,:] = y.reshape(y.shape[0],-1)
So, basically, we are making y 2D while keeping the first axis length intact and changing the second one based on the leftover.
You can reshape y to be the last 2 dimensions of res.
N=10
y = np.zeros((N,2))
res = np.zeros((2,2,N,2))
res[0,0,:,:]= y.reshape(res.shape[-2:])
y = np.zeros((N,1))
res = np.zeros((2,2,N,1))
res[0,0,:,:]= y.reshape(res.shape[-2:])
y = np.zeros(N)
res = np.zeros((2,2,N,1))
res[0,0,:,:]= y.reshape(res.shape[-2:])
Can you pls explain how to create a matrix in python to be created in object datatype. My code :
w, h = 8, 5;
Matrix = ([[0 for x in range(w)] for y in range(h)],dtype=object)
gives a syntax error. I tried various other ways. But still none of them working.
Thanks a lot
In your code the Matrix line tries to create a tuple, however you are giving it an expression dtype=object.
Matrix = ([[0 for x in range(w)] for y in range(h)],dtype=object)
The line reads: Set matrix to the tuple (2D array, dtype=object). However, the second part cannot be set. You can create the matrix as follows:
Matrix = [[0 for x in range(w)] for y in range(h)]
Or if you would like to have a numpy array with dtype object:
import numpy as np
Matrix = np.array([[0 for x in range(w)] for y in range(h)], dtype=object)
Or even more clean:
import numpy as np
Matrix = np.zeros((h, w), dtype=object)
Let me present you two options using numpy module and loops.
import numpy as np
print("Using numpy module:")
x = np.array([1,5,2])
y = np.array([7,4,1])
sum = x + y
subtract = x - y
mult = x * y
div = x / y
print("Sum: {}".format(sum))
print("Subtraction: {}".format(subtract))
print("Multiplication: {}".format(mult))
print("Division: {}".format(div))
print("----------------------------------------")
print("Using for loops:")
x = [1,5,2]
y = [7,4,1]
sum = []
subtract = []
mult =[]
div = []
for i,j in zip(x,y):
sum.append(i+j)
subtract.append(i-j)
mult.append(i*j)
div.append(i/j)
print(sum)
print(subtract)
print(mult)
print(div)
I have three separate 1d arrays of a list of numbers, their squares and cubes (created by a 'for' loop).
I would like these arrays to appear in three corresponding columns, however I have tried the column_stack function and python says its not defined. I have read about the vstack and hstack functions but am confused about which to use and what exactly they do.
My code so far reads;
import numpy
makearange = lambda a: numpy.arange(int(a[0]),int(a[1]),int(a[2]))
x = makearange(raw_input('Enter start,stop,increment: ').split(','))
y = numpy.zeros(len(x), dtype=int)
z = numpy.zeros(len(x), dtype=int)
for i in range(len(x)):
y[i] = x[i]**2
for i in range(len(x)):
z[i] = x[i]**3
print 'original array: ',x
print 'squared array: ',y
print 'cubed array: ', z
I would appreciate any advice
Why don't you define y and z directly?
y = x**2
z = x**3
and then simply:
stacked = np.column_stack((x,y,z))
which gives you a 2D array of shape len(x) * 3
import numpy
makearange = lambda a: numpy.arange(int(a[0]),int(a[1]),int(a[2]))
x = makearange(raw_input('Enter start,stop,increment: ').split(','))
a = np.zeros((len(x),3))
a[:,0] = x
a[:,1] = x**2
a[:,2] = x**3
When using arrays you should avoid for loops as much as possible, that's kind of the point of arrays.
a = np.zeros((len(x),3)) creates an array of length same as x and with 3 columns
a[:,i] is a reference to the 'i'th column of this array (i.e. select all values (denoted by :) along this (i) column)
I would strongly recommend you look at the Numpy Tutorial.
You do want column_stack. Have you tried:
w = numpy.column_stack((x,y,z))
print(w)
In numpy, the numpy.dot() function can be used to calculate the matrix product of two 2D arrays. I have two 3D arrays X and Y (say), and I'd like to calculate the matrix Z where Z[i] == numpy.dot(X[i], Y[i]) for all i. Is this possible to do non-iteratively?
How about:
from numpy.core.umath_tests import inner1d
Z = inner1d(X,Y)
For example:
X = np.random.normal(size=(10,5))
Y = np.random.normal(size=(10,5))
Z1 = inner1d(X,Y)
Z2 = [np.dot(X[k],Y[k]) for k in range(10)]
print np.allclose(Z1,Z2)
returns True
Edit Correction since I didn't see the 3D part of the question
from numpy.core.umath_tests import matrix_multiply
X = np.random.normal(size=(10,5,3))
Y = np.random.normal(size=(10,3,5))
Z1 = matrix_multiply(X,Y)
Z2 = np.array([np.dot(X[k],Y[k]) for k in range(10)])
np.allclose(Z1,Z2) # <== returns True
This works because (as the docstring states), matrix_multiplyprovides
matrix_multiply(x1, x2[, out]) matrix
multiplication on last two dimensions