How do I create a one-dimensional array of squares and sum of the differences between the two four-dimensional arrays?

Asked 2 years ago, Updated 2 years ago, 78 views

What I want to do is to take the difference between the time and positional coordinates and see the growth rate by time.

data1=[t,x,y,z]
data2 = [t, x, y, z]

So I thought it would be good to take the difference between them.

l=data1.shape[0]
li=data1.shape[1]
lj=data1.shape[2]
lk = data1.shape[3]
lists = [ ]
s = 0
 for in range(0,l):
    for i in range(0, li):
       for i in range(0, li):
          for jin range(0,lk):
             s+=sum(np.power((data1[t,i,yn,j]-data2[t,i,yn,j])*2)))
 lists.insert(s)
print(lists)

But

s+=sum(np.power(data1[t,i,yn,j]-data2[t,i,yn,j])*2)))
ValueError: invalid number of arguments

error.What should I do?
Also, as a beginner, I could only think of a way to turn the for sentence, so please let me know if there is an efficient way.

python numpy

2022-09-30 21:44

1 Answers

ValueError: invalid number of arguments is due to insufficient number of arguments for np.power where np.power(a,b) denotes a multiplied by b.If it's squared, you can write np.power (2).

NumPy Array broadcasts the operation broadcasting, so this calculation makes it easy to write without using np.power.

np.sum(data1-data2)**2)

Or

(data1-data2)**2).sum()

In the above writing method, the sum is calculated by taking the square of the difference for all elements.

If you want to calculate the sum of the same indexers for the t axis, you can specify axis in np.sum where axis is an optional sum.

np.sum(data1-data2)**2,axis=(1,2,3))

Or

(data1-data2)**2).sum(axis=(1,2,3))


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.