python - Reshaping numpy array without using two for loops -
i have 2 numpy arrays
import numpy np x = np.linspace(1e10, 1e12, num=50) # 50 values y = np.linspace(1e5, 1e7, num=50) # 50 values x.shape # output (50,) y.shape # output (50,)
i create function returns array shaped (50,50)
such first x value x0
evaluated y values, etc.
the current function using complicated, let's use easier example. let's function is
def func(x,y): return x**2 + y**2
how shape (50,50)
array? @ moment, output 50 values. use loop inside array?
something like:
np.array([[func(x,y) in x] j in y)
but without using 2 loops. takes forever run.
edit: has been requested share "complicated" function. here goes:
there data vector 1d numpy array of 4000 measurements. there "normalized_matrix", shaped (4000,4000)---it nothing special, matrix entry values of integers between 0 , 1, e.g. 0.5567878. these 2 "given" inputs.
my function returns matrix multiplication product of transpose(datavector) * matrix * datavector, single value.
now, can see in code, have initialized 2 arrays, x , y, pass through series of "x parameters" , "y parameters". is, func(x,y)
return value x1
, value y1
, i.e. func(x1,y1)
?
the shape of matrix1
(50, 4000, 4000). shape of matrix2
(50, 4000, 4000). ditto total_matrix
.
normalized_matrix
shape (4000,4000) , id_mat
shaped (4000,4000).
normalized_matrix print normalized_matrix.shape #output (4000,4000) data_vector = datarr print datarr.shape #output (4000,) def func(x, y): matrix1 = x [:, none, none] * normalized_matrix[none, :, :] matrix2 = y[:, none, none] * id_mat[none, :, :] total_matrix = matrix1 + matrix2 # transpose(datavector) * matrix * datavector # matrix multiplication, equals single value return np.array([ np.dot(datarr.t, np.dot(total_matrix, datarr) ) ])
if try use np.meshgrid()
, is, if try
x = np.linspace(1e10, 1e12, num=50) # 50 values y = np.linspace(1e5, 1e7, num=50) # 50 values x, y = np.meshgrid(x,y) z = func(x, y)
i following value error: valueerror: operands not broadcast shapes (50,1,1,50) (1,4000,4000)
.
reshape
in numpy
different meaning. when start (100,)
, change (5,20)
or (10,10)
2d arrays, 'reshape. there a
numpy` function that.
you want take 2 1d array, , use generate 2d array function. taking outer product of 2, passing combinations of values through function.
some sort of double loop 1 way of doing this, whether explicit loop, or list comprehension. speeding depends on function.
for @ x**2+y**2
example, can 'vectorized' quite easily:
in [40]: x=np.linspace(1e10,1e12,num=10) in [45]: y=np.linspace(1e5,1e7,num=5) in [46]: z = x[:,none]**2 + y[none,:]**2 in [47]: z.shape out[47]: (10, 5)
this takes advantage of numpy
broadcasting. none
, x
reshaped (10,1)
, y
(1,5)
, , +
takes outer
sum.
x,y=np.meshgrid(x,y,indexing='ij')
produces 2 (10,5)
arrays can used same way. @ doc other parameters.
so if more complex function can written in way takes 2d arrays this, easy 'vectorize'.
but if function must take 2 scalars, , return scalar, stuck sort of double loop.
a list comprehension form of double loop is:
np.array([[x1**2+y1**2 y1 in y] x1 in x])
another is:
z=np.empty((10,5)) in range(10): j in range(5): z[i,j] = x[i]**2 + y[j]**2
this double loop can sped using np.vectorize
. takes user defined function, , returns 1 can take broadcastable arrays:
in [65]: vprod=np.vectorize(lambda x,y: x**2+y**2) in [66]: vprod(x[:,none],y[none,:]).shape out[66]: (10, 5)
test i've done in past show vectorize
can improve on list comprehension route 20%, improvement nothing writing function work 2d arrays in first place.
by way, sort of 'vectorization' question has been asked many times on numpy. beyond these broad examples, can't without knowning more more complicated function. long black box takes scalars, best can np.vectorize
. , still need understand broadcasting (with or without meshgrid
help).
Comments
Post a Comment