[英]How to subtract each integer in an array from the previous integer and find the summation
I have a numpy array that has the size (122,) and is made up of integers我有一个大小为 (122,) 并且由整数组成的 numpy 数组
I want to use the following formula:我想使用以下公式:
I tried doing我试着做
value=sum((I[:-1]-I[1:])**2))
This line itself does not create an error, but whenever I try to use this value in the bigger formula it gives an invalid syntax error.这一行本身不会产生错误,但是每当我尝试在更大的公式中使用这个值时,它都会给出一个无效的语法错误。
I want to use this value to plug into a bigger formula我想用这个值插入一个更大的公式
I would like an answer of how to plug my array into this formula For instance, the code for the second term in the bigger formula, I have written as follows:我想知道如何将我的数组插入这个公式的答案例如,较大公式中第二项的代码,我写的如下:
calc = np.linalg.norm((RR_intervals[:-1])-(RR_intervals[1:]))
p=(n-1)*sqrt(2)
o=(1/p)*calc
t=o**2
If I try and combine this with the first term I get an invalid syntax error如果我尝试将它与第一个术语结合起来,我会得到一个无效的语法错误
Full interpreter message: runfile('/home/user_1/p01.py', wdir='/home/user_1') Traceback (most recent call last):完整的解释器消息: runfile('/home/user_1/p01.py', wdir='/home/user_1') 回溯(最近一次调用最后一次):
File "/home/user_1/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3291, in run_code exec(code_obj, self.user_global_ns, self.user_ns)文件“/home/user_1/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py”,第3291行,在run_code exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in runfile('/home/user_1/p01.py', wdir='/home/user_1') File "", line 1, in runfile('/home/user_1/p01.py', wdir='/home/user_1')
File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile execfile(filename, namespace)文件“/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py”,第 705 行,在运行文件 execfile(filename, namespace) 中
File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)文件“/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py”,第 102 行,在 execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/user_1/p01.py", line 91 dispersion = (sqrt(mt)) ^ SyntaxError: invalid syntax文件“/home/user_1/p01.py”,第 91 行分散 = (sqrt(mt)) ^ SyntaxError: invalid syntax
You can do using list :您可以使用 list :
a = list(numpy_array)
sum([(i-j)**2 for i,j in zip(a,a[1:])])
这应该这样做:
result = sum((ar[i] - ar[i + 1])**2 for i in range(len(ar) - 1))
Using numpy:使用 numpy:
(((a-np.roll(a,1))**2)[1:]).sum()
Note: The [1:]
is necessary because roll
reinserts the element at the beginning of the array.注意: [1:]
是必需的,因为roll
在数组的开头重新插入元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.