在Python中集成离散点 [英] Integrating Discrete point in Python

查看:219
本文介绍了在Python中集成离散点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个numpy数组(x,y)-

I have two numpy array (x,y)-

import numpy as np
import scipy 
from scipy.integrate import simps

y=np.array([1,1,2,1,-2])
x=np.array([0,1,2,3,4])

在绘制时看起来像这样-(在蓝线中) 黑线突出显示实际点.我希望在x轴上不在原始数据集中的两个点(标记为红线)之间进行积分.目标是在上图中找到灰色阴影区域(两条红线之间).

Which when plotted look like this - (in Blue line) The black lines highlight the actual points. I wish to integrate between two points (marked red line) on x axis which are not in the original dataset. The goal is to find the area shaded in gray (between the two red lines) in the figure above.

我该如何在python中执行此操作?使用python SciPy库,我可以像这样

How do I do it in python? Using python SciPy library I can integrate like this

scipy.integrate.trapz(y,x)

这给了我灰色区域阴影区域-

That gives me the area shaded in the gray region-

但是,如果我在x轴上的点1.5和2.2之间进行积分,则陷印会在下面的灰色阴影区域给出该区域-

But if I integrate between the points say 1.5 and 2.2 on x axis, the trapz gives the area shaded in gray below-

我该如何正确.

PS-折线图不能表示为函数,因为原始数组中有很多随机点.

PS- The line graph cannot be expressed as a function as there are many random points in the original array.

任何正确方向的见识都会有所帮助

Any insight in the right direction would be helpful

推荐答案

scipy插值器(例如

The scipy interpolators (such as InterpolatedUnivariateSpline) have an integral method. For example,

In [23]: from scipy.interpolate import InterpolatedUnivariateSpline

In [24]: x = np.array([0, 1, 2, 3, 4])

In [25]: y = np.array([1, 1, 2, 1, -2])

In [26]: f = InterpolatedUnivariateSpline(x, y, k=1)  # k=1 gives linear interpolation

In [27]: f.integral(1.5, 2.2)
Out[27]: 1.2550000000000003

这篇关于在Python中集成离散点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆