简体   繁体   English

如何从前一个整数中减去数组中的每个整数并求和

[英]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.

相关问题 如何从python中的字符串中减去整数? - How to have integer subtract from a string in python? 如何使用while循环从列表中的每个整数中减去最小的数字? - How do I subtract the smallest number from each integer in a list using a while loop? Python: Given a string s and an integer C I have to find the summation of difference of number of each alphabet and C - Python: Given a string s and an integer C I have to find the summation of difference of number of each alphabet and C 用python字典中的整数值替换文本中的值并找到它的总和 - Replace values in a text with Integer values in a python dictionary and find its summation 如何使用 Python 在数组中找到最大的 integer? - How to find greatest integer in array with Python? 如何找到数组列的平均值,然后从 pyspark dataframe 中的每个元素中减去平均值? - How to find the mean value of a array column and then subtract the mean from each element in a pyspark dataframe? 如何将 integer 拆分为 n 部分(每个整数)? - How to split an integer in n parts (each integer)? 在浮点数和整数列表中找到上一个数字 - Find the previous number in a list of float and integer 如何从上一行减去? - How to subtract from previous row? 如何在python的列表中找到每个具有模数的整数的乘积 - how to find product of each integer with modulo in a list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM